# ========= Copyright 2026 @ Strukto.AI All Rights Reserved. ========= # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ========= Copyright 2026 @ Strukto.AI All Rights Reserved. ========= from unittest.mock import MagicMock, patch import pytest from pymongo.driver_info import DriverInfo from mirage.accessor.mongodb import _DRIVER_INFO, MongoDBAccessor from mirage.resource.mongodb.config import MongoDBConfig @pytest.fixture def accessor(): return MongoDBAccessor(config=MongoDBConfig( uri="mongodb://localhost:27017")) def test_driver_info_is_pymongo_driverinfo_named_mirage(): assert isinstance(_DRIVER_INFO, DriverInfo) assert _DRIVER_INFO.name == "Mirage" @pytest.mark.asyncio async def test_client_constructs_async_mongo_client_with_driver_info(accessor): sentinel = MagicMock() with patch("mirage.accessor.mongodb.AsyncMongoClient", return_value=sentinel) as ctor: client = accessor.client assert client is sentinel ctor.assert_called_once_with("mongodb://localhost:27017", driver=_DRIVER_INFO) @pytest.mark.asyncio async def test_client_is_cached_per_event_loop(accessor): with patch("mirage.accessor.mongodb.AsyncMongoClient", side_effect=lambda *a, **k: MagicMock()) as ctor: first = accessor.client second = accessor.client assert first is second ctor.assert_called_once() def test_client_built_outside_event_loop_uses_loopless_key(accessor): with patch("mirage.accessor.mongodb.AsyncMongoClient", side_effect=lambda *a, **k: MagicMock()) as ctor: first = accessor.client second = accessor.client assert first is second ctor.assert_called_once() assert 0 in accessor._clients