Skip to content

Commit 0a546ab

Browse files
authored
Merge pull request #14 from toirl/test-coverage
100% test coverage
2 parents ee68e69 + 5aad39b commit 0a546ab

File tree

12 files changed

+203
-18
lines changed

12 files changed

+203
-18
lines changed

.github/workflows/ci.yml

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,32 @@ jobs:
8787
cache-dependency-glob: "**/pyproject.toml"
8888
cache-suffix: ${{ matrix.uv-resolution }}
8989

90-
- name: Run tests with SQLite
91-
if: matrix.database == 'sqlite'
92-
run: |
93-
uv run --all-extras --dev --resolution ${{ matrix.uv-resolution }} -m pytest --sqlalchemy-connect-url="sqlite:///foo.sqlite"
90+
- name: Install the project and dependencies
91+
run: uv sync --all-extras --dev --resolution ${{ matrix.uv-resolution }}
9492

95-
- name: Run tests with PostgreSQL
96-
if: matrix.database == 'postgres'
93+
- name: Run tests
9794
run: |
98-
uv run --all-extras --dev --with psycopg2 --resolution ${{ matrix.uv-resolution }} -m pytest --sqlalchemy-connect-url="postgresql://postgres:postgres@localhost:5432/test_db"
95+
# Install software and run tests
96+
if [ "${{ matrix.database }}" = "postgres" ]; then
97+
export WITH='--with psycopg2-binary'
98+
export DB_URL=postgresql://postgres:postgres@localhost:5432/test_db
99+
elif [ "${{ matrix.database }}" = "mysql" ]; then
100+
export WITH='--with mysqlclient'
101+
# 127.0.0.1 is to persuade mysqlclient to use tcp rather than the domain socket :-/
102+
export DB_URL=mysql://root:mysql@127.0.0.1:3306/test_db
103+
fi
104+
uv run --no-sync $WITH --resolution ${{ matrix.uv-resolution }} -m coverage run -m pytest --sqlalchemy-connect-url=$DB_URL
99105
100-
- name: Run tests with MySQL
101-
if: matrix.database == 'mysql'
102-
# 127.0.0.1 is to persuade mysqlclient to use tcp rather than the domain socket :-/
103-
run: |
104-
uv run --all-extras --dev --with mysqlclient --resolution ${{ matrix.uv-resolution }} -m pytest --sqlalchemy-connect-url="mysql://root:mysql@127.0.0.1:3306/test_db"
106+
- name: Upload coverage data
107+
uses: actions/upload-artifact@v4
108+
with:
109+
name: coverage-data-${{ matrix.python-version }}-${{ matrix.uv-resolution }}-${{ matrix.database }}
110+
path: .coverage.*
111+
include-hidden-files: true
112+
if-no-files-found: ignore
113+
114+
coverage:
115+
needs: tests
116+
runs-on: ubuntu-latest
117+
steps:
118+
- uses: cjw296/python-action/check-coverage@v1

pyproject.toml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ classifiers = [
1515
"Operating System :: OS Independent",
1616
]
1717
dependencies = [
18+
"coverage>=7.8.0",
1819
"pytest>=8.0",
1920
"sqlalchemy>=1.4",
20-
"SQLAlchemy-Utils>=0.41"
21+
"SQLAlchemy-Utils>=0.41",
2122
]
2223

2324
[project.urls]
@@ -29,3 +30,15 @@ sqlalchemy = "pytest_sqlalchemy"
2930
[build-system]
3031
requires = ["setuptools>=61.0"]
3132
build-backend = "setuptools.build_meta"
33+
34+
[tool.coverage.run]
35+
source = ["pytest_sqlalchemy", "tests"]
36+
parallel = true
37+
38+
[tool.pytest.ini_options]
39+
norecursedirs = ["tests/examples"]
40+
41+
[dependency-groups]
42+
dev = [
43+
"pytest-xdist>=3.6.1",
44+
]

pytest_sqlalchemy.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ def engine(request, sqlalchemy_connect_url, app_config):
2828
raise RuntimeError("Can not establish a connection to the database")
2929

3030
# Put a suffix like _gw0, _gw1 etc on xdist processes
31-
xdist_suffix = getattr(request.config, 'slaveinput', {}).get('slaveid')
31+
xdist_suffix = getattr(request.config, 'workerinput', {}).get('workerid')
32+
print(xdist_suffix)
3233
if engine.url.database != ':memory:' and xdist_suffix is not None:
33-
engine.url.database = '{}_{}'.format(engine.url.database, xdist_suffix)
34-
engine = create_engine(engine.url) # override engine
34+
url = engine.url.set(database=f'{engine.url.database}_{xdist_suffix}')
35+
engine = create_engine(url) # override engine
3536

3637
def fin():
3738
print ("Disposing engine")

tests/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
pytest_plugins = ["pytester"]
7+
8+
9+
@pytest.fixture()
10+
def db_url(tmp_path: Path) -> str:
11+
return os.environ.get("DB_URL", "sqlite:///" + str(tmp_path / "test.db"))
12+
13+
14+
@pytest.fixture()
15+
def new_db_url(db_url: str) -> str:
16+
"""A db_url pointing at a database that does not exist"""
17+
return db_url + '_new'

tests/examples/test_connect_uri.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def test_uri(connect_uri):
2+
assert isinstance(connect_uri, str)

tests/examples/test_db_schema.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from sqlalchemy.engine import Connection
2+
from sqlalchemy_utils import database_exists
3+
4+
5+
def test_db_schema(db_schema, engine):
6+
assert db_schema is None # Not very helpful?
7+
assert database_exists(engine.url)

tests/examples/test_dbsession.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from sqlalchemy.orm import Session
2+
3+
4+
def test_session(dbsession):
5+
assert isinstance(dbsession, Session)

tests/examples/test_minimal.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from sqlalchemy.engine import Connection
2+
3+
4+
def test_connection(connection):
5+
assert isinstance(connection, Connection)
6+
assert not connection.in_transaction()

tests/examples/test_transaction.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from sqlalchemy.engine import Connection
2+
3+
4+
def test_transaction(transaction):
5+
assert isinstance(transaction, Connection)
6+
assert transaction.in_transaction()

tests/examples/test_xdist.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def test_one(engine):
2+
assert '_gw' in engine.url.database
3+
4+
def test_two(engine):
5+
assert '_gw' in engine.url.database

0 commit comments

Comments
 (0)