Skip to content

Commit

Permalink
wip refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien Coelho committed Sep 1, 2024
1 parent 670008e commit eae53eb
Show file tree
Hide file tree
Showing 21 changed files with 266 additions and 141 deletions.
23 changes: 22 additions & 1 deletion docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,32 @@ make docker.coverage

## Miscellaneous Commands

Run a client with access to host:

```sh
docker run -it -v .:/code --add-host=host.docker.internal:host-gateway some-image bash
```

Build an image:

```sh
docker run -it --add-host=host.docker.internal:host-gateway python bash
docker build -t aiosql-python-mysql -f dockerfile.python-mysql .
```

Run docker clients against manually started docker servers:

```sh
docker run -it -v .:/code --add-host=host.docker.internal:host-gateway \
python-aiosql-dbs \
make VENV=/venv MA_HOST=host.docker.internal check.pytest.mariadb.detached
docker run -it -v .:/code --add-host=host.docker.internal:host-gateway \
python-aiosql-mysql \
make VENV=/venv MY_HOST=host.docker.internal check.pytest.mysql.detached
docker run -it -v .:/code --add-host=host.docker.internal:host-gateway \
python-aiosql-dbs \
make VENV=/venv MS_HOST=host.docker.internal check.pytest.mssql.detached
```

## MS SQL Server

