1919
2020sys .path [0 :0 ] = ["" ]
2121
22- from bson .binary import UUIDLegacy , OLD_UUID_SUBTYPE , UUID_SUBTYPE
22+ from bson .binary import UUIDLegacy , PYTHON_LEGACY , STANDARD
2323from bson .code import Code
2424from bson .objectid import ObjectId
2525from bson .son import SON
26+ from pymongo .codec_options import CodecOptions
2627from pymongo .mongo_client import MongoClient
2728from pymongo .errors import ConfigurationError , OperationFailure
2829from pymongo .write_concern import WriteConcern
@@ -37,38 +38,41 @@ def setUpModule():
3738
3839class TestCommon (IntegrationTest ):
3940
40- def test_uuid_subtype (self ):
41+ def test_uuid_representation (self ):
4142 coll = self .db .uuid
4243 coll .drop ()
4344
44- def change_subtype ( collection , subtype ):
45- collection . uuid_subtype = subtype
45+ self . assertRaises ( ValueError , CodecOptions , uuid_representation = 7 )
46+ self . assertRaises ( ValueError , CodecOptions , uuid_representation = 2 )
4647
4748 # Test property
48- self .assertEqual (OLD_UUID_SUBTYPE , coll .uuid_subtype )
49- self .assertRaises (ConfigurationError , change_subtype , coll , 7 )
50- self .assertRaises (ConfigurationError , change_subtype , coll , 2 )
49+ self .assertEqual (PYTHON_LEGACY ,
50+ coll .codec_options .uuid_representation )
5151
5252 # Test basic query
5353 uu = uuid .uuid4 ()
5454 # Insert as binary subtype 3
5555 coll .insert ({'uu' : uu })
5656 self .assertEqual (uu , coll .find_one ({'uu' : uu })['uu' ])
57- coll .uuid_subtype = UUID_SUBTYPE
58- self .assertEqual (UUID_SUBTYPE , coll .uuid_subtype )
57+ coll = self .db .get_collection (
58+ "uuid" , CodecOptions (uuid_representation = STANDARD ))
59+ self .assertEqual (STANDARD , coll .codec_options .uuid_representation )
5960 self .assertEqual (None , coll .find_one ({'uu' : uu }))
6061 self .assertEqual (uu , coll .find_one ({'uu' : UUIDLegacy (uu )})['uu' ])
6162
6263 # Test Cursor.count
6364 self .assertEqual (0 , coll .find ({'uu' : uu }).count ())
64- coll .uuid_subtype = OLD_UUID_SUBTYPE
65+ coll = self .db .get_collection (
66+ "uuid" , CodecOptions (uuid_representation = PYTHON_LEGACY ))
6567 self .assertEqual (1 , coll .find ({'uu' : uu }).count ())
6668
6769 # Test remove
68- coll .uuid_subtype = UUID_SUBTYPE
70+ coll = self .db .get_collection (
71+ "uuid" , CodecOptions (uuid_representation = STANDARD ))
6972 coll .remove ({'uu' : uu })
7073 self .assertEqual (1 , coll .count ())
71- coll .uuid_subtype = OLD_UUID_SUBTYPE
74+ coll = self .db .get_collection (
75+ "uuid" , CodecOptions (uuid_representation = PYTHON_LEGACY ))
7276 coll .remove ({'uu' : uu })
7377 self .assertEqual (0 , coll .count ())
7478
@@ -83,22 +87,26 @@ def change_subtype(collection, subtype):
8387 self .assertEqual (1 , coll .find_one ({'_id' : uu })['i' ])
8488
8589 # Test update
86- coll .uuid_subtype = UUID_SUBTYPE
90+ coll = self .db .get_collection (
91+ "uuid" , CodecOptions (uuid_representation = STANDARD ))
8792 coll .update ({'_id' : uu }, {'$set' : {'i' : 2 }})
88- coll .uuid_subtype = OLD_UUID_SUBTYPE
93+ coll = self .db .get_collection (
94+ "uuid" , CodecOptions (uuid_representation = PYTHON_LEGACY ))
8995 self .assertEqual (1 , coll .find_one ({'_id' : uu })['i' ])
9096 coll .update ({'_id' : uu }, {'$set' : {'i' : 2 }})
9197 self .assertEqual (2 , coll .find_one ({'_id' : uu })['i' ])
9298
9399 # Test Cursor.distinct
94100 self .assertEqual ([2 ], coll .find ({'_id' : uu }).distinct ('i' ))
95- coll .uuid_subtype = UUID_SUBTYPE
101+ coll = self .db .get_collection (
102+ "uuid" , CodecOptions (uuid_representation = STANDARD ))
96103 self .assertEqual ([], coll .find ({'_id' : uu }).distinct ('i' ))
97104
98105 # Test find_and_modify
99106 self .assertEqual (None , coll .find_and_modify ({'_id' : uu },
100107 {'$set' : {'i' : 5 }}))
101- coll .uuid_subtype = OLD_UUID_SUBTYPE
108+ coll = self .db .get_collection (
109+ "uuid" , CodecOptions (uuid_representation = PYTHON_LEGACY ))
102110 self .assertEqual (2 , coll .find_and_modify ({'_id' : uu },
103111 {'$set' : {'i' : 5 }})['i' ])
104112 self .assertEqual (5 , coll .find_one ({'_id' : uu })['i' ])
@@ -132,7 +140,8 @@ def change_subtype(collection, subtype):
132140 " return total;"
133141 "}" )
134142
135- coll .uuid_subtype = UUID_SUBTYPE
143+ coll = self .db .get_collection (
144+ "uuid" , CodecOptions (uuid_representation = STANDARD ))
136145 q = {"_id" : uu }
137146 if client_context .version .at_least (1 , 7 , 4 ):
138147 result = coll .inline_map_reduce (map , reduce , query = q )
@@ -141,7 +150,8 @@ def change_subtype(collection, subtype):
141150 result = coll .map_reduce (map , reduce , "results" , query = q )
142151 self .assertEqual (0 , self .db .results .count ())
143152
144- coll .uuid_subtype = OLD_UUID_SUBTYPE
153+ coll = self .db .get_collection (
154+ "uuid" , CodecOptions (uuid_representation = PYTHON_LEGACY ))
145155 q = {"_id" : uu }
146156 if client_context .version .at_least (1 , 7 , 4 ):
147157 result = coll .inline_map_reduce (map , reduce , query = q )
@@ -158,11 +168,13 @@ def change_subtype(collection, subtype):
158168 coll .insert ({"_id" : uuid .uuid4 (), "a" : 1 })
159169
160170 reduce = "function (obj, prev) { prev.count++; }"
161- coll .uuid_subtype = UUID_SUBTYPE
171+ coll = self .db .get_collection (
172+ "uuid" , CodecOptions (uuid_representation = STANDARD ))
162173 self .assertEqual ([],
163174 coll .group ([], {"_id" : uu },
164175 {"count" : 0 }, reduce ))
165- coll .uuid_subtype = OLD_UUID_SUBTYPE
176+ coll = self .db .get_collection (
177+ "uuid" , CodecOptions (uuid_representation = PYTHON_LEGACY ))
166178 self .assertEqual ([{"count" : 1 }],
167179 coll .group ([], {"_id" : uu },
168180 {"count" : 0 }, reduce ))
0 commit comments