8000 Forward CodecOptions to BSON Encoder. by joshuashaffer · Pull Request #793 · mongomock/mongomock · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Forward CodecOptions to BSON Encoder. #793

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

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from

Conversation

joshuashaffer
Copy link
Contributor

Fixes #745.

This allows the use of UUID in pymongo4.

Passes unit tests.

@codecov
Copy link
codecov bot commented Jul 14, 2022

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.30%. Comparing base (d5fc4c7) to head (d7c8004).
Report is 34 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop     #793   +/-   ##
========================================
  Coverage    95.29%   95.30%           
========================================
  Files           20       20           
  Lines         3849     3853    +4     
========================================
+ Hits          3668     3672    +4     
  Misses         181      181           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@joshuashaffer
Copy link
Contributor Author

@pcorpet, does this seem to be an acceptable way of getting UUID support in pymongo4 ???

@jschlyter
Copy link

Is setting uuidRepresentation via MongoClient , either via kwargs or via URI, also supported?

@joshuashaffer
Copy link
Contributor Author

Is setting uuidRepresentation via MongoClient , either via kwargs or via URI, also supported?

Here's the test I used

https://github.com/mongomock/mongomock/pull/793/files#diff-dec417cb2fe6e43e2fac60200a0dc7b290cbfd4bf3aa78a2505ae802b22c57fcR152

Via kwargs to the collection.

@Greeengooo
Copy link

@pcorpet We are waiting for your opinion. Uuid representation is a great feature that will unblock many teams

@pcorpet
Copy link
Member
pcorpet commented Aug 19, 2022

Thanks a lot for taking a look at this one.

I think the solution is on the right track, and thanks for adding the value in the BSON check. I'll comment in the review for smaller stuffs. The major point missing to me is comparing UUIDs in the same server from two different client objects using different UUID representations. I think the easiest way is to prevent any legacy Java / legacy C encoding as the python & standard encodings are the same. By prevent I mean raise a NotImplementedError.

Copy link
Member
@pcorpet pcorpet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test in test__mongomock.py to check that PyMongo + MongoDb and Mongomock have the same behaviors when using a uuid_representation=4

except ImportError:
json_utils = SON = BSON = None
CodecOptions = type(None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's possible, I'd rather have it to be None, and that you check that it's not None before using it.

@@ -74,6 +76,14 @@ class ReturnDocument(object):
}


def _bson_encode(document, codec_options):
if isinstance(codec_options, CodecOptions):
BSON.encode(document, check_keys=True,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please break after encode(

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time to add ruff format lint stage to the CI? :)

@@ -74,6 +76,14 @@ class ReturnDocument(object):
}


def _bson_encode(document, codec_options):
if isinstance(codec_options, CodecOptions):
BSON.encode(document, check_keys=True,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test where this fails because of the encoding?

with self.assertRaises(NotImplementedError):
self.database.with_options(custom_uuid_representation)
db = self.database
db.get_collection('yes_hello',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please break after get_collection(

db = self.database
db.get_collection('yes_hello',
codec_options=custom_uuid_representation).insert_one({'_id': uuid4()})
# db.yes_hello.insert_one({'_id': uuid4()})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either drop this line or remove the #

@arturmkr
Copy link

@pcorpet
@joshuashaffer
Hello,
Do you need any help with this PR?

@arturmkr
Copy link
arturmkr commented Oct 20, 2022

Any updates on this?

@hugo-azevedo
Copy link
hugo-azevedo commented Nov 1, 2022

Any news on when this fix is coming? Thanks

@arturmkr
Copy link
arturmkr commented Nov 30, 2022

PyMongo 4 was released one year ago.
But MongoMock still does not support.
@pcorpet , any plans on updating, supporting MongoMock?
Thanks!

< 6DAF /div>
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

Successfully merging this pull request may close these issues.

Support UUID representation
7 participants
0