Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no_dereference context manager is not working #1864

Closed
bagerard opened this issue Sep 1, 2018 · 2 comments
Closed

no_dereference context manager is not working #1864

bagerard opened this issue Sep 1, 2018 · 2 comments

Comments

@bagerard
Copy link
Collaborator

bagerard commented Sep 1, 2018

The no_dereference context manager suffers from the same issue as the no_subclasses context (#1865) regarding its _exit_ https://github.com/MongoEngine/mongoengine/blob/master/mongoengine/context_managers.py#L130, which means that any exceptions raised during the context gets silenced...

This means that the context is also swallowing the AssertionError being actually raised from the tests...
The following piece of code is not working since at least 0.10.0:

In [1]: import mongoengine as me
   ...: me.connect()
   ...: 
   ...: class Ref(me.Document):
   ...:     whatever = me.StringField()
   ...:     
   ...: class Doc(me.Document):
   ...:     ref = me.ReferenceField(Ref)
   ...: 
   ...: ref = Ref(whatever='').save()
   ...: Doc(ref=ref).save()
   ...: 
   ...: with me.context_managers.no_dereference(Doc):
   ...:     print Doc.objects().first().ref			# should be a DBRef but is a 'Ref object...'
Ref object

In [2]: Doc.objects().no_dereference().first().ref
Out[2]: DBRef('ref', ObjectId('5b8b07714ec5dc0d752d504b'))	# fine

I'll push a PR shortly

@bagerard bagerard changed the title Context managers no_dereference & no_sub_classes are very buggy Context managers no_dereference & no_sub_classes are very buggy and potentially not thread-safe Sep 1, 2018
@bagerard bagerard changed the title Context managers no_dereference & no_sub_classes are very buggy and potentially not thread-safe no_dereference context manager is not working Sep 1, 2018
@bagerard
Copy link
Collaborator Author

bagerard commented Sep 1, 2018

Just like for no_subclasses, it is confusing that the context manager returns something in the _enter_ (i.e: allowing for "with ... as ..." instead of just "with ...") https://github.com/MongoEngine/mongoengine/blob/master/tests/test_context_managers.py#L86
But in the following

with no_dereference(Group) as NoDeRefGroup:
    Group is NoDeRefGroup # True!

I find this confusing because the user might think that he can mess around with the NoDeRefGroup class but he is actually using Group. May I remove the "as ..." construct (breaking change)?

@bagerard
Copy link
Collaborator Author

bagerard commented Sep 1, 2018

Closing this as it was already reported (#1245 - #1284). I'll refer to those ticket when fixing it

@bagerard bagerard closed this as completed Sep 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant