def test_duplicate_key_error(self, monkeypatch, orion_db, test_collection): """Should raise generic DuplicateKeyError.""" # Add unique indexes to force trigger of DuplicateKeyError on write() orion_db.ensure_index( "test_collection", [("field0", Database.ASCENDING), ("datetime", Database.ASCENDING)], unique=True, ) config_to_add = test_collection[0] config_to_add.pop("_id") query = {"_id": test_collection[1]["_id"]} # Make sure it raises pymongo.errors.DuplicateKeyError when there is no # wrapper monkeypatch.setattr( orion_db, "read_and_write", functools.partial(orion_db.read_and_write.__wrapped__, orion_db), ) with pytest.raises(pymongo.errors.DuplicateKeyError) as exc_info: orion_db.read_and_write("test_collection", query, config_to_add) monkeypatch.undo() # Verify that the wrapper converts it properly to DuplicateKeyError with pytest.raises(DuplicateKeyError) as exc_info: orion_db.read_and_write("test_collection", query, config_to_add) assert "duplicate key error" in str(exc_info.value)
def test_concurrent_unique_writes(self, orion_db): """Test that concurrent writes cannot duplicate unique fields""" orion_db.ensure_index("concurrent", "unique", unique=True) assert orion_db.count("concurrent", {"unique": 1}) == 0 Pool(10).starmap(write, (("unique", 1) for i in range(10))) assert orion_db.count("concurrent", {"unique": 1}) == 1
def test_concurrent_writes(self, orion_db): """Test that concurrent writes all get written properly""" orion_db.ensure_index("concurrent", "diff") assert orion_db.count("concurrent", {"diff": {"$gt": -1}}) == 0 Pool(10).starmap(write, (("diff", i) for i in range(10))) assert orion_db.count("concurrent", {"diff": {"$gt": -1}}) == 10
def test_drop_unique_index(self, orion_db): """Test with single indexes.""" orion_db.ensure_index("hello", [("bonjour", Database.DESCENDING)], unique=True) index_info = orion_db.index_information("hello") assert index_info == {"_id_": True, "bonjour_-1": True} orion_db.drop_index("hello", "bonjour_-1") index_info = orion_db.index_information("hello") assert index_info == {"_id_": True}
def test_unique_index(self, orion_db): """Index should be set as unique in mongo database's index information.""" assert ("name_1_metadata.user_1" not in get_db(orion_db)["experiments"].index_information()) orion_db.ensure_index( "experiments", [("name", Database.ASCENDING), ("metadata.user", Database.ASCENDING)], unique=True, ) index_information = get_db(orion_db)["experiments"].index_information() assert "name_1_metadata.user_1" in index_information assert index_information["name_1_metadata.user_1"]["unique"]
def test_drop_ordered_index(self, orion_db, db_test_data): """Test with single and compound indexes.""" ( keys_1, keys_2, stored_keys_1, stored_keys_2, index_information_initial, index_information_wo_1, ) = db_test_data orion_db.ensure_index("experiments", keys_1) orion_db.ensure_index("experiments", keys_2) assert orion_db.index_information( "experiments") == index_information_initial orion_db.drop_index("experiments", stored_keys_1) assert orion_db.index_information( "experiments") == index_information_wo_1 with pytest.raises(DatabaseError) as exc: orion_db.drop_index("experiments", stored_keys_1) assert "index not found with name" in str(exc.value) orion_db.drop_index("experiments", stored_keys_2) assert orion_db.index_information("experiments") == {"_id_": True}