simple-module-test 0.0.34__tar.gz → 0.0.35__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 (18) hide show
  1. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/PKG-INFO +4 -4
  2. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/pyproject.toml +4 -4
  3. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/simple_module_test/__init__.py +2 -0
  4. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/simple_module_test/_schema.py +3 -0
  5. simple_module_test-0.0.35/simple_module_test/database.py +78 -0
  6. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/simple_module_test/fixtures.py +14 -2
  7. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/simple_module_test/plugin.py +13 -0
  8. simple_module_test-0.0.35/simple_module_test/redis_server.py +145 -0
  9. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/.gitignore +0 -0
  10. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/LICENSE +0 -0
  11. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/README.md +0 -0
  12. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/simple_module_test/app_factory.py +0 -0
  13. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/simple_module_test/fake_events.py +0 -0
  14. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/simple_module_test/routes.py +0 -0
  15. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/simple_module_test/session_cookie.py +0 -0
  16. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/tests/test_app_factory.py +0 -0
  17. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/tests/test_fake_events.py +0 -0
  18. {simple_module_test-0.0.34 → simple_module_test-0.0.35}/tests/test_plugin_fixtures.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: simple_module_test
3
- Version: 0.0.34
3
+ Version: 0.0.35
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
@@ -25,9 +25,9 @@ Requires-Dist: fastapi>=0.115
25
25
  Requires-Dist: httpx>=0.27
26
26
  Requires-Dist: pytest-asyncio>=0.24
27
27
  Requires-Dist: pytest>=8.0
28
- Requires-Dist: simple-module-core==0.0.34
29
- Requires-Dist: simple-module-db==0.0.34
30
- Requires-Dist: simple-module-hosting==0.0.34
28
+ Requires-Dist: simple-module-core==0.0.35
29
+ Requires-Dist: simple-module-db==0.0.35
30
+ Requires-Dist: simple-module-hosting==0.0.35
31
31
  Requires-Dist: sqlalchemy>=2.0
32
32
  Description-Content-Type: text/markdown
33
33
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "simple_module_test"
3
- version = "0.0.34"
3
+ version = "0.0.35"
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"
@@ -25,9 +25,9 @@ dependencies = [
25
25
  "httpx>=0.27",
26
26
  "pytest>=8.0",
27
27
  "pytest-asyncio>=0.24",
28
- "simple_module_core==0.0.34",
29
- "simple_module_db==0.0.34",
30
- "simple_module_hosting==0.0.34",
28
+ "simple_module_core==0.0.35",
29
+ "simple_module_db==0.0.35",
30
+ "simple_module_hosting==0.0.35",
31
31
  "sqlalchemy>=2.0",
32
32
  ]
33
33
 
@@ -15,6 +15,8 @@ Primary exports:
15
15
  The pytest plugin auto-registers these fixtures (no imports needed):
16
16
  ``fake_event_bus``, ``build_test_app``, ``settings``, ``db_state``,
17
17
  ``engine``, ``db_session``, ``app``, ``client``, and ``authenticated_client``.
18
+ ``redis_server`` starts a throwaway ``redis-server`` for the tests that need a
19
+ real broker (cross-worker cache invalidation) and skips when the binary is absent.
18
20
  See ``simple_module_test.plugin`` (and ``simple_module_test.fixtures``) for
19
21
  their definitions. ``authenticated_client`` additionally requires the ``users``
20
22
  module to be installed — it seeds an admin via ``users.bootstrap``.
