MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / test_tailable

Method test_tailable

test/asynchronous/test_cursor.py:1122–1182  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1120 self.assertEqual(10, len(await db.test.find().batch_size(5).to_list()))
1121
1122 async def test_tailable(self):
1123 db = self.db
1124 await db.drop_collection("test")
1125 await db.create_collection("test", capped=True, size=1000, max=3)
1126 self.addAsyncCleanup(db.drop_collection, "test")
1127 cursor = db.test.find(cursor_type=CursorType.TAILABLE)
1128
1129 await db.test.insert_one({"x": 1})
1130 count = 0
1131 async for doc in cursor:
1132 count += 1
1133 self.assertEqual(1, doc["x"])
1134 self.assertEqual(1, count)
1135
1136 await db.test.insert_one({"x": 2})
1137 count = 0
1138 async for doc in cursor:
1139 count += 1
1140 self.assertEqual(2, doc["x"])
1141 self.assertEqual(1, count)
1142
1143 await db.test.insert_one({"x": 3})
1144 count = 0
1145 async for doc in cursor:
1146 count += 1
1147 self.assertEqual(3, doc["x"])
1148 self.assertEqual(1, count)
1149
1150 # Capped rollover - the collection can never
1151 # have more than 3 documents. Just make sure
1152 # this doesn't raise...
1153 await db.test.insert_many([{"x": i} for i in range(4, 7)])
1154 self.assertEqual(0, len(await cursor.to_list()))
1155
1156 # and that the cursor doesn't think it's still alive.
1157 self.assertFalse(cursor.alive)
1158
1159 self.assertEqual(3, await db.test.count_documents({}))
1160
1161 # __getitem__(index)
1162 if _IS_SYNC:
1163 for cursor in (
1164 db.test.find(cursor_type=CursorType.TAILABLE),
1165 db.test.find(cursor_type=CursorType.TAILABLE_AWAIT),
1166 ):
1167 self.assertEqual(4, cursor[0]["x"])
1168 self.assertEqual(5, cursor[1]["x"])
1169 self.assertEqual(6, cursor[2]["x"])
1170
1171 cursor.rewind()
1172 self.assertEqual([4], [doc["x"] for doc in cursor[0:1]])
1173 cursor.rewind()
1174 self.assertEqual([5], [doc["x"] for doc in cursor[1:2]])
1175 cursor.rewind()
1176 self.assertEqual([6], [doc["x"] for doc in cursor[2:3]])
1177 cursor.rewind()
1178 self.assertEqual([4, 5], [doc["x"] for doc in cursor[0:2]])
1179 cursor.rewind()

Callers

nothing calls this directly

Calls 9

addAsyncCleanupMethod · 0.80
drop_collectionMethod · 0.45
create_collectionMethod · 0.45
findMethod · 0.45
insert_oneMethod · 0.45
insert_manyMethod · 0.45
to_listMethod · 0.45
count_documentsMethod · 0.45
rewindMethod · 0.45

Tested by

no test coverage detected