simple-module-test 0.0.48__tar.gz → 0.0.49__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/PKG-INFO +5 -5
  2. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/pyproject.toml +5 -5
  3. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/database.py +11 -7
  4. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/layout.py +1 -0
  5. simple_module_test-0.0.49/tests/test_schema_reuse_pg.py +185 -0
  6. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/tests/test_testing_module_layout.py +1 -0
  7. simple_module_test-0.0.48/tests/test_schema_reuse_pg.py +0 -75
  8. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/.gitignore +0 -0
  9. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/LICENSE +0 -0
  10. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/README.md +0 -0
  11. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/__init__.py +0 -0
  12. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/_compile_cache.py +0 -0
  13. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/_dotenv.py +0 -0
  14. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/_schema.py +0 -0
  15. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/_sqlite_template.py +0 -0
  16. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/app_factory.py +0 -0
  17. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/fake_events.py +0 -0
  18. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/fixtures.py +0 -0
  19. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/inertia.py +0 -0
  20. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/inertia_page.py +0 -0
  21. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/mail.py +0 -0
  22. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/oidc_issuer.py +0 -0
  23. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/plugin.py +0 -0
  24. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/process_state.py +0 -0
  25. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/redis_server.py +0 -0
  26. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/routes.py +0 -0
  27. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/session_cookie.py +0 -0
  28. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/setup_wizard.py +0 -0
  29. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/tenant_client.py +0 -0
  30. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/simple_module_test/waiting.py +0 -0
  31. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/tests/test_app_factory.py +0 -0
  32. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/tests/test_fake_events.py +0 -0
  33. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/tests/test_plugin_fixtures.py +0 -0
  34. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/tests/test_shared_compile_cache.py +0 -0
  35. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/tests/test_sqlite_schema_template.py +0 -0
  36. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/tests/test_testing_inertia_headers.py +0 -0
  37. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/tests/test_testing_inertia_page.py +0 -0
  38. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/tests/test_testing_oidc_issuer.py +0 -0
  39. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/tests/test_testing_process_state.py +0 -0
  40. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/tests/test_testing_project_isolation.py +0 -0
  41. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/tests/test_waiting_helpers.py +0 -0
  42. {simple_module_test-0.0.48 → simple_module_test-0.0.49}/tests/test_worker_database.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: simple_module_test
3
- Version: 0.0.48
3
+ Version: 0.0.49
4
4
  Summary: Shared pytest fixtures (app, client, db_session, authenticated_client) for writing simple_module module tests
5
5
  Project-URL: Homepage, https://github.com/antosubash/simple_module_python
6
6
  Project-URL: Repository, https://github.com/antosubash/simple_module_python
@@ -28,10 +28,10 @@ Requires-Dist: pydantic-settings<3,>=2.4
28
28
  Requires-Dist: pyjwt[crypto]<3,>=2.8
29
29
  Requires-Dist: pytest-asyncio<2,>=0.24
30
30
  Requires-Dist: pytest<10,>=8.0
31
- Requires-Dist: simple-module-core==0.0.48
32
- Requires-Dist: simple-module-db==0.0.48
33
- Requires-Dist: simple-module-hosting==0.0.48
34
- Requires-Dist: simple-module-inertia==0.0.48
31
+ Requires-Dist: simple-module-core==0.0.49
32
+ Requires-Dist: simple-module-db==0.0.49
33
+ Requires-Dist: simple-module-hosting==0.0.49
34
+ Requires-Dist: simple-module-inertia==0.0.49
35
35
  Requires-Dist: sqlalchemy<3,>=2.0
36
36
  Description-Content-Type: text/markdown
37
37
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "simple_module_test"
3
- version = "0.0.48"
3
+ version = "0.0.49"
4
4
  description = "Shared pytest fixtures (app, client, db_session, authenticated_client) for writing simple_module module tests"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -30,10 +30,10 @@ dependencies = [
30
30
  "pydantic-settings>=2.4,<3",
31
31
  "pytest>=8.0,<10",
32
32
  "pytest-asyncio>=0.24,<2",
33
- "simple_module_core==0.0.48",
34
- "simple_module_db==0.0.48",
35
- "simple_module_hosting==0.0.48",
36
- "simple_module_inertia==0.0.48",
33
+ "simple_module_core==0.0.49",
34
+ "simple_module_db==0.0.49",
35
+ "simple_module_hosting==0.0.49",
36
+ "simple_module_inertia==0.0.49",
37
37
  "sqlalchemy>=2.0,<3",
38
38
  ]
