PYTHON-801 - Remove BaseObject.uuid_subtype
This commit is contained in:
parent
7e4ebde147
commit
a3fb2462bb
@ -174,16 +174,15 @@ class Binary(bytes):
|
||||
|
||||
|
||||
class UUIDLegacy(Binary):
|
||||
"""UUID wrapper to support working with UUIDs stored as legacy
|
||||
BSON binary subtype 3.
|
||||
"""UUID wrapper to support working with UUIDs stored as PYTHON_LEGACY.
|
||||
|
||||
.. doctest::
|
||||
|
||||
>>> import uuid
|
||||
>>> from bson.binary import Binary, UUIDLegacy, UUID_SUBTYPE
|
||||
>>> from bson.binary import Binary, UUIDLegacy, STANDARD
|
||||
>>> my_uuid = uuid.uuid4()
|
||||
>>> coll = db.test
|
||||
>>> coll.uuid_subtype = UUID_SUBTYPE
|
||||
>>> coll = db.get_collection('test',
|
||||
... CodecOptions(uuid_representation=STANDARD))
|
||||
>>> coll.insert({'uuid': Binary(my_uuid.bytes, 3)})
|
||||
ObjectId('...')
|
||||
>>> coll.find({'uuid': my_uuid}).count()
|
||||
|
||||
@ -27,7 +27,6 @@
|
||||
.. autoattribute:: codec_options
|
||||
.. autoattribute:: read_preference
|
||||
.. autoattribute:: write_concern
|
||||
.. autoattribute:: uuid_subtype
|
||||
.. automethod:: with_options
|
||||
.. automethod:: insert(doc_or_docs[, manipulate=True[, check_keys=True[, continue_on_error=False[, **kwargs]]]])
|
||||
.. automethod:: save(to_save[, manipulate=True[, check_keys=True[, **kwargs]]])
|
||||
|
||||
@ -26,7 +26,6 @@
|
||||
.. autoattribute:: codec_options
|
||||
.. autoattribute:: read_preference
|
||||
.. autoattribute:: write_concern
|
||||
.. autoattribute:: uuid_subtype
|
||||
|
||||
|
||||
.. autoclass:: pymongo.database.SystemJS
|
||||
|
||||
@ -32,7 +32,6 @@
|
||||
.. autoattribute:: read_preference
|
||||
.. autoattribute:: secondary_acceptable_latency_ms
|
||||
.. autoattribute:: write_concern
|
||||
.. autoattribute:: uuid_subtype
|
||||
.. autoattribute:: is_locked
|
||||
.. automethod:: database_names
|
||||
.. automethod:: drop_database
|
||||
|
||||
@ -30,7 +30,6 @@
|
||||
.. autoattribute:: read_preference
|
||||
.. autoattribute:: secondary_acceptable_latency_ms
|
||||
.. autoattribute:: write_concern
|
||||
.. autoattribute:: uuid_subtype
|
||||
.. automethod:: database_names
|
||||
.. automethod:: drop_database
|
||||
.. automethod:: get_default_database
|
||||
|
||||
@ -90,6 +90,7 @@ class Collection(common.BaseObject):
|
||||
|
||||
.. versionchanged:: 3.0
|
||||
Added the codec_options, read_preference, and write_concern options.
|
||||
Removed the uuid_subtype attribute.
|
||||
:class:`~pymongo.collection.Collection` no longer returns an
|
||||
instance of :class:`~pymongo.collection.Collection` for attribute
|
||||
names with leading underscores. You must use dict-style lookups
|
||||
|
||||
@ -27,8 +27,7 @@ from pymongo.read_preferences import (make_read_preference,
|
||||
from pymongo.ssl_support import validate_cert_reqs
|
||||
from pymongo.write_concern import WriteConcern
|
||||
from bson.binary import (STANDARD, PYTHON_LEGACY,
|
||||
JAVA_LEGACY, CSHARP_LEGACY,
|
||||
ALL_UUID_REPRESENTATIONS)
|
||||
JAVA_LEGACY, CSHARP_LEGACY)
|
||||
from bson.py3compat import string_type, integer_types
|
||||
|
||||
# Defaults until we connect to a server and get updated limits.
|
||||
@ -255,14 +254,6 @@ def validate_uuid_representation(dummy, value):
|
||||
"%s" % (value, tuple(_UUID_REPRESENTATIONS)))
|
||||
|
||||
|
||||
def validate_uuid_subtype(dummy, value):
|
||||
"""Validate the uuid subtype option, a numerical value whose acceptable
|
||||
values are defined in bson.binary."""
|
||||
if value not in ALL_UUID_REPRESENTATIONS:
|
||||
raise ConfigurationError("Not a valid setting for uuid_subtype.")
|
||||
return value
|
||||
|
||||
|
||||
def validate_read_preference_tags(name, value):
|
||||
"""Parse readPreferenceTags if passed as a client kwarg.
|
||||
"""
|
||||
@ -472,24 +463,6 @@ class BaseObject(object):
|
||||
|
||||
tag_sets = property(__get_tags, __set_tags)
|
||||
|
||||
def __get_uuid_subtype(self):
|
||||
"""This attribute specifies which BSON Binary subtype is used when
|
||||
storing UUIDs. Historically UUIDs have been stored as BSON Binary
|
||||
subtype 3. This attribute is used to switch to the newer BSON Binary
|
||||
subtype 4. It can also be used to force legacy byte order and subtype
|
||||
compatibility with the Java and C# drivers. See the :mod:`bson.binary`
|
||||
module for all options."""
|
||||
return self.__codec_options.uuid_representation
|
||||
|
||||
def __set_uuid_subtype(self, value):
|
||||
"""Sets the BSON Binary subtype to be used when storing UUIDs."""
|
||||
uuid_representation = validate_uuid_subtype("uuid_subtype", value)
|
||||
self.__codec_options = CodecOptions(self.__codec_options.as_class,
|
||||
self.__codec_options.tz_aware,
|
||||
uuid_representation)
|
||||
|
||||
uuid_subtype = property(__get_uuid_subtype, __set_uuid_subtype)
|
||||
|
||||
def _get_wc_override(self):
|
||||
"""Get write concern override.
|
||||
|
||||
|
||||
@ -28,6 +28,7 @@ from bson.binary import *
|
||||
from bson.py3compat import u
|
||||
from bson.son import SON
|
||||
from test import client_context, unittest
|
||||
from pymongo.codec_options import CodecOptions
|
||||
from pymongo.mongo_client import MongoClient
|
||||
|
||||
|
||||
@ -131,11 +132,11 @@ class TestBinary(unittest.TestCase):
|
||||
def test_legacy_java_uuid(self):
|
||||
# Test decoding
|
||||
data = self.java_data
|
||||
docs = bson.decode_all(data, SON, False, OLD_UUID_SUBTYPE)
|
||||
docs = bson.decode_all(data, SON, False, PYTHON_LEGACY)
|
||||
for d in docs:
|
||||
self.assertNotEqual(d['newguid'], uuid.UUID(d['newguidstring']))
|
||||
|
||||
docs = bson.decode_all(data, SON, False, UUID_SUBTYPE)
|
||||
docs = bson.decode_all(data, SON, False, STANDARD)
|
||||
for d in docs:
|
||||
self.assertNotEqual(d['newguid'], uuid.UUID(d['newguidstring']))
|
||||
|
||||
@ -149,11 +150,11 @@ class TestBinary(unittest.TestCase):
|
||||
|
||||
# Test encoding
|
||||
encoded = b''.join([bson.BSON.encode(doc,
|
||||
uuid_subtype=OLD_UUID_SUBTYPE)
|
||||
uuid_subtype=PYTHON_LEGACY)
|
||||
for doc in docs])
|
||||
self.assertNotEqual(data, encoded)
|
||||
|
||||
encoded = b''.join([bson.BSON.encode(doc, uuid_subtype=UUID_SUBTYPE)
|
||||
encoded = b''.join([bson.BSON.encode(doc, uuid_subtype=STANDARD)
|
||||
for doc in docs])
|
||||
self.assertNotEqual(data, encoded)
|
||||
|
||||
@ -171,15 +172,17 @@ class TestBinary(unittest.TestCase):
|
||||
docs = bson.decode_all(data, SON, False, JAVA_LEGACY)
|
||||
|
||||
client_context.client.pymongo_test.drop_collection('java_uuid')
|
||||
coll = client_context.client.pymongo_test.java_uuid
|
||||
coll.uuid_subtype = JAVA_LEGACY
|
||||
db = client_context.client.pymongo_test
|
||||
coll = db.get_collection(
|
||||
'java_uuid', CodecOptions(uuid_representation=JAVA_LEGACY))
|
||||
|
||||
coll.insert(docs)
|
||||
self.assertEqual(5, coll.count())
|
||||
for d in coll.find():
|
||||
self.assertEqual(d['newguid'], uuid.UUID(d['newguidstring']))
|
||||
|
||||
coll.uuid_subtype = OLD_UUID_SUBTYPE
|
||||
coll = db.get_collection(
|
||||
'java_uuid', CodecOptions(uuid_representation=PYTHON_LEGACY))
|
||||
for d in coll.find():
|
||||
self.assertNotEqual(d['newguid'], d['newguidstring'])
|
||||
client_context.client.pymongo_test.drop_collection('java_uuid')
|
||||
@ -188,11 +191,11 @@ class TestBinary(unittest.TestCase):
|
||||
data = self.csharp_data
|
||||
|
||||
# Test decoding
|
||||
docs = bson.decode_all(data, SON, False, OLD_UUID_SUBTYPE)
|
||||
docs = bson.decode_all(data, SON, False, PYTHON_LEGACY)
|
||||
for d in docs:
|
||||
self.assertNotEqual(d['newguid'], uuid.UUID(d['newguidstring']))
|
||||
|
||||
docs = bson.decode_all(data, SON, False, UUID_SUBTYPE)
|
||||
docs = bson.decode_all(data, SON, False, STANDARD)
|
||||
for d in docs:
|
||||
self.assertNotEqual(d['newguid'], uuid.UUID(d['newguidstring']))
|
||||
|
||||
@ -206,11 +209,11 @@ class TestBinary(unittest.TestCase):
|
||||
|
||||
# Test encoding
|
||||
encoded = b''.join([bson.BSON.encode(doc,
|
||||
uuid_subtype=OLD_UUID_SUBTYPE)
|
||||
uuid_subtype=PYTHON_LEGACY)
|
||||
for doc in docs])
|
||||
self.assertNotEqual(data, encoded)
|
||||
|
||||
encoded = b''.join([bson.BSON.encode(doc, uuid_subtype=UUID_SUBTYPE)
|
||||
encoded = b''.join([bson.BSON.encode(doc, uuid_subtype=STANDARD)
|
||||
for doc in docs])
|
||||
self.assertNotEqual(data, encoded)
|
||||
|
||||
@ -228,15 +231,17 @@ class TestBinary(unittest.TestCase):
|
||||
docs = bson.decode_all(data, SON, False, CSHARP_LEGACY)
|
||||
|
||||
client_context.client.pymongo_test.drop_collection('csharp_uuid')
|
||||
coll = client_context.client.pymongo_test.csharp_uuid
|
||||
coll.uuid_subtype = CSHARP_LEGACY
|
||||
db = client_context.client.pymongo_test
|
||||
coll = db.get_collection(
|
||||
'csharp_uuid', CodecOptions(uuid_representation=CSHARP_LEGACY))
|
||||
|
||||
coll.insert(docs)
|
||||
self.assertEqual(5, coll.count())
|
||||
for d in coll.find():
|
||||
self.assertEqual(d['newguid'], uuid.UUID(d['newguidstring']))
|
||||
|
||||
coll.uuid_subtype = OLD_UUID_SUBTYPE
|
||||
coll = db.get_collection(
|
||||
'csharp_uuid', CodecOptions(uuid_representation=PYTHON_LEGACY))
|
||||
for d in coll.find():
|
||||
self.assertNotEqual(d['newguid'], d['newguidstring'])
|
||||
client_context.client.pymongo_test.drop_collection('csharp_uuid')
|
||||
@ -245,12 +250,15 @@ class TestBinary(unittest.TestCase):
|
||||
|
||||
uri = "mongodb://foo/?uuidrepresentation=csharpLegacy"
|
||||
client = MongoClient(uri, connect=False)
|
||||
self.assertEqual(client.pymongo_test.test.uuid_subtype, CSHARP_LEGACY)
|
||||
self.assertEqual(
|
||||
client.pymongo_test.test.codec_options.uuid_representation,
|
||||
CSHARP_LEGACY)
|
||||
|
||||
@client_context.require_connection
|
||||
def test_uuid_queries(self):
|
||||
|
||||
coll = client_context.client.pymongo_test.test
|
||||
db = client_context.client.pymongo_test
|
||||
coll = db.test
|
||||
coll.drop()
|
||||
|
||||
uu = uuid.uuid4()
|
||||
@ -258,7 +266,8 @@ class TestBinary(unittest.TestCase):
|
||||
self.assertEqual(1, coll.count())
|
||||
|
||||
# Test UUIDLegacy queries.
|
||||
coll.uuid_subtype = 4
|
||||
coll = db.get_collection("test",
|
||||
CodecOptions(uuid_representation=STANDARD))
|
||||
self.assertEqual(0, coll.find({'uuid': uu}).count())
|
||||
cur = coll.find({'uuid': UUIDLegacy(uu)})
|
||||
self.assertEqual(1, cur.count())
|
||||
|
||||
@ -19,10 +19,11 @@ import uuid
|
||||
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
from bson.binary import UUIDLegacy, OLD_UUID_SUBTYPE, UUID_SUBTYPE
|
||||
from bson.binary import UUIDLegacy, PYTHON_LEGACY, STANDARD
|
||||
from bson.code import Code
|
||||
from bson.objectid import ObjectId
|
||||
from bson.son import SON
|
||||
from pymongo.codec_options import CodecOptions
|
||||
from pymongo.mongo_client import MongoClient
|
||||
from pymongo.errors import ConfigurationError, OperationFailure
|
||||
from pymongo.write_concern import WriteConcern
|
||||
@ -37,38 +38,41 @@ def setUpModule():
|
||||
|
||||
class TestCommon(IntegrationTest):
|
||||
|
||||
def test_uuid_subtype(self):
|
||||
def test_uuid_representation(self):
|
||||
coll = self.db.uuid
|
||||
coll.drop()
|
||||
|
||||
def change_subtype(collection, subtype):
|
||||
collection.uuid_subtype = subtype
|
||||
self.assertRaises(ValueError, CodecOptions, uuid_representation=7)
|
||||
self.assertRaises(ValueError, CodecOptions, uuid_representation=2)
|
||||
|
||||
# Test property
|
||||
self.assertEqual(OLD_UUID_SUBTYPE, coll.uuid_subtype)
|
||||
self.assertRaises(ConfigurationError, change_subtype, coll, 7)
|
||||
self.assertRaises(ConfigurationError, change_subtype, coll, 2)
|
||||
self.assertEqual(PYTHON_LEGACY,
|
||||
coll.codec_options.uuid_representation)
|
||||
|
||||
# Test basic query
|
||||
uu = uuid.uuid4()
|
||||
# Insert as binary subtype 3
|
||||
coll.insert({'uu': uu})
|
||||
self.assertEqual(uu, coll.find_one({'uu': uu})['uu'])
|
||||
coll.uuid_subtype = UUID_SUBTYPE
|
||||
self.assertEqual(UUID_SUBTYPE, coll.uuid_subtype)
|
||||
coll = self.db.get_collection(
|
||||
"uuid", CodecOptions(uuid_representation=STANDARD))
|
||||
self.assertEqual(STANDARD, coll.codec_options.uuid_representation)
|
||||
self.assertEqual(None, coll.find_one({'uu': uu}))
|
||||
self.assertEqual(uu, coll.find_one({'uu': UUIDLegacy(uu)})['uu'])
|
||||
|
||||
# Test Cursor.count
|
||||
self.assertEqual(0, coll.find({'uu': uu}).count())
|
||||
coll.uuid_subtype = OLD_UUID_SUBTYPE
|
||||
coll = self.db.get_collection(
|
||||
"uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
|
||||
self.assertEqual(1, coll.find({'uu': uu}).count())
|
||||
|
||||
# Test remove
|
||||
coll.uuid_subtype = UUID_SUBTYPE
|
||||
coll = self.db.get_collection(
|
||||
"uuid", CodecOptions(uuid_representation=STANDARD))
|
||||
coll.remove({'uu': uu})
|
||||
self.assertEqual(1, coll.count())
|
||||
coll.uuid_subtype = OLD_UUID_SUBTYPE
|
||||
coll = self.db.get_collection(
|
||||
"uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
|
||||
coll.remove({'uu': uu})
|
||||
self.assertEqual(0, coll.count())
|
||||
|
||||
@ -83,22 +87,26 @@ class TestCommon(IntegrationTest):
|
||||
self.assertEqual(1, coll.find_one({'_id': uu})['i'])
|
||||
|
||||
# Test update
|
||||
coll.uuid_subtype = UUID_SUBTYPE
|
||||
coll = self.db.get_collection(
|
||||
"uuid", CodecOptions(uuid_representation=STANDARD))
|
||||
coll.update({'_id': uu}, {'$set': {'i': 2}})
|
||||
coll.uuid_subtype = OLD_UUID_SUBTYPE
|
||||
coll = self.db.get_collection(
|
||||
"uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
|
||||
self.assertEqual(1, coll.find_one({'_id': uu})['i'])
|
||||
coll.update({'_id': uu}, {'$set': {'i': 2}})
|
||||
self.assertEqual(2, coll.find_one({'_id': uu})['i'])
|
||||
|
||||
# Test Cursor.distinct
|
||||
self.assertEqual([2], coll.find({'_id': uu}).distinct('i'))
|
||||
coll.uuid_subtype = UUID_SUBTYPE
|
||||
coll = self.db.get_collection(
|
||||
"uuid", CodecOptions(uuid_representation=STANDARD))
|
||||
self.assertEqual([], coll.find({'_id': uu}).distinct('i'))
|
||||
|
||||
# Test find_and_modify
|
||||
self.assertEqual(None, coll.find_and_modify({'_id': uu},
|
||||
{'$set': {'i': 5}}))
|
||||
coll.uuid_subtype = OLD_UUID_SUBTYPE
|
||||
coll = self.db.get_collection(
|
||||
"uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
|
||||
self.assertEqual(2, coll.find_and_modify({'_id': uu},
|
||||
{'$set': {'i': 5}})['i'])
|
||||
self.assertEqual(5, coll.find_one({'_id': uu})['i'])
|
||||
@ -132,7 +140,8 @@ class TestCommon(IntegrationTest):
|
||||
" return total;"
|
||||
"}")
|
||||
|
||||
coll.uuid_subtype = UUID_SUBTYPE
|
||||
coll = self.db.get_collection(
|
||||
"uuid", CodecOptions(uuid_representation=STANDARD))
|
||||
q = {"_id": uu}
|
||||
if client_context.version.at_least(1, 7, 4):
|
||||
result = coll.inline_map_reduce(map, reduce, query=q)
|
||||
@ -141,7 +150,8 @@ class TestCommon(IntegrationTest):
|
||||
result = coll.map_reduce(map, reduce, "results", query=q)
|
||||
self.assertEqual(0, self.db.results.count())
|
||||
|
||||
coll.uuid_subtype = OLD_UUID_SUBTYPE
|
||||
coll = self.db.get_collection(
|
||||
"uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
|
||||
q = {"_id": uu}
|
||||
if client_context.version.at_least(1, 7, 4):
|
||||
result = coll.inline_map_reduce(map, reduce, query=q)
|
||||
@ -158,11 +168,13 @@ class TestCommon(IntegrationTest):
|
||||
coll.insert({"_id": uuid.uuid4(), "a": 1})
|
||||
|
||||
reduce = "function (obj, prev) { prev.count++; }"
|
||||
coll.uuid_subtype = UUID_SUBTYPE
|
||||
coll = self.db.get_collection(
|
||||
"uuid", CodecOptions(uuid_representation=STANDARD))
|
||||
self.assertEqual([],
|
||||
coll.group([], {"_id": uu},
|
||||
{"count": 0}, reduce))
|
||||
coll.uuid_subtype = OLD_UUID_SUBTYPE
|
||||
coll = self.db.get_collection(
|
||||
"uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
|
||||
self.assertEqual([{"count": 1}],
|
||||
coll.group([], {"_id": uu},
|
||||
{"count": 0}, reduce))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user