Skip to content

Commit

Permalink
Add extension testing for SQlite
Browse files Browse the repository at this point in the history
Extends x.py to download an appropriate shared object file for supported
operating systems, and uses cURL to fetch one into the GitHub Actions
context for use by CI.
  • Loading branch information
bradfier committed Aug 25, 2022
1 parent 487fed7 commit 9d1b241
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/sqlx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ jobs:
steps:
- uses: actions/checkout@v2

- uses: wei/curl@master
with:
args: -O https://github.com/nalgeon/sqlean/releases/download/0.15.2/ipaddr.so

- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand All @@ -164,6 +168,7 @@ jobs:
--test-threads=1
env:
DATABASE_URL: sqlite://tests/sqlite/sqlite.db
RUSTFLAGS: --cfg sqlite_ipaddr

postgres:
name: Postgres
Expand Down
13 changes: 13 additions & 0 deletions tests/sqlite/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use sqlx::{
SqliteConnection, SqlitePool, Statement, TypeInfo,
};
use sqlx_test::new;
use std::str::FromStr;

#[sqlx_macros::test]
async fn it_connects() -> anyhow::Result<()> {
Expand Down Expand Up @@ -204,6 +205,18 @@ async fn it_executes_with_pool() -> anyhow::Result<()> {
Ok(())
}

#[cfg(sqlite_ipaddr)]
#[sqlx_macros::test]
async fn it_opens_with_extension() -> anyhow::Result<()> {
let opts = SqliteConnectOptions::from_str(&dotenvy::var("DATABASE_URL")?)?.extension("ipaddr");

let mut conn = SqliteConnection::connect_with(&opts).await?;
conn.execute("SELECT ipmasklen('192.168.16.12/24');").await?;
conn.close().await?;

Ok(())
}

#[sqlx_macros::test]
async fn it_opens_in_memory() -> anyhow::Result<()> {
// If the filename is ":memory:", then a private, temporary in-memory database
Expand Down
39 changes: 39 additions & 0 deletions tests/x.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import sys
import time
import argparse
import platform
import urllib.request
from glob import glob
from docker import start_database

Expand All @@ -23,6 +25,36 @@
dir_tests = os.path.join(dir_workspace, "tests")


def maybe_fetch_sqlite_extension():
"""
For supported platforms, if we're testing SQLite and the file isn't
already present, grab a simple extension for testing.
Returns the extension name if it was downloaded successfully or `None` if not.
"""
BASE_URL = "https://github.com/nalgeon/sqlean/releases/download/0.15.2/"
if platform.system() == "Darwin":
if platform.machine() == "arm64":
download_url = BASE_URL + "/ipaddr.arm64.dylib"
filename = "ipaddr.dylib"
else:
download_url = BASE_URL + "/ipaddr.dylib"
filename = "ipaddr.dylib"
elif platform.system() == "Linux":
download_url = BASE_URL + "/ipaddr.so"
filename = "ipaddr.so"
else:
# Unsupported OS
return None

if not os.path.exists(filename):
content = urllib.request.urlopen(download_url).read()
with open(filename, "wb") as fd:
fd.write(content)

return filename.split(".")[0]


def run(command, comment=None, env=None, service=None, tag=None, args=None, database_url_args=None):
if argv.list_targets:
if tag:
Expand All @@ -41,6 +73,13 @@ def run(command, comment=None, env=None, service=None, tag=None, args=None, data

environ = env or {}

if service == "sqlite":
if maybe_fetch_sqlite_extension() is not None:
if environ.get("RUSTFLAGS"):
environ["RUSTFLAGS"] += " --cfg sqlite_ipaddr"
else:
environ["RUSTFLAGS"] = "--cfg sqlite_ipaddr"

if service is not None:
database_url = start_database(service, database="sqlite/sqlite.db" if service == "sqlite" else "sqlx", cwd=dir_tests)

Expand Down

0 comments on commit 9d1b241

Please sign in to comment.