mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

test_no_dereference_context_manager_object_id not working

Open clysto opened this issue 4 years ago • 1 comments

test_no_dereference_context_manager_object_id:

https://github.com/MongoEngine/mongoengine/blob/96802599045432274481b4ed9fcc4fad4ce5f89b/tests/test_context_managers.py#L119-L154

run this test:

pytest tests/test_context_managers.py -k "test_no_dereference_context_manager_object_id"

we got no error, but if I put the assert False in with no_dereference(Group) as Group::

        user = User.objects.first()
        Group(ref=user, members=User.objects, generic=user).save()

        with no_dereference(Group) as NoDeRefGroup:
            assert False # <--------
            assert Group._fields["members"]._auto_dereference
            assert not NoDeRefGroup._fields["members"]._auto_dereference

pytest shows that there are still no errors!!!

image

I think this is because the context manager causes the error to not be caught normally

clysto avatar Apr 22 '21 17:04 clysto

https://github.com/MongoEngine/mongoengine/blob/96802599045432274481b4ed9fcc4fad4ce5f89b/mongoengine/context_managers.py#L137-L141

returned value self.cls suppress the exception.

if i remove the return statement, rerun the test:

image

the pytest shows that Group._fields["members"]._auto_dereference is actually False. This is because the Group class and NoRefGroup class are the same class.

clysto avatar Apr 22 '21 17:04 clysto

Just seeing this now, good catch!. Indeed it's due to the faulty return in exit and existing tests were actually failing. The feature was actually quite broken :grimacing:

bagerard avatar Dec 19 '23 22:12 bagerard