1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
import pymongo
from beanie.odm.fields import PydanticObjectId
from tests.odm.models import DocumentTestModel
async def test_get(document):
new_document = await DocumentTestModel.get(document.id)
assert new_document == document
async def test_get_not_found(document):
new_document = await DocumentTestModel.get(PydanticObjectId())
assert new_document is None
async def test_find_one(documents):
inserted_one = await documents(1, "kipasa")
await documents(10, "smthe else")
expected_doc_id = PydanticObjectId(inserted_one[0])
new_document = await DocumentTestModel.find_one({"test_str": "kipasa"})
assert new_document.id == expected_doc_id
async def test_find_one_not_found(documents):
await documents(10, "smthe else")
new_document = await DocumentTestModel.find_one({"test_str": "wrong"})
assert new_document is None
async def test_find_one_more_than_one_found(documents):
await documents(10, "one")
await documents(10, "two")
new_document = await DocumentTestModel.find_one({"test_str": "one"})
assert new_document.test_str == "one"
async def test_find_all(documents):
await documents(4, "uno")
await documents(2, "dos")
await documents(1, "cuatro")
result = await DocumentTestModel.find_all().to_list()
assert len(result) == 7
async def test_find_all_limit(documents):
await documents(4, "uno")
await documents(2, "dos")
await documents(1, "cuatro")
result = await DocumentTestModel.find_all(limit=5).to_list()
assert len(result) == 5
async def test_find_all_skip(documents):
await documents(4, "uno")
await documents(2, "dos")
await documents(1, "cuatro")
result = await DocumentTestModel.find_all(skip=1).to_list()
assert len(result) == 6
async def test_find_all_sort(documents):
await documents(4, "uno", True)
await documents(2, "dos", True)
await documents(1, "cuatro", True)
result = await DocumentTestModel.find_all(
sort=[
("test_str", pymongo.ASCENDING),
("test_int", pymongo.DESCENDING),
]
).to_list()
assert result[0].test_str == "cuatro"
assert result[1].test_str == result[2].test_str == "dos"
assert (
result[3].test_str
== result[4].test_str
== result[5].test_str
== result[5].test_str
== "uno"
)
assert result[1].test_int >= result[2].test_int
assert (
result[3].test_int
>= result[4].test_int
>= result[5].test_int
>= result[6].test_int
)
async def test_find_many(documents):
await documents(4, "uno")
await documents(2, "dos")
await documents(1, "cuatro")
result = await DocumentTestModel.find_many(
DocumentTestModel.test_str == "uno"
).to_list()
assert len(result) == 4
async def test_find_many_limit(documents):
await documents(4, "uno")
await documents(2, "dos")
await documents(1, "cuatro")
result = await DocumentTestModel.find_many(
{"test_str": "uno"}, limit=2
).to_list()
assert len(result) == 2
async def test_find_many_skip(documents):
await documents(4, "uno")
await documents(2, "dos")
await documents(1, "cuatro")
result = await DocumentTestModel.find_many(
{"test_str": "uno"}, skip=1
).to_list()
assert len(result) == 3
async def test_find_many_sort(documents):
await documents(4, "uno", True)
await documents(2, "dos", True)
await documents(1, "cuatro", True)
result = await DocumentTestModel.find_many(
{"test_str": "uno"}, sort="test_int"
).to_list()
assert (
result[0].test_int
<= result[1].test_int
<= result[2].test_int
<= result[3].test_int
)
result = await DocumentTestModel.find_many(
{"test_str": "uno"}, sort=[("test_int", pymongo.DESCENDING)]
).to_list()
assert (
result[0].test_int
>= result[1].test_int
>= result[2].test_int
>= result[3].test_int
)
async def test_find_many_not_found(documents):
await documents(4, "uno")
await documents(2, "dos")
await documents(1, "cuatro")
result = await DocumentTestModel.find_many({"test_str": "wrong"}).to_list()
assert len(result) == 0
async def test_get_with_session(document, session):
new_document = await DocumentTestModel.get(document.id, session=session)
assert new_document == document
async def test_find_one_with_session(documents, session):
inserted_one = await documents(1, "kipasa")
await documents(10, "smthe else")
expected_doc_id = PydanticObjectId(inserted_one[0])
new_document = await DocumentTestModel.find_one(
{"test_str": "kipasa"}, session=session
)
assert new_document.id == expected_doc_id
async def test_find_all_with_session(documents, session):
await documents(4, "uno")
await documents(2, "dos")
await documents(1, "cuatro")
result = await DocumentTestModel.find_all(session=session).to_list()
assert len(result) == 7
async def test_find_many_with_session(documents, session):
await documents(4, "uno")
await documents(2, "dos")
await documents(1, "cuatro")
result = await DocumentTestModel.find_many(
{"test_str": "uno"}, session=session
).to_list()
assert len(result) == 4
|