MCPcopy Create free account
hub / github.com/MongoEngine/mongoengine / test_hint

Method test_hint

tests/document/test_indexes.py:490–527  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

488 assert indexes["categories_1__id_1"]["key"] == [("categories", 1), ("_id", 1)]
489
490 def test_hint(self):
491 TAGS_INDEX_NAME = "tags_1"
492
493 class BlogPost(Document):
494 tags = ListField(StringField())
495 meta = {"indexes": [{"fields": ["tags"], "name": TAGS_INDEX_NAME}]}
496
497 BlogPost.drop_collection()
498
499 for i in range(10):
500 tags = [("tag %i" % n) for n in range(i % 2)]
501 BlogPost(tags=tags).save()
502
503 # Hinting by shape should work.
504 assert BlogPost.objects.hint([("tags", 1)]).count() == 10
505
506 # Hinting by index name should work.
507 assert BlogPost.objects.hint(TAGS_INDEX_NAME).count() == 10
508
509 # Clearing the hint should work fine.
510 assert BlogPost.objects.hint().count() == 10
511 assert BlogPost.objects.hint([("ZZ", 1)]).hint().count() == 10
512
513 # Hinting on a non-existent index shape should fail.
514 with pytest.raises(OperationFailure):
515 BlogPost.objects.hint([("ZZ", 1)]).count()
516
517 # Hinting on a non-existent index name should fail.
518 with pytest.raises(OperationFailure):
519 BlogPost.objects.hint("Bad Name").count()
520
521 # Invalid shape argument (missing list brackets) should fail.
522 if PYMONGO_VERSION <= (4, 3):
523 with pytest.raises(ValueError):
524 BlogPost.objects.hint(("tags", 1)).count()
525 else:
526 with pytest.raises(TypeError):
527 BlogPost.objects.hint(("tags", 1)).count()
528
529 def test_collation(self):
530 base = {"locale": "en", "strength": 2}

Callers

nothing calls this directly

Calls 5

drop_collectionMethod · 0.80
hintMethod · 0.80
BlogPostClass · 0.70
saveMethod · 0.45
countMethod · 0.45

Tested by

no test coverage detected