MCPcopy Create free account
hub / github.com/mongodb/mongo-python-driver / test_create_indexes

Method test_create_indexes

test/test_collection.py:221–270  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

219 self.db.drop_collection("test")
220
221 def test_create_indexes(self):
222 db = self.db
223
224 with self.assertRaises(TypeError):
225 db.test.create_indexes("foo") # type: ignore[arg-type]
226 with self.assertRaises(TypeError):
227 db.test.create_indexes(["foo"]) # type: ignore[list-item]
228 self.assertRaises(TypeError, IndexModel, 5)
229 self.assertRaises(ValueError, IndexModel, [])
230
231 db.test.drop_indexes()
232 db.test.insert_one({})
233 self.assertEqual(len(db.test.index_information()), 1)
234
235 db.test.create_indexes([IndexModel("hello")])
236 db.test.create_indexes([IndexModel([("hello", DESCENDING), ("world", ASCENDING)])])
237
238 # Tuple instead of list.
239 db.test.create_indexes([IndexModel((("world", ASCENDING),))])
240
241 self.assertEqual(len(db.test.index_information()), 4)
242
243 db.test.drop_indexes()
244 names = db.test.create_indexes(
245 [IndexModel([("hello", DESCENDING), ("world", ASCENDING)], name="hello_world")]
246 )
247 self.assertEqual(names, ["hello_world"])
248
249 db.test.drop_indexes()
250 self.assertEqual(len(db.test.index_information()), 1)
251 db.test.create_indexes([IndexModel("hello")])
252 self.assertIn("hello_1", db.test.index_information())
253
254 db.test.drop_indexes()
255 self.assertEqual(len(db.test.index_information()), 1)
256 names = db.test.create_indexes(
257 [IndexModel([("hello", DESCENDING), ("world", ASCENDING)]), IndexModel("hello")]
258 )
259 info = db.test.index_information()
260 for name in names:
261 self.assertIn(name, info)
262
263 db.test.drop()
264 db.test.insert_one({"a": 1})
265 db.test.insert_one({"a": 1})
266 with self.assertRaises(DuplicateKeyError):
267 db.test.create_indexes([IndexModel("a", unique=True)])
268
269 with self.write_concern_collection() as coll:
270 coll.create_indexes([IndexModel("hello")])
271
272 @client_context.require_version_max(4, 3, -1)
273 def test_create_indexes_commitQuorum_requires_44(self):

Callers

nothing calls this directly

Calls 7

IndexModelClass · 0.85
create_indexesMethod · 0.45
drop_indexesMethod · 0.45
insert_oneMethod · 0.45
index_informationMethod · 0.45
dropMethod · 0.45

Tested by

no test coverage detected