def test_insert_one_no_validation(register_documents): invalid_document = {"invalid": "doc"} result = collection(Document).insert_one(invalid_document, _skip_validation=True) assert isinstance(result, InsertOneResult) assert collection(Document).find_one({"_id": result.inserted_id}, _skip_validation=True)
def test_find_skip_validation(register_documents): invalid_document = {"invalid": "doc"} _id = (collection(Document).insert_one(invalid_document, _skip_validation=True).inserted_id) result = collection(Document).find({"_id": _id}, _skip_validation=True)[0] assert isinstance(result, dict) assert result == {"_id": _id, "invalid": "doc"}
def test_find(document, name, money): document.name = name document.money = money collection(Document).insert_one(document) result = collection(Document).find({"_id": document.uid}).first() assert isinstance(result, Document) assert result.name == name assert result.money == money
def test_update_one(document): assert document.name != "Jake" collection(Document).insert_one(document) result = collection(Document).update_one({"_id": document.uid}, {"$set": { "name": "Jake" }}) assert isinstance(result, UpdateResult) assert result.matched_count == 1 assert result.modified_count == 1 assert result.upserted_id is None
def test_upsert_one(document): assert document.name != "Jake" result = collection(Document).update_one({"_id": document.uid}, {"$set": { "name": "Jake" }}, upsert=True) assert isinstance(result, UpdateResult) assert result.matched_count == 0 assert result.modified_count == 0 assert result.upserted_id is not None document = collection(Document).find_one({"_id": result.upserted_id}) assert document.name == "Jake"
def test_find_one_nothing(register_documents): result = collection(Document).find_one({"nonexistent": 10}) assert result is None
def test_insert_one_validation(register_documents): document = {"name": "walter", "amount_of_money": 10} result = collection(Document).insert_one(document) assert isinstance(result, InsertOneResult) assert collection(Document).find_one({"_id": result.inserted_id})
def test_insert_one(document): result = collection(Document).insert_one(document) assert isinstance(result, InsertOneResult) assert collection(Document).find_one({"_id": result.inserted_id})
def test_collection_function(monkeypatch): with monkeypatch.context() as m: _class, _result = type(object), object() m.setattr(MongoManager, "get_collection", MagicMock(return_value=_result)) assert collection(_class) is _result MongoManager.get_collection.assert_called_with(_class)