39
39
 
@@ -152,8 +152,8 @@ async def prepare_schema_once(engine: Any, tables: list[Any], build: Any) -> Non
152
152
  per-process copy of the first build (``_sqlite_template``), a file one runs
153
153
  ``build(engine)``.
154
154
  Postgres: when this process already built the schema and the catalog still
155
- matches it, delete every row and restart the sequences (~0.03 s) instead of
156
- dropping and rebuilding it (~0.25 s). Anything else (the first test, a
155
+ matches it, empty the tables with one TRUNCATE and restart the sequences
156
+ instead of dropping and rebuilding it. Anything else (the first test, a
157
157
  direct ``reset_schema`` call, a test's own DDL) resets and rebuilds.
158
158
  """
159
159
  global _reset_for, _built_schema
@@ -181,13 +181,17 @@ async def _fingerprint(engine: Any) -> str:
181
181
 
182
182
 
183
183
  async def _empty_tables(engine: Any, tables: list[Any]) -> None:
184
- from sqlalchemy.schema import sort_tables
184
+ """Empty a reused Postgres schema; SQLite is handled before this call."""
185
185
 
186
186
  async with engine.begin() as conn:
187
- quote = conn.dialect.identifier_preparer.format_table
188
- # Children first, so no foreign key refuses a parent's delete.
189
- for table in reversed(sort_tables(tables)):
190
- await conn.execute(text(f"DELETE FROM {quote(table)}"))
187
+ # A leaked reader must fail setup instead of waiting forever for TRUNCATE.
188
+ await conn.execute(text("SET LOCAL lock_timeout = '5s'"))
189
+ if tables:
190
+ quote = conn.dialect.identifier_preparer.format_table
191
+ names = ", ".join(quote(table) for table in tables)
192
+ # Refuse foreign-key dependants outside the caller's reset boundary.
193
+ await conn.execute(text(f"TRUNCATE {names} RESTART IDENTITY RESTRICT"))
194
+ # RESTART IDENTITY excludes unowned sequences; preserve their reset too.
191
195
  await conn.execute(_RESTART_SEQUENCES)
192
196
 
193
197
 
@@ -9,6 +9,7 @@ from pathlib import Path
9
9
  ALLOWED_ROOT = frozenset(
10
10
  {
11
11
  "__init__.py",
12
+ "__main__.py",
12
13
  "py.typed",
13
14
  "module.py",
14
15
  "models.py",
@@ -0,0 +1,185 @@
1
+ """Postgres reuses the schema between tests, emptied of rows (``prepare_schema_once``)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+
7
+ import pytest
8
+ from simple_module_test import database
9
+ from sqlalchemy import (
10
+ Column,
11
+ ForeignKey,
12
+ Identity,
13
+ Integer,
14
+ MetaData,
15
+ String,
16
+ Table,
17
+ func,
18
+ select,
19
+ text,
20
+ )
21
+ from sqlalchemy.exc import DBAPIError
22
+ from sqlalchemy.ext.asyncio import create_async_engine
23
+
24
+ pytestmark = pytest.mark.postgres
25
+
26
+ _metadata = MetaData()
27
+ _item = Table(
28
+ "reuse_probe_item",
29
+ _metadata,
30
+ Column("id", Integer, Identity(), primary_key=True),
31
+ Column("name", String(20)),
32
+ )
33
+
34
+
35
+ @pytest.fixture
36
+ async def engine(monkeypatch):
37
+ # The probe drives the per-test state itself; put it back afterwards.
38
+ for attr in ("_current_test", "_reset_for", "_built_schema"):
39
+ monkeypatch.setattr(database, attr, getattr(database, attr))
40
+ url = database.database_url_for_tests()
41
+ engine = create_async_engine(url, **database.init_db_kwargs(url))
42
+ try:
43
+ yield engine
44
+ finally:
45
+ await database.reset_schema(engine) # the next test rebuilds the real schema
46
+ await engine.dispose()
47
+
48
+
49
+ async def _next_test(engine, builds: list[int]) -> None:
50
+ async def build(engine) -> None:
51
+ builds.append(1)
52
+ async with engine.begin() as conn:
53
+ await conn.run_sync(_metadata.create_all)
54
+
55
+ database.begin_test(object())
56
+ await database.prepare_schema_once(engine, [_item], build)
57
+
58
+
59
+ async def _insert(engine) -> int:
60
+ async with engine.begin() as conn:
61
+ return (
62
+ await conn.execute(_item.insert().values(name="x").returning(_item.c.id))
63
+ ).scalar_one()
64
+
65
+
66
+ async def test_a_later_test_gets_the_same_schema_without_rows(engine):
67
+ builds: list[int] = []
68
+ await _next_test(engine, builds)
69
+ assert [await _insert(engine), await _insert(engine)] == [1, 2]
70
+
71
+ await _next_test(engine, builds)
72
+
73
+ assert builds == [1]
74
+ async with engine.connect() as conn:
75
+ assert await conn.scalar(select(func.count()).select_from(_item)) == 0
76
+ assert await _insert(engine) == 1 # sequences restart too
77
+
78
+
79
+ async def test_a_test_that_changes_the_schema_forces_a_rebuild(engine):
80
+ builds: list[int] = []
81
+ await _next_test(engine, builds)
82
+ async with engine.begin() as conn:
83
+ await conn.execute(text("CREATE TABLE reuse_probe_extra (x int)"))
84
+
85
+ await _next_test(engine, builds)
86
+
87
+ assert builds == [1, 1]
88
+ async with engine.connect() as conn:
89
+ assert await conn.scalar(text("SELECT to_regclass('reuse_probe_extra')")) is None
90
+
91
+
92
+ async def test_reset_bypasses_delete_row_triggers_and_restarts_identity(engine):
93
+ await _next_test(engine, [])
94
+ child = Table(
95
+ "reset probe child",
96
+ MetaData(),
97
+ Column("item_id", Integer, ForeignKey(_item.c.id), primary_key=True),
98
+ schema="public",
99
+ )
100
+ async with engine.begin() as conn:
101
+ await conn.run_sync(child.create)
102
+ await conn.execute(
103
+ text("""
104
+ CREATE FUNCTION refuse_probe_delete() RETURNS trigger
105
+ LANGUAGE plpgsql AS $$
106
+ BEGIN
107
+ RAISE EXCEPTION 'probe rows are immutable';
108
+ END;
109
+ $$
110
+ """)
111
+ )
112
+ await conn.execute(
113
+ text("""
114
+ CREATE TRIGGER refuse_probe_delete BEFORE DELETE ON reuse_probe_item
115
+ FOR EACH ROW EXECUTE FUNCTION refuse_probe_delete()
116
+ """)
117
+ )
118
+ assert [await _insert(engine), await _insert(engine)] == [1, 2]
119
+ async with engine.begin() as conn:
120
+ await conn.execute(child.insert().values(item_id=1))
121
+
122
+ # A quoted, schema-qualified child and parent-first input need no DELETE ordering.
123
+ await database._empty_tables(engine, [_item, child])
124
+
125
+ async with engine.connect() as conn:
126
+ assert await conn.scalar(select(func.count()).select_from(_item)) == 0
127
+ assert await conn.scalar(select(func.count()).select_from(child)) == 0
128
+ assert await _insert(engine) == 1
129
+
130
+
131
+ async def test_reset_restarts_sequences_without_a_table_owner(engine):
132
+ await _next_test(engine, [])
133
+ async with engine.begin() as conn:
134
+ await conn.execute(text("CREATE SEQUENCE reuse_probe_unowned"))
135
+ assert await conn.scalar(text("SELECT nextval('reuse_probe_unowned')")) == 1
136
+ assert await conn.scalar(text("SELECT nextval('reuse_probe_unowned')")) == 2
137
+
138
+ await database._empty_tables(engine, [_item])
139
+
140
+ async with engine.connect() as conn:
141
+ assert await conn.scalar(text("SELECT nextval('reuse_probe_unowned')")) == 1
142
+
143
+
144
+ async def test_reset_refuses_to_empty_tables_outside_the_passed_list(engine):
145
+ await _next_test(engine, [])
146
+ outside = Table(
147
+ "outside_probe",
148
+ MetaData(),
149
+ Column("id", Integer, primary_key=True),
150
+ Column("item_id", Integer, ForeignKey(_item.c.id)),
151
+ )
152
+ async with engine.begin() as conn:
153
+ await conn.run_sync(outside.create)
154
+ # DELETE on the empty parent would leave this unrelated row untouched.
155
+ await conn.execute(outside.insert().values(id=1, item_id=None))
156
+
157
+ with pytest.raises(DBAPIError, match="foreign key"):
158
+ await database._empty_tables(engine, [_item])
159
+
160
+ async with engine.connect() as conn:
161
+ assert await conn.scalar(select(outside.c.id)) == 1
162
+
163
+
164
+ async def test_reset_times_out_when_a_previous_reader_holds_a_lock(engine):
165
+ await _next_test(engine, [])
166
+ async with engine.connect() as reader:
167
+ await reader.execute(select(_item))
168
+ try:
169
+ with pytest.raises(DBAPIError, match="lock timeout"):
170
+ await asyncio.wait_for(database._empty_tables(engine, [_item]), timeout=10)
171
+ finally:
172
+ await reader.rollback()
173
+
174
+
175
+ async def test_reset_with_no_tables_preserves_the_migration_stamp(engine):
176
+ await _next_test(engine, [])
177
+ async with engine.begin() as conn:
178
+ await conn.execute(text("CREATE TABLE alembic_version (version_num text)"))
179
+ await conn.execute(text("INSERT INTO alembic_version VALUES ('probe')"))
180
+
181
+ await database._empty_tables(engine, [])
182
+ await database._empty_tables(engine, [_item])
183
+
184
+ async with engine.connect() as conn:
185
+ assert await conn.scalar(text("SELECT version_num FROM alembic_version")) == "probe"
@@ -8,6 +8,7 @@ def test_public_layout_helper_allows_duplicate_test_basenames(tmp_path):
8
8
 
9
9
  package = tmp_path / "orders"
10
10
  (package / "core").mkdir(parents=True)
11
+ (package / "__main__.py").touch()
11
12
  for area in ("core", "web"):
12
13
  tests = tmp_path / "tests" / area / "crud"
13
14
  tests.mkdir(parents=True)
@@ -1,75 +0,0 @@
1
- """Postgres reuses the schema between tests, emptied of rows (``prepare_schema_once``)."""
2
-
3
- from __future__ import annotations
4
-
5
- import pytest
6
- from simple_module_test import database
7
- from sqlalchemy import Column, Integer, MetaData, String, Table, func, select, text
8
- from sqlalchemy.ext.asyncio import create_async_engine
9
-
10
- pytestmark = pytest.mark.postgres
11
-
12
- _metadata = MetaData()
13
- _item = Table(
14
- "reuse_probe_item",
15
- _metadata,
16
- Column("id", Integer, primary_key=True),
17
- Column("name", String(20)),
18
- )
19
-
20
-
21
- @pytest.fixture
22
- async def engine(monkeypatch):
23
- # The probe drives the per-test state itself; put it back afterwards.
24
- for attr in ("_current_test", "_reset_for", "_built_schema"):
25
- monkeypatch.setattr(database, attr, getattr(database, attr))
26
- url = database.database_url_for_tests()
27
- engine = create_async_engine(url, **database.init_db_kwargs(url))
28
- try:
29
- yield engine
30
- finally:
31
- await database.reset_schema(engine) # the next test rebuilds the real schema
32
- await engine.dispose()
33
-
34
-
35
- async def _next_test(engine, builds: list[int]) -> None:
36
- async def build(engine) -> None:
37
- builds.append(1)
38
- async with engine.begin() as conn:
39
- await conn.run_sync(_metadata.create_all)
40
-
41
- database.begin_test(object())
42
- await database.prepare_schema_once(engine, [_item], build)
43
-
44
-
45
- async def _insert(engine) -> int:
46
- async with engine.begin() as conn:
47
- return (
48
- await conn.execute(_item.insert().values(name="x").returning(_item.c.id))
49
- ).scalar_one()
50
-
51
-
52
- async def test_a_later_test_gets_the_same_schema_without_rows(engine):
53
- builds: list[int] = []
54
- await _next_test(engine, builds)
55
- assert [await _insert(engine), await _insert(engine)] == [1, 2]
56
-
57
- await _next_test(engine, builds)
58
-
59
- assert builds == [1]
60
- async with engine.connect() as conn:
61
- assert await conn.scalar(select(func.count()).select_from(_item)) == 0
62
- assert await _insert(engine) == 1 # sequences restart too
63
-
64
-
65
- async def test_a_test_that_changes_the_schema_forces_a_rebuild(engine):
66
- builds: list[int] = []
67
- await _next_test(engine, builds)
68
- async with engine.begin() as conn:
69
- await conn.execute(text("CREATE TABLE reuse_probe_extra (x int)"))
70
-
71
- await _next_test(engine, builds)
72
-
73
- assert builds == [1, 1]
74
- async with engine.connect() as conn:
75
- assert await conn.scalar(text("SELECT to_regclass('reuse_probe_extra')")) is None