See [ubuntu image](https://hub.docker.com/r/microsoft/mssql-server) and its associated
Expand Down
10 changes: 9 additions & 1 deletion tests/blogdb/sql/blogs/blogs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
is expected
TO BE IGNORED SILENTLY…

-- name: drop-table-users#
DROP TABLE IF EXISTS users;

-- name: drop-table-blogs#
DROP TABLE IF EXISTS blogs;

-- name: drop-table-comments#
DROP TABLE IF EXISTS comments;

-- name: get-all-blogs
-- Fetch all fields for every blog in the database.
select * from blogs;
Expand All @@ -24,7 +33,6 @@ values (
-- Remove a blog from the database
delete from blogs where blogid = :blogid;


-- name: get-user-blogs
-- record_class: UserBlogSummary
-- Get blogs authored by a user.
Expand Down
19 changes: 19 additions & 0 deletions tests/blogdb/sql/blogs/du/blogs.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
-- name: create-table-users#
CREATE TABLE IF NOT EXISTS users(
userid INTEGER PRIMARY KEY,
username TEXT NOT NULL,
firstname TEXT NOT NULL,
lastname TEXT NOT NULL
);
CREATE SEQUENCE users_seq;

-- name: create-table-blogs#
CREATE TABLE IF NOT EXISTS blogs(
blogid INTEGER PRIMARY KEY,
userid INTEGER NOT NULL REFERENCES users,
title TEXT NOT NULL,
content TEXT NOT NULL,
published DATE NOT NULL DEFAULT (CURRENT_DATE)
);
CREATE SEQUENCE blogs_seq;

-- name: get-blogs-published-after
-- Get all blogs by all authors published after the given date.
select b.title,
Expand Down
17 changes: 17 additions & 0 deletions tests/blogdb/sql/blogs/li/blogs.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
-- name: create-table-users#
CREATE TABLE IF NOT EXISTS users(
userid INTEGER PRIMARY KEY,
username TEXT NOT NULL,
firstname TEXT NOT NULL,
lastname TEXT NOT NULL
);

-- name: create-table-blogs#
CREATE TABLE IF NOT EXISTS blogs(
blogid INTEGER PRIMARY KEY,
userid INTEGER NOT NULL REFERENCES users,
title TEXT NOT NULL,
content TEXT NOT NULL,
published DATE NOT NULL DEFAULT (CURRENT_DATE)
);

-- name: get-blogs-published-after
-- Get all blogs by all authors published after the given date.
select b.title,
Expand Down
29 changes: 29 additions & 0 deletions tests/blogdb/sql/blogs/ms/blogs.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
-- name: create-table-users#
CREATE TABLE users(
userid INTEGER IDENTITY(1, 1) PRIMARY KEY,
username VARCHAR(MAX) NOT NULL,
firstname VARCHAR(MAX) NOT NULL,
lastname VARCHAR(MAX) NOT NULL
);

-- name: create-table-blogs#
CREATE TABLE blogs(
blogid INTEGER IDENTITY(1, 1) PRIMARY KEY,
userid INTEGER NOT NULL REFERENCES users,
title VARCHAR(MAX) NOT NULL,
content VARCHAR(MAX) NOT NULL,
published DATE NOT NULL DEFAULT (GETDATE())
);

-- name: drop-table-comments#
IF OBJECT_ID('comments', 'U') IS NOT NULL
DROP TABLE comments;

-- name: drop-table-blogs#
IF OBJECT_ID('blogs', 'U') IS NOT NULL
DROP TABLE blogs;

-- name: drop-table-users#
IF OBJECT_ID('users', 'U') IS NOT NULL
DROP TABLE users;

-- name: get-blogs-published-after
-- Get all blogs by all authors published after the given date.
select title,
Expand Down
24 changes: 23 additions & 1 deletion tests/blogdb/sql/blogs/my/blogs.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
-- name: create-table-users#
CREATE TABLE IF NOT EXISTS users(
userid INTEGER auto_increment PRIMARY KEY,
username TEXT NOT NULL,
firstname TEXT NOT NULL,
lastname TEXT NOT NULL
);

-- name: create-table-blogs#
CREATE TABLE IF NOT EXISTS blogs(
blogid INTEGER auto_increment PRIMARY KEY,
userid INTEGER NOT NULL REFERENCES users,
title TEXT NOT NULL,
content TEXT NOT NULL,
published DATE NOT NULL DEFAULT (CURRENT_DATE)
);

-- name: get-blogs-published-after
-- Get all blogs by all authors published after the given date.
select b.title,
u.username,
DATE_FORMAT(b.published, '%Y-%m-%d %H:%M') as published
DATE_FORMAT(b.published, '%Y-%m-%d %H:%i') as published
from blogs b
inner join users u on b.userid = u.userid
where b.published >= :published
Expand All @@ -18,3 +35,8 @@ insert into blogs (
published
)
values (%s, %s, %s, %s);

-- name: publish-new-blog
insert into blogs (userid, title, content)
values (:userid, :title, :contents)
returning blogid, title;
17 changes: 17 additions & 0 deletions tests/blogdb/sql/blogs/pg/blogs.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
-- name: create-table-users#
CREATE TABLE IF NOT EXISTS users(
userid SERIAL PRIMARY KEY,
username TEXT NOT NULL,
firstname TEXT NOT NULL,
lastname TEXT NOT NULL
);

-- name: create-table-blogs#
CREATE TABLE IF NOT EXISTS blogs(
blogid SERIAL PRIMARY KEY,
userid INTEGER NOT NULL REFERENCES users,
title TEXT NOT NULL,
content TEXT NOT NULL,
published DATE NOT NULL DEFAULT CURRENT_DATE
);

-- name: get-blogs-published-after
-- Get all blogs by all authors published after the given date.
select title,
Expand Down
2 changes: 1 addition & 1 deletion tests/blogdb/sql/misc/misc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ SELECT :one,:two,:three AS comma;
SELECT 'L''art du rire' AS escaped;

-- name: person-attributes^
SELECT :p.name AS nom, :p.age AS age;
SELECT :p.name AS name, :p.age AS age;

-- FIXME this one does not work
-- name: my-escape-quotes$
Expand Down
7 changes: 1 addition & 6 deletions tests/blogdb/sql/misc/ms/misc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,4 @@ SELECT CONVERT(VARCHAR, CURRENT_TIMESTAMP, 120) AS now;
SELECT 'hello' AS message WHERE 0 = 1;

-- name: get-modulo$
-- %-escaped percent modulo operator
SELECT :numerator %% :denominator;

-- name: get-modulo-2$
-- no-escape modulo + cast
SELECT :numerator::INT8 % :denominator::INT8;
SELECT :numerator % :denominator AS modulo;
27 changes: 9 additions & 18 deletions tests/conf_duckdb.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import pytest
from pathlib import Path
from conf_schema import create_user_blogs, USERS_DATA_PATH, BLOGS_DATA_PATH
from conf_schema import USERS_DATA_PATH, BLOGS_DATA_PATH, create_user_blogs, drop_user_blogs

try:
import duckdb

@pytest.fixture
def duckdb_db_path(tmpdir):
db_path = str(Path(tmpdir.strpath) / "blogdb.duck.db")
return db_path

@pytest.fixture
def duckdb_conn(duckdb_db_path):
def duckdb_conn(queries):
# db_path = str(Path(tmpdir.strpath) / "blogdb.duck.db")
conn = duckdb.connect(":memory:")
populate_duckdb_db(conn)
yield conn
conn.close()

def populate_duckdb_db(conn):
conn.execute("\n".join(create_user_blogs("duckdb")))
create_user_blogs(conn, queries)
conn.execute(
"insert into users(userid, username, firstname, lastname)\n"
"select nextval('users_seq') as userid, column0 as username,\n"
Expand All @@ -31,13 +22,13 @@ def populate_duckdb_db(conn):
" column2 as content, column3 as published\n"
f"from read_csv_auto('{str(BLOGS_DATA_PATH)}', header=False)"
)
conn.commit()
yield conn
drop_user_blogs(conn, queries)
conn.close()

except ModuleNotFoundError:

@pytest.fixture
def duckdb_db_path(tmpdir):
raise Exception("undefined fixture")

@pytest.fixture
def duckdb_conn(duckdb_db_path):
def duckdb_conn():
raise Exception("undefined fixture")
31 changes: 16 additions & 15 deletions tests/conf_mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ def ms_has_db(conn, database):

try:

@pytest.fixture
# NOTE rhetorical, there is only one ms driver for now
@pytest.fixture(scope="module")
def ms_driver(request):
"""Return driver class."""
driver = request.config.getoption("mssql_driver") or "pymssql"
db = importlib.import_module(driver)
return db

@pytest.fixture
@pytest.fixture(scope="module")
def ms_dsn(request):
"""Return connection parameters suitable to pymssql driver."""
yield {
Expand All @@ -29,9 +30,10 @@ def ms_dsn(request):
"password": request.config.getoption("mssql_password"),
"database": request.config.getoption("mssql_database") or "pytest",
"as_dict": True,
"autocommit": False,
}

@pytest.fixture
@pytest.fixture(scope="module")
def ms_master(ms_dsn):
"""Return connection parameters suitable for "system admin" access."""
dsn = dict(ms_dsn)
Expand All @@ -47,30 +49,29 @@ def ms_conn(request, ms_dsn, ms_driver):
with u.db_connect(ms_driver, tries, **ms_dsn) as conn:
yield conn

@pytest.fixture
def ms_db(ms_driver, ms_dsn, ms_master):
@pytest.fixture(scope="module")
def ms_db(ms_driver, ms_dsn, ms_master, queries):
"""Build the test database and return a connection to that."""
with ms_driver.connect(**ms_master) as conn:
# initial contents if needed
if not ms_has_db(conn, "pytest"):
with conn.cursor() as cur:
cur.execute("CREATE DATABASE pytest")
cur.execute("USE pytest")
for ct in create_user_blogs("mssql"):
cur.execute(ct)
# conn.commit()
fill_user_blogs(cur, "mssql")
conn.commit()
create_user_blogs(conn, queries)
fill_user_blogs(conn, "mssql")
else:
u.log.warning("skipping pytest schema creation")
# connection to pytest possibly database created above
with ms_driver.connect(**ms_dsn, autocommit=False) as conn:
with ms_driver.connect(**ms_dsn) as conn:
yield conn
# cleanup:
# with ms_driver.connect(**master, autocommit=True) as conn:
# with conn.cursor() as cur:
# u.log.warning("cleaning up pytest schema")
# cur.execute("DROP DATABASE pytest")
# cleanup
with ms_driver.connect(**ms_master) as conn:
with conn.cursor() as cur:
# u.log.warning("cleaning up pytest schema")
cur.execute("DROP DATABASE pytest")
conn.commit()

except ModuleNotFoundError:
# provide empty fixtures to please pytest "parsing"
Expand Down
34 changes: 16 additions & 18 deletions tests/conf_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,20 @@ def my_dsn(request):
yield dsn

@pytest.fixture
def my_conn(request, my_dsn):
"""Return a connection using the expected driver."""
def my_driver(request):
driver = request.config.getoption("mysql_driver")
tries = request.config.getoption("mysql_tries")
db = importlib.import_module(driver)
return db

@pytest.fixture
def my_conn(request, my_dsn, my_driver):
"""Return a connection using the expected driver."""
tries = request.config.getoption("mysql_tries")
fails = 0
while tries > 0:
tries -= 1
try:
with db.connect(**my_dsn) as conn:
with my_driver.connect(**my_dsn) as conn:
tries = 0
yield conn
except Exception as e:
Expand All @@ -60,22 +64,12 @@ def my_conn(request, my_dsn):
time.sleep(1.0)

@pytest.fixture
def my_db(my_conn):
def my_db(my_conn, queries):
"""Build the test database."""
# initial contents
with my_conn.cursor() as cur:
for ct in create_user_blogs("mysql"):
cur.execute(ct)
my_conn.commit()
fill_user_blogs(cur, "mysql")
my_conn.commit()
# connection to use
create_user_blogs(my_conn, queries)
fill_user_blogs(my_conn, "mysql")
yield my_conn
# cleanup
with my_conn.cursor() as cur:
for dt in drop_user_blogs("mysql"):
cur.execute(dt)
my_conn.commit()
drop_user_blogs(my_conn, queries)

except ModuleNotFoundError:
# provide empty fixtures to please pytest "parsing"
Expand All @@ -84,6 +78,10 @@ def my_db(my_conn):
def my_dsn():
raise Exception("undefined fixture")

@pytest.fixture
def my_driver():
raise Exception("undefined fixture")

@pytest.fixture
def my_conn():
raise Exception("undefined fixture")
Expand Down
Loading

0 comments on commit eae53eb

Please sign in to comment.