Skip to content

Commit 7331570

Browse files
committed
fix tests more
1 parent f9ba0c7 commit 7331570

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

test/test_mypy.py

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,92 @@ def test_typeddict_document_type(self) -> None:
339339

340340
@only_type_check
341341
def test_typeddict_document_type_insertion(self) -> None:
342-
coll: Collection[Movie] = self.db.test
343-
mov = Movie(name="THX-1138", year=1971)
342+
client: MongoClient[Movie] = MongoClient()
343+
coll = client.test.test
344+
mov = Movie(_id=ObjectId(), name="THX-1138", year=1971)
344345
coll.insert_one(mov)
345346
coll.insert_many([mov])
346347
coll.bulk_write([InsertOne(mov)])
347348

349+
# This should work the same as the test above, but this time using NotRequired to allow
350+
# automatic insertion of the _id field by insert_one.
351+
@only_type_check
352+
def test_typeddict_document_type_normal(self) -> None:
353+
client: MongoClient[Movie] = MongoClient()
354+
coll: Collection[Movie] = client.test.test
355+
mov = Movie(_id=ObjectId(), name="THX-1138", year=1971)
356+
coll.insert_many([mov])
357+
out = coll.find_one({"name": "THX-1138"})
358+
assert out is not None
359+
# This should fail because the output is a Movie.
360+
assert out["foo"] # type:ignore[typeddict-item]
361+
assert type(out["_id"]) == ObjectId
362+
coll.drop()
363+
coll.insert_one(mov)
364+
out = coll.find_one({"name": "THX-1138"})
365+
assert out is not None
366+
assert out["foo"] # type:ignore[typeddict-item]
367+
assert type(out["_id"]) == ObjectId
368+
coll.drop()
369+
coll.bulk_write([InsertOne(mov)])
370+
out = coll.find_one({"name": "THX-1138"})
371+
assert out is not None
372+
assert out["foo"] # type:ignore[typeddict-item]
373+
assert type(out["_id"]) == ObjectId
374+
coll.drop()
375+
376+
@only_type_check
377+
def test_typeddict_document_type_not_required(self) -> None:
378+
client: MongoClient[ImplicitMovie] = MongoClient()
379+
coll: Collection[ImplicitMovie] = client.test.test
380+
mov = ImplicitMovie(name="THX-1138", year=1971)
381+
coll.insert_many([mov])
382+
out = coll.find_one({"name": "THX-1138"})
383+
assert out is not None
384+
# This should fail because the output is a Movie.
385+
assert out["foo"] # type:ignore[typeddict-item]
386+
# This should fail because _id is not included in our TypedDict definition.
387+
assert type(out["_id"]) == ObjectId # type:ignore[typeddict-item]
388+
coll.insert_one(mov)
389+
out = coll.find_one({"name": "THX-1138"})
390+
assert out is not None
391+
assert out["foo"] # type:ignore[typeddict-item]
392+
assert type(out["_id"]) == ObjectId
393+
coll.bulk_write([InsertOne(mov)])
394+
out = coll.find_one({"name": "THX-1138"})
395+
assert out is not None
396+
assert out["foo"] # type:ignore[typeddict-item]
397+
assert type(out["_id"]) == ObjectId
398+
399+
assert out is not None
400+
assert out["foo"] # type:ignore[typeddict-item]
401+
assert type(out["_id"]) == ObjectId
402+
coll.drop()
403+
404+
@only_type_check
405+
def test_typeddict_document_type_no_id(self) -> None:
406+
client: MongoClient[MovieWithoutId] = MongoClient()
407+
coll: Collection[MovieWithoutId] = client.test.test
408+
mov = MovieWithoutId(name="THX-1138", year=1971)
409+
coll.insert_many([mov])
410+
out = coll.find_one({"name": "THX-1138"})
411+
assert out is not None
412+
assert out["foo"] # type:ignore[typeddict-item]
413+
assert type(out["_id"]) == ObjectId # type:ignore[typeddict-item]
414+
coll.drop()
415+
coll.insert_one(mov)
416+
out = coll.find_one({"name": "THX-1138"})
417+
assert out is not None
418+
assert out["foo"] # type:ignore[typeddict-item]
419+
assert type(out["_id"]) == ObjectId # type:ignore[typeddict-item]
420+
coll.drop()
421+
coll.bulk_write([InsertOne(mov)])
422+
out = coll.find_one({"name": "THX-1138"})
423+
assert out is not None
424+
assert out["foo"] # type:ignore[typeddict-item]
425+
assert type(out["_id"]) == ObjectId # type:ignore[typeddict-item]
426+
coll.drop()
427+
348428
@only_type_check
349429
def test_raw_bson_document_type(self) -> None:
350430
client = MongoClient(document_class=RawBSONDocument)

0 commit comments

Comments
 (0)