mdomke / pytest-mongodb

pytest plugin for mocking MongoDB with fixtures
MIT License
66 stars 19 forks source link

Testing with mongoengine #24

Open Inventrohyder opened 2 years ago

Inventrohyder commented 2 years ago

Is it possible to use this library to test mongoengine?

Here is a preliminary test:

def test_something(mongodb: Database):
    print(f"type(mongodb.user)\t->\t{type(mongodb.user)}")
    print(f"mongodb.user.count()\t->\t{mongodb.user.count()}")
    print(f"type(User.objects()\t->\t{type(User.objects())}")
    print(f"User.objects().count()\t->\t{User.objects().count()}")
    print(f"type(User)\t->\t{type(User)}")
    print(f"{mongodb.list_collection_names()}")
    print(mongodb.client)
    print(User._get_collection_name())
    for i in User.objects():
        print(f"type(user object)\t->\t{type(i)}")
        print(I)

Output:

tests/services/test_announcement.py ....type(mongodb.user)      ->      <class 'mongomock.collection.Collection'>
mongodb.user.count()    ->      15726
type(User.objects()     ->      <class 'mongoengine.queryset.queryset.QuerySet'>
User.objects().count()  ->      1
type(User)      ->      <class 'modules.mongoengine_extensions.IndexDocumentsMeta'>
['user', 'venue', 'players']
mongomock.MongoClient('localhost', 27017)
user
type(user object)       ->      <class 'src.models.model.User'>
User object

As we can see above mongodb.user.count() is 15726 but User.objects().count() is 1. How do I make the mongoengine documents work with pytest-mongodb?

mdomke commented 2 years ago

To be honest, I don't know. If not been working with MongoDB under Python for quite some time, and I currently don't have the time to investigate this. I would very much appreciate a pull-request if you figure it out.