|
1 | | -"""Shared Mongo test configuration and helpers.""" |
| 1 | +"""Shared Mongo test fixtures.""" |
2 | 2 |
|
3 | | -import platform |
4 | | -import sys |
5 | | -from contextlib import suppress |
6 | | -from urllib.parse import quote_plus |
| 3 | +import pytest |
7 | 4 |
|
8 | | -from birch import Birch # type: ignore[import-not-found] |
| 5 | +from .helpers import _cleanup_mongo_client |
9 | 6 |
|
10 | | -try: |
11 | | - from pymongo.mongo_client import MongoClient |
12 | | -except (ImportError, ModuleNotFoundError): |
13 | 7 |
|
14 | | - class MongoClient: |
15 | | - """Mock MongoClient class raising ImportError on missing pymongo.""" |
16 | | - |
17 | | - def __init__(self, *args, **kwargs): |
18 | | - """Initialize the mock MongoClient.""" |
19 | | - raise ImportError("pymongo is not installed!") |
20 | | - |
21 | | - |
22 | | -try: |
23 | | - from pymongo_inmemory import MongoClient as InMemoryMongoClient |
24 | | -except (ImportError, ModuleNotFoundError): |
25 | | - |
26 | | - class InMemoryMongoClient: |
27 | | - """Mock InMemoryMongoClient class. |
28 | | -
|
29 | | - Raises an ImportError on missing pymongo_inmemory. |
30 | | -
|
31 | | - """ |
32 | | - |
33 | | - def __init__(self, *args, **kwargs): |
34 | | - """Initialize the mock InMemoryMongoClient.""" |
35 | | - raise ImportError("pymongo_inmemory is not installed!") |
36 | | - |
37 | | - |
38 | | -class CfgKey: |
39 | | - HOST = "TEST_HOST" |
40 | | - PORT = "TEST_PORT" |
41 | | - TEST_VS_DOCKERIZED_MONGO = "TEST_VS_DOCKERIZED_MONGO" |
42 | | - |
43 | | - |
44 | | -CFG = Birch(namespace="cachier", defaults={CfgKey.TEST_VS_DOCKERIZED_MONGO: False}) |
45 | | -_COLLECTION_NAME = f"cachier_test_{platform.system()}_{'.'.join(map(str, sys.version_info[:3]))}" |
46 | | - |
47 | | - |
48 | | -def _get_cachier_db_mongo_client(): |
49 | | - host = quote_plus(CFG[CfgKey.HOST]) |
50 | | - port = quote_plus(CFG[CfgKey.PORT]) |
51 | | - uri = f"mongodb://{host}:{port}?retrywrites=true&w=majority" |
52 | | - return MongoClient(uri) |
53 | | - |
54 | | - |
55 | | -def _test_mongetter(): |
56 | | - if not hasattr(_test_mongetter, "client"): |
57 | | - if str(CFG.mget(CfgKey.TEST_VS_DOCKERIZED_MONGO)).lower() == "true": |
58 | | - print("Using live MongoDB instance for testing.") |
59 | | - _test_mongetter.client = _get_cachier_db_mongo_client() |
60 | | - else: |
61 | | - print("Using in-memory MongoDB instance for testing.") |
62 | | - _test_mongetter.client = InMemoryMongoClient() |
63 | | - db_obj = _test_mongetter.client["cachier_test"] |
64 | | - if _COLLECTION_NAME not in db_obj.list_collection_names(): |
65 | | - db_obj.create_collection(_COLLECTION_NAME) |
66 | | - return db_obj[_COLLECTION_NAME] |
67 | | - |
68 | | - |
69 | | -def _cleanup_mongo_client(): |
70 | | - """Close any cached Mongo test client safely.""" |
71 | | - client = getattr(_test_mongetter, "client", None) |
72 | | - if client is None: |
73 | | - return |
74 | | - with suppress(Exception): |
75 | | - client._mongod._client.close() # type: ignore[attr-defined] |
76 | | - with suppress(Exception): |
77 | | - client.close() |
78 | | - with suppress(Exception): |
79 | | - delattr(_test_mongetter, "client") |
| 8 | +@pytest.fixture(scope="session", autouse=True) |
| 9 | +def cleanup_mongo_client_fixture(): |
| 10 | + """Release cached Mongo client resources after the test session.""" |
| 11 | + yield |
| 12 | + _cleanup_mongo_client() |
0 commit comments