@@ -51,8 +51,11 @@ async def _create_all_tables(engine) -> None:
51
51
  """
52
52
  from sqlalchemy import text
53
53
 
54
+ from simple_module_test.database import reset_schema_once
55
+
54
56
  bases = _ensure_models_imported()
55
57
  heads = _alembic_heads()
58
+ await reset_schema_once(engine) # Postgres: start empty; SQLite memory already is
56
59
 
57
60
  async with engine.begin() as conn:
58
61
 
@@ -0,0 +1,78 @@
1
+ """Which database the test fixtures run on (#343).
2
+
3
+ In-memory SQLite by default. Set ``SM_TEST_DATABASE_URL`` to an async URL
4
+ (``postgresql+asyncpg://postgres@localhost:5432/sm_test``) to run the same
5
+ suites on Postgres — what SQLite cannot show: real concurrent transactions,
6
+ row locks, stricter types, and SQL that SQLite happens to accept.
7
+
8
+ On Postgres every fixture starts from an empty ``public`` schema, so tests
9
+ stay independent; pooled connections are disabled because pytest-asyncio
10
+ gives each test its own event loop.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ import os
16
+ from typing import Any
17
+
18
+ from sqlalchemy import text
19
+ from sqlalchemy.pool import NullPool
20
+
21
+ TEST_DATABASE_ENV = "SM_TEST_DATABASE_URL"
22
+ SQLITE_MEMORY = "sqlite+aiosqlite:///:memory:"
23
+
24
+
25
+ def database_url_for_tests() -> str:
26
+ return os.environ.get(TEST_DATABASE_ENV) or SQLITE_MEMORY
27
+
28
+
29
+ def is_sqlite(url: str) -> bool:
30
+ return url.startswith("sqlite")
31
+
32
+
33
+ def init_db_kwargs(url: str) -> dict[str, Any]:
34
+ return {} if is_sqlite(url) else {"poolclass": NullPool}
35
+
36
+
37
+ async def reset_schema(engine: Any) -> None:
38
+ """Empty the database before a test (Postgres); a no-op on SQLite."""
39
+ if engine.dialect.name == "sqlite":
40
+ return
41
+ async with engine.begin() as conn:
42
+ await conn.execute(text("DROP SCHEMA IF EXISTS public CASCADE"))
43
+ await conn.execute(text("CREATE SCHEMA public"))
44
+
45
+
46
+ # The test the schema was last emptied for. On SQLite every engine is its own
47
+ # in-memory database; on Postgres the ``app`` and ``db_session`` fixtures share
48
+ # one, so a second reset inside the same test would wipe the first fixture's
49
+ # rows (the seeded admin, say) from under it.
50
+ _current_test: object | None = None
51
+ _reset_for: object | None = None
52
+
53
+
54
+ def begin_test(token: object) -> None:
55
+ """Mark the start of a test; the next ``reset_schema_once`` resets."""
56
+ global _current_test
57
+ _current_test = token
58
+
59
+
60
+ async def reset_schema_once(engine: Any) -> None:
61
+ """``reset_schema`` at most once per test (see ``begin_test``)."""
62
+ global _reset_for
63
+ if _current_test is not None and _reset_for is _current_test:
64
+ return
65
+ await reset_schema(engine)
66
+ _reset_for = _current_test
67
+
68
+
69
+ __all__ = [
70
+ "SQLITE_MEMORY",
71
+ "TEST_DATABASE_ENV",
72
+ "begin_test",
73
+ "database_url_for_tests",
74
+ "init_db_kwargs",
75
+ "is_sqlite",
76
+ "reset_schema",
77
+ "reset_schema_once",
78
+ ]
@@ -31,6 +31,7 @@ from simple_module_hosting.settings import Settings
31
31
  from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession
32
32
 
33
33
  from simple_module_test._schema import _create_all_tables
34
+ from simple_module_test.database import begin_test, database_url_for_tests, init_db_kwargs
34
35
  from simple_module_test.session_cookie import forge_session_cookie
35
36
 
36
37
  _AUTH_PROVIDER_ENV = "SM_AUTH_PROVIDER"
@@ -63,6 +64,16 @@ def pinned_auth_provider() -> Iterator[str]:
63
64
  os.environ[_AUTH_PROVIDER_ENV] = previous
64
65
 
65
66
 
67
+ @pytest.fixture(autouse=True)
68
+ def fresh_test_database(request: pytest.FixtureRequest) -> None:
69
+ """Let the first fixture of each test empty the Postgres schema, and no later one.
70
+
71
+ A no-op on the default in-memory SQLite, where every engine is already
72
+ its own empty database (see ``simple_module_test.database``).
73
+ """
74
+ begin_test(request.node)
75
+
76
+
66
77
  @pytest.fixture
67
78
  def settings() -> Settings:
68
79
  """Settings configured for testing with in-memory SQLite.
@@ -78,7 +89,7 @@ def settings() -> Settings:
78
89
  that expects ``/users/login``.
79
90
  """
80
91
  return Settings(
81
- database_url="sqlite+aiosqlite:///:memory:",
92
+ database_url=database_url_for_tests(),
82
93
  environment="testing",
83
94
  secret_key="test-secret-key",
84
95
  multi_tenant=True,
@@ -92,7 +103,8 @@ async def db_state() -> AsyncGenerator[DatabaseState, None]:
92
103
  """Create a fresh in-memory DatabaseState with listeners registered."""
93
104
  from simple_module_db.listeners import register_listeners
94
105
 
95
- state = init_db("sqlite+aiosqlite:///:memory:")
106
+ url = database_url_for_tests()
107
+ state = init_db(url, **init_db_kwargs(url))
96
108
  register_listeners(state)
97
109
  yield state
98
110
  await state.engine.dispose()
@@ -9,6 +9,7 @@ test suites — treat the fixture surface like a public API.
9
9
  from __future__ import annotations
10
10
 
11
11
  import contextlib
12
+ import os
12
13
 
13
14
  import pytest
14
15
 
@@ -27,10 +28,12 @@ from simple_module_test.fixtures import ( # noqa: F401
27
28
  db_session,
28
29
  db_state,
29
30
  engine,
31
+ fresh_test_database,
30
32
  pinned_auth_provider,
31
33
  settings,
32
34
  setup_pending_app,
33
35
  )
36
+ from simple_module_test.redis_server import redis_server # noqa: F401
34
37
 
35
38
 
36
39
  def _bootstrap_eager_celery() -> None:
@@ -40,7 +43,17 @@ def _bootstrap_eager_celery() -> None:
40
43
  so the host's ``build_celery`` call never runs and ``task.delay()``
41
44
  falls through to the broker. Skipped silently when ``background_tasks``
42
45
  isn't installed.
46
+
47
+ Also switches off the cross-worker invalidation transport. ``on_startup``
48
+ would otherwise open a Redis pub/sub listener per app fixture — against a
49
+ broker no test environment runs — leaving a reconnect loop in the
50
+ background of every suite. ``setdefault``, so a test that wants the transport
51
+ can set the variable and get it — though the tests that exercise the transport
52
+ build ``RedisInvalidationTransport`` directly instead, against either a fake
53
+ client (``test_bg_invalidation.py``) or a throwaway ``redis-server`` (the
54
+ ``redis_server`` fixture, used by ``test_bg_invalidation_redis.py``).
43
55
  """
56
+ os.environ.setdefault("SM_BG_TASKS_BROADCAST_INVALIDATIONS", "false")
44
57
  with contextlib.suppress(ImportError):
45
58
  from background_tasks.celery_app import build_celery
46
59
  from background_tasks.settings import BackgroundTasksSettings
@@ -0,0 +1,145 @@
1
+ """A throwaway ``redis-server`` for tests that need a real broker.
2
+
3
+ The cross-worker invalidation transport is the one piece of this framework whose
4
+ whole job happens *between* processes, and a fake client cannot show that it
5
+ works — it can only show that this repo's own retry and teardown logic behaves.
6
+ So the tests that matter start a real server.
7
+
8
+ Deliberately a subprocess rather than a Docker service or the shared
9
+ ``dev-services`` stack: a unit-test job that runs ``make install-py`` and nothing
10
+ else must be able to run these, and a developer must not have to bring anything
11
+ up first. The server is ephemeral — a free port, no persistence
12
+ (``--save '' --appendonly no``), killed on teardown — so it cannot collide with
13
+ the real ``dev-services`` Redis on 6379 or leave state behind.
14
+
15
+ ``SM_TEST_REDIS_URL`` overrides all of this and is used as-is, which is how CI
16
+ supplies a Redis **service container** instead of a binary: GitHub's
17
+ ``ubuntu-latest`` image does not ship ``redis-server``, contrary to what this
18
+ docstring first claimed — the ``CI`` guard below caught that on the first run,
19
+ which is the whole reason it exists.
20
+
21
+ Absent both the variable and the binary the fixture skips — except under ``CI``,
22
+ where it **fails**.
23
+ A skip is right on a developer's machine and wrong on a build: the whole point of
24
+ these tests is to cover what the fake client cannot, and a runner image that
25
+ stopped shipping ``redis-server`` would silently shed every one of them while the
26
+ build stayed green. That is the same "passes while testing nothing" failure the
27
+ tests themselves exist to rule out, so it gets an explicit guard rather than a
28
+ sentence in a plan.
29
+ """
30
+
31
+ from __future__ import annotations
32
+
33
+ import contextlib
34
+ import os
35
+ import shutil
36
+ import socket
37
+ import subprocess
38
+ import time
39
+ from collections.abc import Iterator
40
+
41
+ import pytest
42
+
43
+ __all__ = ["redis_server"]
44
+
45
+ _STARTUP_TIMEOUT_SECONDS = 10.0
46
+ _POLL_INTERVAL_SECONDS = 0.05
47
+
48
+
49
+ def _free_port() -> int:
50
+ """A port nothing is listening on, as of a moment ago.
51
+
52
+ Inherently racy — the port could be taken between the probe and the bind —
53
+ but the alternative is a fixed port, which collides with a developer's own
54
+ Redis and with a parallel test run. ``redis-server`` fails loudly on a taken
55
+ port, and :func:`_wait_until_ready` turns that into a clear timeout rather
56
+ than a hang.
57
+ """
58
+ with socket.socket() as sock:
59
+ sock.bind(("127.0.0.1", 0))
60
+ return int(sock.getsockname()[1])
61
+
62
+
63
+ def _wait_until_ready(process: subprocess.Popen, port: int) -> None:
64
+ """Block until the server accepts a connection, or fail with its output.
65
+
66
+ Polls a TCP connect rather than shelling out to ``redis-cli``: one less
67
+ binary to require, and the thing under test is whether a socket opens.
68
+ """
69
+ deadline = time.monotonic() + _STARTUP_TIMEOUT_SECONDS
70
+ while time.monotonic() < deadline:
71
+ if process.poll() is not None:
72
+ output = (process.stdout.read() if process.stdout else "") or "(no output)"
73
+ raise RuntimeError(f"redis-server exited during startup: {output}")
74
+ with contextlib.suppress(OSError), socket.create_connection(("127.0.0.1", port), 0.2):
75
+ return
76
+ time.sleep(_POLL_INTERVAL_SECONDS)
77
+ process.kill()
78
+ raise RuntimeError(f"redis-server did not accept connections on {port} in time")
79
+
80
+
81
+ @pytest.fixture(scope="session")
82
+ def redis_server() -> Iterator[str]:
83
+ """A URL for a usable Redis. Session-scoped — a local start costs ~50ms.
84
+
85
+ Session scope means tests share one server, so anything that would leave a
86
+ subscription or a key behind must clean up after itself. Pub/sub carries no
87
+ state between messages, which is why sharing is safe for the invalidation
88
+ tests; a test that wrote keys would want its own database index.
89
+
90
+ With ``SM_TEST_REDIS_URL`` set the server is somebody else's — a CI service
91
+ container — so it is neither private nor guaranteed empty, and this fixture
92
+ neither starts nor stops it.
93
+ """
94
+ external = os.environ.get("SM_TEST_REDIS_URL")
95
+ if external:
96
+ yield external
97
+ return
98
+
99
+ binary = shutil.which("redis-server")
100
+ if binary is None:
101
+ message = (
102
+ "no redis-server binary and no SM_TEST_REDIS_URL, so the real-transport "
103
+ "and cross-process tests cannot run"
104
+ )
105
+ if os.environ.get("CI"):
106
+ pytest.fail(
107
+ f"{message}. On CI this is a failure, not a skip: silently dropping "
108
+ "these tests would leave the cross-process claim untested while the "
109
+ "build stayed green. Give the job a redis service and point "
110
+ "SM_TEST_REDIS_URL at it, or install redis-server on the runner."
111
+ )
112
+ pytest.skip(message)
113
+
114
+ port = _free_port()
115
+ process = subprocess.Popen(
116
+ [
117
+ binary,
118
+ "--port",
119
+ str(port),
120
+ "--bind",
121
+ "127.0.0.1",
122
+ # No persistence: nothing to write, nothing to load, no dump.rdb
123
+ # dropped into whatever directory pytest happened to run from.
124
+ "--save",
125
+ "",
126
+ "--appendonly",
127
+ "no",
128
+ "--databases",
129
+ "1",
130
+ ],
131
+ stdout=subprocess.PIPE,
132
+ stderr=subprocess.STDOUT,
133
+ text=True,
134
+ )
135
+ try:
136
+ _wait_until_ready(process, port)
137
+ yield f"redis://127.0.0.1:{port}/0"
138
+ finally:
139
+ process.terminate()
140
+ with contextlib.suppress(subprocess.TimeoutExpired):
141
+ process.wait(timeout=5)
142
+ if process.poll() is None:
143
+ process.kill()
144
+ if process.stdout is not None:
145
+ process.stdout.close()