Skip to content

Commit f9ba0c7

Browse files
committed
simplify tests
1 parent 8322387 commit f9ba0c7

File tree

1 file changed

+19
-54
lines changed

1 file changed

+19
-54
lines changed

test/test_mypy.py

Lines changed: 19 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,20 @@
1717

1818
import os
1919
import tempfile
20+
import typing
2021
import unittest
21-
from typing import TYPE_CHECKING, Any, Dict, Iterable, Iterator, List, Optional
22+
from typing import (
23+
TYPE_CHECKING,
24+
Any,
25+
Callable,
26+
Dict,
27+
Iterable,
28+
Iterator,
29+
List,
30+
Optional,
31+
Sequence,
32+
Tuple,
33+
)
2234

2335
try:
2436
from typing_extensions import NotRequired, TypedDict
@@ -67,6 +79,7 @@ class MovieWithoutId(TypedDict): # type: ignore[misc]
6779
from pymongo.collection import Collection
6880
from pymongo.operations import InsertOne
6981
from pymongo.read_preferences import ReadPreference
82+
from pymongo.results import BulkWriteResult, InsertManyResult, InsertOneResult
7083

7184
TEST_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "mypy_fails")
7285

@@ -326,59 +339,11 @@ def test_typeddict_document_type(self) -> None:
326339

327340
@only_type_check
328341
def test_typeddict_document_type_insertion(self) -> None:
329-
client: MongoClient[Movie] = MongoClient()
330-
coll: Collection[Movie] = client.test.test
331-
mov = Movie(_id=ObjectId(), name="THX-1138", year=1971)
332-
for meth, arg in [
333-
(coll.insert_many, [mov]),
334-
(coll.insert_one, mov),
335-
(coll.bulk_write, [InsertOne(mov)]),
336-
]:
337-
meth(arg) # type:ignore[operator]
338-
out = coll.find_one({"name": "THX-1138"})
339-
assert out is not None
340-
# This should fail because the output is a Movie.
341-
assert out["foo"] # type:ignore[typeddict-item]
342-
assert type(out["_id"]) == ObjectId
343-
coll.drop()
344-
345-
# This should work the same as the test above, but this time using NotRequired to allow
346-
# automatic insertion of the _id field by insert_one.
347-
@only_type_check
348-
def test_typeddict_document_type_not_required(self) -> None:
349-
client: MongoClient[ImplicitMovie] = MongoClient()
350-
coll: Collection[ImplicitMovie] = client.test.test
351-
mov = ImplicitMovie(name="THX-1138", year=1971)
352-
for meth, arg in [
353-
(coll.insert_many, [mov]),
354-
(coll.insert_one, mov),
355-
(coll.bulk_write, [InsertOne(mov)]),
356-
]:
357-
meth(arg) # type:ignore[operator]
358-
out = coll.find_one({"name": "THX-1138"})
359-
assert out is not None
360-
# This should fail because the output is a Movie.
361-
assert out["foo"] # type:ignore[typeddict-item]
362-
assert type(out["_id"]) == ObjectId
363-
coll.drop()
364-
365-
@only_type_check
366-
def test_typeddict_document_type_empty(self) -> None:
367-
client: MongoClient[MovieWithoutId] = MongoClient()
368-
coll: Collection[MovieWithoutId] = client.test.test
369-
mov = MovieWithoutId(name="THX-1138", year=1971)
370-
for meth, arg in [
371-
(coll.insert_many, [mov]),
372-
(coll.insert_one, mov),
373-
(coll.bulk_write, [InsertOne(mov)]),
374-
]:
375-
meth(arg) # type:ignore[operator]
376-
out = coll.find_one({"name": "THX-1138"})
377-
assert out is not None
378-
# This should fail because the output is a Movie.
379-
assert out["foo"] # type:ignore[typeddict-item]
380-
# This should fail because _id is not included in our TypedDict definition.
381-
assert type(out["_id"]) == ObjectId # type:ignore[typeddict-item]
342+
coll: Collection[Movie] = self.db.test
343+
mov = Movie(name="THX-1138", year=1971)
344+
coll.insert_one(mov)
345+
coll.insert_many([mov])
346+
coll.bulk_write([InsertOne(mov)])
382347

383348
@only_type_check
384349
def test_raw_bson_document_type(self) -> None:

0 commit comments

Comments
 (0)