Skip to content

Commit 1511505

Browse files
committed
100% line coverage
1 parent 8a2c202 commit 1511505

File tree

9 files changed

+150
-5
lines changed

9 files changed

+150
-5
lines changed

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ build-backend = "setuptools.build_meta"
3333

3434
[tool.coverage.run]
3535
source = ["pytest_sqlalchemy", "tests"]
36+
parallel = true
3637

3738
[tool.pytest.ini_options]
3839
norecursedirs = ["tests/examples"]
40+
41+
[dependency-groups]
42+
dev = [
43+
"pytest-xdist>=3.6.1",
44+
]

tests/conftest.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import os
2+
from pathlib import Path
23

34
import pytest
45

56
pytest_plugins = ["pytester"]
67

78

8-
@pytest.fixture(scope="session")
9-
def db_url() -> str:
10-
return os.environ.get("DB_URL", "sqlite:///:memory:")
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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
from sqlalchemy.engine import Connection
2+
3+
14
def test_connection(connection):
2-
assert 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

tests/test_plugin.py

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,111 @@
1+
import os
2+
from pathlib import Path
3+
from textwrap import dedent
4+
5+
from pytest import FixtureRequest
16
from _pytest.pytester import Pytester
7+
from sqlalchemy_utils import database_exists, create_database
28

39

4-
def test_plugin(pytester: Pytester, db_url: str) -> None:
10+
def test_minimal(pytester: Pytester, db_url: str) -> None:
511
pytester.copy_example("tests/examples/test_minimal.py")
612
result = pytester.runpytest('--sqlalchemy-connect-url', db_url)
713
result.assert_outcomes(passed=1)
814

15+
16+
def test_transaction_fixture(pytester: Pytester, db_url: str) -> None:
17+
pytester.copy_example("tests/examples/test_transaction.py")
18+
result = pytester.runpytest('--sqlalchemy-connect-url', db_url)
19+
result.assert_outcomes(passed=1)
20+
21+
22+
def test_dbsession_fixture(pytester: Pytester, db_url: str) -> None:
23+
pytester.copy_example("tests/examples/test_dbsession.py")
24+
result = pytester.runpytest('--sqlalchemy-connect-url', db_url)
25+
result.assert_outcomes(passed=1)
26+
27+
28+
def test_db_schema(pytester: Pytester, new_db_url: str) -> None:
29+
assert not database_exists(new_db_url)
30+
pytester.copy_example("tests/examples/test_db_schema.py")
31+
result = pytester.runpytest(
32+
'--sqlalchemy-connect-url', new_db_url, '--sqlalchemy-manage-db'
33+
)
34+
result.assert_outcomes(passed=1)
35+
assert not database_exists(new_db_url)
36+
37+
38+
def test_db_exists_no_keep_db(pytester: Pytester, db_url: str) -> None:
39+
if not database_exists(db_url):
40+
create_database(db_url)
41+
pytester.copy_example("tests/examples/test_db_schema.py")
42+
result = pytester.runpytest(
43+
'--sqlalchemy-connect-url', db_url, '--sqlalchemy-manage-db'
44+
)
45+
result.assert_outcomes(errors=1)
46+
assert "DB exists, remove it before proceeding" in result.stdout.str()
47+
assert database_exists(db_url)
48+
49+
50+
def test_db_exists_keep_db(pytester: Pytester, db_url: str) -> None:
51+
if not database_exists(db_url):
52+
create_database(db_url)
53+
pytester.copy_example("tests/examples/test_db_schema.py")
54+
result = pytester.runpytest(
55+
'--sqlalchemy-connect-url',
56+
db_url,
57+
'--sqlalchemy-manage-db',
58+
'--sqlalchemy-keep-db',
59+
)
60+
result.assert_outcomes(passed=1)
61+
assert database_exists(db_url)
62+
63+
64+
def test_connect_uri_alias(pytester: Pytester, db_url: str) -> None:
65+
pytester.copy_example("tests/examples/test_connect_uri.py")
66+
result = pytester.runpytest('--sqlalchemy-connect-url', db_url)
67+
result.assert_outcomes(passed=1)
68+
69+
70+
def test_engine_with_config_file(pytester: Pytester, tmp_path: Path) -> None:
71+
config_path = tmp_path / "config.ini"
72+
config_path.write_text(
73+
dedent(
74+
"""
75+
[DEFAULT]
76+
sqlalchemy.url = sqlite:///:memory:
77+
"""
78+
)
79+
)
80+
81+
pytester.copy_example("tests/examples/test_minimal.py")
82+
result = pytester.runpytest('--sqlalchemy-config-file', config_path)
83+
result.assert_outcomes(passed=1)
84+
85+
86+
def test_engine_no_config_source(pytester: Pytester) -> None:
87+
pytester.copy_example("tests/examples/test_minimal.py")
88+
result = pytester.runpytest()
89+
result.assert_outcomes(errors=1)
90+
assert "Can not establish a connection to the database" in result.stdout.str()
91+
92+
93+
def test_xdist_naming(pytester: Pytester, db_url: str, request: FixtureRequest) -> None:
94+
running_under_coverage = bool(os.environ.get("COVERAGE_RUN"))
95+
# Measuring coverage when running under xdist is pretty tricky:
96+
if running_under_coverage:
97+
pyproject_toml = Path(__file__).parent.parent / "pyproject.toml"
98+
pytester.makeconftest(dedent(f"""
99+
import coverage, os, sys
100+
def pytest_configure(config):
101+
worker_id = os.environ.get("PYTEST_XDIST_WORKER")
102+
if worker_id is not None:
103+
os.environ["COVERAGE_PROCESS_START"] = "{pyproject_toml}"
104+
coverage.process_startup()
105+
"""))
106+
pytester.copy_example("tests/examples/test_xdist.py")
107+
result = pytester.runpytest('-n', '2', '--sqlalchemy-connect-url', db_url)
108+
result.assert_outcomes(passed=2)
109+
if running_under_coverage:
110+
for item in pytester.path.glob('.coverage.*'):
111+
item.rename(request.config.rootdir / item.name)

0 commit comments

Comments
 (0)