|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import contextlib |
| 4 | +import traceback |
| 5 | +from collections.abc import Generator |
| 6 | +from dataclasses import dataclass |
| 7 | +from typing import TYPE_CHECKING |
| 8 | + |
| 9 | +import pymongo |
| 10 | +import pytest |
| 11 | +from pymongo.errors import ConnectionFailure |
| 12 | + |
| 13 | +from pytest_databases.helpers import get_xdist_worker_num |
| 14 | +from pytest_databases.types import ServiceContainer, XdistIsolationLevel |
| 15 | + |
| 16 | +if TYPE_CHECKING: |
| 17 | + from collections.abc import Generator |
| 18 | + |
| 19 | + from pymongo import MongoClient |
| 20 | + from pymongo.database import Database |
| 21 | + |
| 22 | + from pytest_databases._service import DockerService |
| 23 | + |
| 24 | + |
| 25 | +@dataclass |
| 26 | +class MongoDBService(ServiceContainer): |
| 27 | + username: str |
| 28 | + password: str |
| 29 | + database: str |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture(scope="session") |
| 33 | +def xdist_mongodb_isolation_level() -> XdistIsolationLevel: |
| 34 | + return "database" |
| 35 | + |
| 36 | + |
| 37 | +@contextlib.contextmanager |
| 38 | +def _provide_mongodb_service( |
| 39 | + docker_service: DockerService, |
| 40 | + image: str, |
| 41 | + name: str, |
| 42 | + isolation_level: XdistIsolationLevel, |
| 43 | +) -> Generator[MongoDBService, None, None]: |
| 44 | + username = "mongo_user" |
| 45 | + password = "mongo_password" |
| 46 | + default_database_name = "pytest_db" |
| 47 | + |
| 48 | + container_name = name |
| 49 | + database_name = default_database_name |
| 50 | + worker_num = get_xdist_worker_num() |
| 51 | + if worker_num is not None: |
| 52 | + suffix = f"_{worker_num}" |
| 53 | + if isolation_level == "server": |
| 54 | + container_name += suffix |
| 55 | + else: |
| 56 | + database_name += suffix |
| 57 | + |
| 58 | + def check(_service: ServiceContainer) -> bool: |
| 59 | + client: MongoClient | None = None |
| 60 | + try: |
| 61 | + client = pymongo.MongoClient( |
| 62 | + host=_service.host, |
| 63 | + port=_service.port, |
| 64 | + username=username, |
| 65 | + password=password, |
| 66 | + serverSelectionTimeoutMS=2000, # Increased timeout for robust check |
| 67 | + connectTimeoutMS=2000, |
| 68 | + socketTimeoutMS=2000, |
| 69 | + ) |
| 70 | + client.admin.command("ping") |
| 71 | + except ConnectionFailure: |
| 72 | + traceback.print_exc() |
| 73 | + return False |
| 74 | + else: |
| 75 | + return True |
| 76 | + finally: |
| 77 | + if client: |
| 78 | + client.close() |
| 79 | + |
| 80 | + with docker_service.run( |
| 81 | + image=image, |
| 82 | + name=container_name, |
| 83 | + container_port=27017, |
| 84 | + env={ |
| 85 | + "MONGO_INITDB_ROOT_USERNAME": username, |
| 86 | + "MONGO_INITDB_ROOT_PASSWORD": password, |
| 87 | + }, |
| 88 | + check=check, |
| 89 | + pause=0.5, |
| 90 | + timeout=120, |
| 91 | + transient=isolation_level == "server", |
| 92 | + ) as service: |
| 93 | + yield MongoDBService( |
| 94 | + host=service.host, port=service.port, username=username, password=password, database=database_name |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +@pytest.fixture(autouse=False, scope="session") |
| 99 | +def mongodb_image() -> str: |
| 100 | + return "mongo:latest" |
| 101 | + |
| 102 | + |
| 103 | +@pytest.fixture(autouse=False, scope="session") |
| 104 | +def mongodb_service( |
| 105 | + docker_service: DockerService, |
| 106 | + xdist_mongodb_isolation_level: XdistIsolationLevel, |
| 107 | + mongodb_image: str, |
| 108 | +) -> Generator[MongoDBService, None, None]: |
| 109 | + with _provide_mongodb_service(docker_service, mongodb_image, "mongodb", xdist_mongodb_isolation_level) as service: |
| 110 | + yield service |
| 111 | + |
| 112 | + |
| 113 | +@pytest.fixture(autouse=False, scope="session") |
| 114 | +def mongodb_connection(mongodb_service: MongoDBService) -> Generator[MongoClient, None, None]: |
| 115 | + client: MongoClient | None = None |
| 116 | + try: |
| 117 | + client = pymongo.MongoClient( |
| 118 | + host=mongodb_service.host, |
| 119 | + port=mongodb_service.port, |
| 120 | + username=mongodb_service.username, |
| 121 | + password=mongodb_service.password, |
| 122 | + ) |
| 123 | + yield client |
| 124 | + finally: |
| 125 | + if client: |
| 126 | + client.close() |
| 127 | + |
| 128 | + |
| 129 | +@pytest.fixture(autouse=False, scope="function") |
| 130 | +def mongodb_database( |
| 131 | + mongodb_connection: MongoClient, mongodb_service: MongoDBService |
| 132 | +) -> Generator[Database, None, None]: |
| 133 | + """Provides a MongoDB database instance for testing. |
| 134 | +
|
| 135 | + Yields: |
| 136 | + A MongoDB database instance. |
| 137 | + """ |
| 138 | + db = mongodb_connection[mongodb_service.database] |
| 139 | + yield db |
| 140 | + # For a truly clean state per test, you might consider dropping the database here, |
| 141 | + # but it depends on the desired test isolation and speed. |
| 142 | + # e.g., mongodb_connection.drop_database(mongodb_service.database) |
0 commit comments