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

Method test_create_index

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

Source from the content-addressed store, hash-verified

284 self.db.coll.create_indexes([IndexModel("a")], commitQuorum="majority")
285
286 def test_create_index(self):
287 db = self.db
288
289 with self.assertRaises(TypeError):
290 db.test.create_index(5) # type: ignore[arg-type]
291 with self.assertRaises(ValueError):
292 db.test.create_index([])
293
294 db.test.drop_indexes()
295 db.test.insert_one({})
296 self.assertEqual(len(db.test.index_information()), 1)
297
298 db.test.create_index("hello")
299 db.test.create_index([("hello", DESCENDING), ("world", ASCENDING)])
300
301 # Tuple instead of list.
302 db.test.create_index((("world", ASCENDING),))
303
304 self.assertEqual(len(db.test.index_information()), 4)
305
306 db.test.drop_indexes()
307 ix = db.test.create_index([("hello", DESCENDING), ("world", ASCENDING)], name="hello_world")
308 self.assertEqual(ix, "hello_world")
309
310 db.test.drop_indexes()
311 self.assertEqual(len(db.test.index_information()), 1)
312 db.test.create_index("hello")
313 self.assertIn("hello_1", db.test.index_information())
314
315 db.test.drop_indexes()
316 self.assertEqual(len(db.test.index_information()), 1)
317 db.test.create_index([("hello", DESCENDING), ("world", ASCENDING)])
318 self.assertIn("hello_-1_world_1", db.test.index_information())
319
320 db.test.drop_indexes()
321 db.test.create_index([("hello", DESCENDING), ("world", ASCENDING)], name=None)
322 self.assertIn("hello_-1_world_1", db.test.index_information())
323
324 db.test.drop()
325 db.test.insert_one({"a": 1})
326 db.test.insert_one({"a": 1})
327 with self.assertRaises(DuplicateKeyError):
328 db.test.create_index("a", unique=True)
329
330 with self.write_concern_collection() as coll:
331 coll.create_index([("hello", DESCENDING)])
332
333 db.test.create_index(["hello", "world"])
334 db.test.create_index(["hello", ("world", DESCENDING)])
335 db.test.create_index({"hello": 1}.items()) # type:ignore[arg-type]
336
337 def test_drop_index(self):
338 db = self.db

Callers

nothing calls this directly

Calls 7

itemsMethod · 0.80
create_indexMethod · 0.45
drop_indexesMethod · 0.45
insert_oneMethod · 0.45
index_informationMethod · 0.45
dropMethod · 0.45

Tested by

no test coverage detected