diff --git a/suitcase/mongo_normalized/__init__.py b/suitcase/mongo_normalized/__init__.py index af68543..6a2b478 100644 --- a/suitcase/mongo_normalized/__init__.py +++ b/suitcase/mongo_normalized/__init__.py @@ -83,9 +83,8 @@ def __init__( self._asset_registry_db = assets_db self._ignore_duplicates = ignore_duplicates self._resource_uid_unique = resource_uid_unique - self._create_indexes() - def _create_indexes(self): + def create_indexes(self): """ Create indexes on the various collections. diff --git a/suitcase/mongo_normalized/tests/conftest.py b/suitcase/mongo_normalized/tests/conftest.py index 6f365cb..e4e5157 100644 --- a/suitcase/mongo_normalized/tests/conftest.py +++ b/suitcase/mongo_normalized/tests/conftest.py @@ -2,4 +2,4 @@ from ophyd.tests.conftest import hw # noqa from suitcase.utils.tests.conftest import ( # noqa example_data, generate_data, plan_type, detector_list, event_type) -from .fixtures import db_factory # noqa +from .fixtures import db_factory, db_factory_no_indexes # noqa diff --git a/suitcase/mongo_normalized/tests/fixtures.py b/suitcase/mongo_normalized/tests/fixtures.py index 2fca399..e0c3103 100644 --- a/suitcase/mongo_normalized/tests/fixtures.py +++ b/suitcase/mongo_normalized/tests/fixtures.py @@ -4,6 +4,23 @@ import mongomock import pytest import uuid +from suitcase.mongo_normalized import Serializer + + +@pytest.fixture() +def db_factory_no_indexes(request): + def inner(): + database_name = f'test-{str(uuid.uuid4())}' + uri = 'mongodb://localhost:27017/' + client = mongomock.MongoClient(uri) + db = client[database_name] + + def drop(): + client.drop_database(database_name) + + request.addfinalizer(drop) + return db + return inner @pytest.fixture() @@ -12,10 +29,13 @@ def inner(): database_name = f'test-{str(uuid.uuid4())}' uri = 'mongodb://localhost:27017/' client = mongomock.MongoClient(uri) + db = client[database_name] + serializer = Serializer(db, db) + serializer.create_indexes() def drop(): client.drop_database(database_name) request.addfinalizer(drop) - return client[database_name] + return db return inner diff --git a/suitcase/mongo_normalized/tests/tests.py b/suitcase/mongo_normalized/tests/tests.py index 2ba7b5a..9bfac81 100644 --- a/suitcase/mongo_normalized/tests/tests.py +++ b/suitcase/mongo_normalized/tests/tests.py @@ -23,6 +23,7 @@ def test_duplicates(db_factory, example_data): metadatastore_db = db_factory() asset_registry_db = db_factory() serializer = Serializer(metadatastore_db, asset_registry_db) + for item in documents: serializer(*item) for item in documents: @@ -204,12 +205,10 @@ def test_index_creation(db_factory): assert indexes["descriptor_-1_time_1"] -def test_resource_uid_unique(db_factory): - db = db_factory() - print(type(db)) - metadatastore_db = db_factory() - asset_registry_db = db_factory() - Serializer(metadatastore_db, asset_registry_db, resource_uid_unique=True) +def test_resource_uid_unique(db_factory_no_indexes): + metadatastore_db = db_factory_no_indexes() + asset_registry_db = db_factory_no_indexes() + Serializer(metadatastore_db, asset_registry_db, resource_uid_unique=True).create_indexes() indexes = asset_registry_db.resource.index_information() assert indexes["uid_1"].get("unique")