4
4
import unittest
5
5
import sys
6
6
7
+ import bson
8
+ import pymongo
7
9
import six
8
10
9
11
from nanomongo .field import Field
12
14
ConfigurationError , DBRefNotSetError , ExtraFieldError , UnsupportedOperation , ValidationError ,
13
15
)
14
16
15
- try :
16
- import pymongo
17
- import bson
18
- pymongo .MongoClient ()
19
- PYMONGO_OK = True
20
- except ImportError :
21
- PYMONGO_OK = False
17
+ from . import PYMONGO_CLIENT
22
18
23
19
24
20
class DocumentTestCase (unittest .TestCase ):
@@ -161,19 +157,18 @@ class Doc(BaseDocument):
161
157
[self .assertRaises (TypeError , func ) for func in (bad_client , bad_db , bad_col )]
162
158
self .assertRaises (ConfigurationError , db_before_client )
163
159
164
- @unittest .skipUnless (PYMONGO_OK , 'pymongo not installed or connection refused' )
160
+ @unittest .skipUnless (PYMONGO_CLIENT , 'pymongo not installed or connection refused' )
165
161
def test_document_client (self ):
166
162
"""Pymongo: Test correct client input and document configuration"""
167
- mclient = pymongo .MongoClient ()
168
163
169
164
class Doc (BaseDocument ):
170
- client = mclient
165
+ client = PYMONGO_CLIENT
171
166
db = 'nanomongotest'
172
167
dot_notation = True
173
168
foo = Field (six .text_type )
174
169
175
170
class Doc2 (Doc ):
176
- client = mclient
171
+ client = PYMONGO_CLIENT
177
172
db = 'nanomongotest'
178
173
collection = 'othercollection'
179
174
dot_notation = True
@@ -186,27 +181,26 @@ class Doc3(Doc):
186
181
dd = Doc2 ()
187
182
ddd = Doc3 ()
188
183
self .assertEqual (d .nanomongo .client , dd .nanomongo .client )
189
- self .assertEqual (mclient ['nanomongotest' ], d .nanomongo .database )
190
- self .assertEqual (mclient ['nanomongotest' ], dd .nanomongo .database )
184
+ self .assertEqual (PYMONGO_CLIENT ['nanomongotest' ], d .nanomongo .database )
185
+ self .assertEqual (PYMONGO_CLIENT ['nanomongotest' ], dd .nanomongo .database )
191
186
self .assertEqual ('doc' , d .nanomongo .collection )
192
187
self .assertEqual ('othercollection' , dd .nanomongo .collection )
193
188
self .assertNotEqual (d .nanomongo .client , ddd .nanomongo .client )
194
189
self .assertFalse (hasattr (dd , 'client' ) or hasattr (dd , 'db' ) or
195
190
hasattr (dd , 'collection' ))
196
191
197
- @unittest .skipUnless (PYMONGO_OK , 'pymongo not installed or connection refused' )
192
+ @unittest .skipUnless (PYMONGO_CLIENT , 'pymongo not installed or connection refused' )
198
193
def test_document_configuration (self ):
199
194
"""Pymongo: Test document misconfiguration eg. client, db, collection"""
200
- mclient = pymongo .MongoClient ()
201
195
202
196
class Doc (BaseDocument ):
203
197
pass
204
198
205
199
class Doc2 (BaseDocument ):
206
- client = mclient
200
+ client = PYMONGO_CLIENT
207
201
208
202
class Doc3 (BaseDocument ): # autoset
209
- client = mclient
203
+ client = PYMONGO_CLIENT
210
204
db = 'nanotestdb'
211
205
212
206
self .assertRaises (ConfigurationError , Doc .get_collection )
@@ -216,34 +210,33 @@ class Doc3(BaseDocument): # autoset
216
210
self .assertRaises (ConfigurationError , Doc3 .get_collection )
217
211
# register
218
212
self .assertRaises (ConfigurationError , Doc .register )
219
- self .assertRaises (ConfigurationError , Doc .register , ** {'client' : mclient })
213
+ self .assertRaises (ConfigurationError , Doc .register , ** {'client' : PYMONGO_CLIENT })
220
214
self .assertRaises (TypeError , Doc .register ,
221
- ** {'client' : mclient , 'db' : mclient ['nanotestdb' ]})
215
+ ** {'client' : PYMONGO_CLIENT , 'db' : PYMONGO_CLIENT ['nanotestdb' ]})
222
216
self .assertFalse (Doc .nanomongo .registered )
223
- Doc .register (client = mclient , db = 'nanotestdb' )
217
+ Doc .register (client = PYMONGO_CLIENT , db = 'nanotestdb' )
224
218
self .assertTrue (Doc .nanomongo .registered )
225
219
self .assertRaises (ConfigurationError , Doc .register ,
226
- ** {'client' : mclient , 'db' : 'nanotestdb' })
220
+ ** {'client' : PYMONGO_CLIENT , 'db' : 'nanotestdb' })
227
221
m_count = len (Doc .get_collection ().database .outgoing_copying_manipulators )
228
222
self .assertEqual (1 , m_count )
229
223
230
- Doc2 .register (client = mclient , db = 'nanotestdb' , collection = 'doc2_collection' )
224
+ Doc2 .register (client = PYMONGO_CLIENT , db = 'nanotestdb' , collection = 'doc2_collection' )
231
225
232
226
233
227
class MongoDocumentTestCase (unittest .TestCase ):
234
228
def setUp (self ):
235
- pymongo . MongoClient () .drop_database ('nanotestdb' )
229
+ PYMONGO_CLIENT .drop_database ('nanotestdb' )
236
230
237
- @unittest .skipUnless (PYMONGO_OK , 'pymongo not installed or connection refused' )
231
+ @unittest .skipUnless (PYMONGO_CLIENT , 'pymongo not installed or connection refused' )
238
232
def test_insert_find (self ):
239
233
"""Pymongo: Test document insert, find, find_one"""
240
- client = pymongo .MongoClient ()
241
234
242
235
class Doc (BaseDocument ):
243
236
dot_notation = True
244
237
foo = Field (six .text_type )
245
238
bar = Field (int , required = False )
246
- Doc .register (client = client , db = 'nanotestdb' )
239
+ Doc .register (client = PYMONGO_CLIENT , db = 'nanotestdb' )
247
240
248
241
self .assertEqual (None , Doc .find_one ())
249
242
d = Doc (foo = six .u ('foo value' ))
@@ -255,17 +248,16 @@ class Doc(BaseDocument):
255
248
self .assertEqual (1 , Doc .find ({'foo' : 'foo value' }).count ())
256
249
self .assertEqual (d , Doc .find_one ())
257
250
258
- @unittest .skipUnless (PYMONGO_OK , 'pymongo not installed or connection refused' )
251
+ @unittest .skipUnless (PYMONGO_CLIENT , 'pymongo not installed or connection refused' )
259
252
def test_partial_update (self ):
260
253
"""Pymongo: Test partial atomic update with save"""
261
- client = pymongo .MongoClient ()
262
254
263
255
class Doc (BaseDocument ):
264
256
dot_notation = True
265
257
foo = Field (six .text_type )
266
258
bar = Field (int , required = False )
267
259
moo = Field (list )
268
- Doc .register (client = client , db = 'nanotestdb' )
260
+ Doc .register (client = PYMONGO_CLIENT , db = 'nanotestdb' )
269
261
270
262
d = Doc (foo = six .u ('foo value' ), bar = 42 )
271
263
self .assertRaises (ValidationError , d .save ) # no _id yet
@@ -298,17 +290,16 @@ class Doc(BaseDocument):
298
290
del d .foo
299
291
self .assertRaises (ValidationError , d .save )
300
292
301
- @unittest .skipUnless (PYMONGO_OK , 'pymongo not installed or connection refused' )
293
+ @unittest .skipUnless (PYMONGO_CLIENT , 'pymongo not installed or connection refused' )
302
294
def test_partial_update_addToSet (self ):
303
295
"""Pymongo: addToSet functionality with `save`"""
304
- client = pymongo .MongoClient ()
305
296
306
297
class Doc (BaseDocument ):
307
298
dot_notation = True
308
299
foo = Field (list )
309
300
bar = Field (dict )
310
301
moo = Field (six .text_type )
311
- Doc .register (client = client , db = 'nanotestdb' )
302
+ Doc .register (client = PYMONGO_CLIENT , db = 'nanotestdb' )
312
303
313
304
self .assertRaises (ValidationError , Doc ().addToSet , * ('$fail' , 42 ))
314
305
self .assertRaises (ValidationError , Doc ().addToSet , * ('bar.$1' , 42 ))
@@ -362,7 +353,7 @@ class Doc2(BaseDocument):
362
353
dot_notation = True
363
354
optional = Field (dict , required = False )
364
355
365
- Doc2 .register (client = client , db = 'nanotestdb' )
356
+ Doc2 .register (client = PYMONGO_CLIENT , db = 'nanotestdb' )
366
357
dd = Doc2 ()
367
358
dd .insert ()
368
359
# addToSet on unset field
@@ -372,16 +363,15 @@ class Doc2(BaseDocument):
372
363
dd .save ()
373
364
self .assertEqual (1 , Doc2 .find ({'optional.sub' : 42 }).count ())
374
365
375
- @unittest .skipUnless (PYMONGO_OK , 'pymongo not installed or connection refused' )
366
+ @unittest .skipUnless (PYMONGO_CLIENT , 'pymongo not installed or connection refused' )
376
367
def test_sub_diff (self ):
377
368
"""Test embedded document diff"""
378
- client = pymongo .MongoClient ()
379
369
380
370
class Doc (BaseDocument ):
381
371
dot_notation = True
382
372
foo = Field (six .text_type )
383
373
bar = Field (dict )
384
- Doc .register (client = client , db = 'nanotestdb' )
374
+ Doc .register (client = PYMONGO_CLIENT , db = 'nanotestdb' )
385
375
386
376
d = Doc ()
387
377
d .foo = six .u ('foo value' )
@@ -401,17 +391,16 @@ class Doc(BaseDocument):
401
391
'bar' : {'sub_b' : 'b' , 'sub_c' : 'c' }}
402
392
self .assertTrue (expected == d == Doc .find_one ())
403
393
404
- @unittest .skipUnless (PYMONGO_OK , 'pymongo not installed or connection refused' )
394
+ @unittest .skipUnless (PYMONGO_CLIENT , 'pymongo not installed or connection refused' )
405
395
def test_auto_update (self ):
406
396
"""Test auto_update keyword workings"""
407
- client = pymongo .MongoClient ()
408
397
409
398
class Doc (BaseDocument ):
410
399
dot_notation = True
411
400
foo = Field (six .text_type )
412
401
bar = Field (datetime .datetime , auto_update = True )
413
402
moo = Field (int , required = False )
414
- Doc .register (client = client , db = 'nanotestdb' )
403
+ Doc .register (client = PYMONGO_CLIENT , db = 'nanotestdb' )
415
404
416
405
dt = datetime .datetime .utcnow () - datetime .timedelta (hours = 1 )
417
406
d = Doc (foo = six .u ('foo value' ), bar = dt )
@@ -422,14 +411,13 @@ class Doc(BaseDocument):
422
411
d .save ()
423
412
self .assertNotEqual (d .bar , dt_after_insert )
424
413
425
- @unittest .skipUnless (PYMONGO_OK , 'pymongo not installed or connection refused' )
414
+ @unittest .skipUnless (PYMONGO_CLIENT , 'pymongo not installed or connection refused' )
426
415
def test_document_dbref (self ):
427
416
"""Test get_dbref functionality"""
428
- client = pymongo .MongoClient ()
429
417
430
418
class Doc (BaseDocument ):
431
419
pass
432
- Doc .register (client = client , db = 'nanotestdb' )
420
+ Doc .register (client = PYMONGO_CLIENT , db = 'nanotestdb' )
433
421
434
422
d = Doc ()
435
423
self .assertRaises (AssertionError , d .get_dbref )
@@ -439,16 +427,15 @@ class Doc(BaseDocument):
439
427
dd = d .get_collection ().database .dereference (dbref )
440
428
self .assertEqual (d ['_id' ], dd ['_id' ])
441
429
442
- @unittest .skipUnless (PYMONGO_OK , 'pymongo not installed or connection refused' )
430
+ @unittest .skipUnless (PYMONGO_CLIENT , 'pymongo not installed or connection refused' )
443
431
@unittest .skipUnless (six .PY2 , 'test irrelevant on PY3' )
444
432
def test_string_types (self ):
445
433
"""Test text type case (as mongodb-pymongo returned string type is always unicode)"""
446
- client = pymongo .MongoClient ()
447
434
448
435
class Doc (BaseDocument ):
449
436
foo = Field (six .binary_type )
450
437
bar = Field (six .text_type )
451
- Doc .register (client = client , db = 'nanotestdb' )
438
+ Doc .register (client = PYMONGO_CLIENT , db = 'nanotestdb' )
452
439
d = Doc (foo = six .binary_type ('value \xc3 \xbc ' ), bar = six .u ('value \xfc ' ))
453
440
d .insert ()
454
441
dd = Doc .find_one (d ['_id' ])
@@ -458,22 +445,21 @@ class Doc(BaseDocument):
458
445
459
446
def test_dbref_getter_methods (self ):
460
447
"""Test the creation and function of ``get_<field_name>_field`` methods"""
461
- client = pymongo .MongoClient ()
462
448
463
449
class Doc (BaseDocument ):
464
450
foo = Field (six .text_type )
465
451
self = Field (bson .DBRef , required = False , document_class = 'Doc' )
466
452
self2 = Field (bson .DBRef , required = False , document_class = 'test.test_document.Doc' )
467
453
other = Field (bson .DBRef , required = False )
468
- Doc .register (client = client , db = 'nanotestdb' )
454
+ Doc .register (client = PYMONGO_CLIENT , db = 'nanotestdb' )
469
455
# temporarily attach Doc to module so document class import can find it
470
456
sys .modules [__name__ ].Doc = Doc
471
457
472
458
# to test DBRef document class auto discover
473
459
# different naming so it wont clash with other Doc2 defined here
474
460
class XDoc2 (BaseDocument ):
475
461
pass
476
- XDoc2 .register (client = client , db = 'nanotestdb' )
462
+ XDoc2 .register (client = PYMONGO_CLIENT , db = 'nanotestdb' )
477
463
478
464
d = Doc (foo = six .text_type ('1337' ))
479
465
self .assertTrue (hasattr (d , 'get_self_field' ))
@@ -494,7 +480,7 @@ class XDoc2(BaseDocument):
494
480
# new subclass using same collection as XDoc2 to test undecided discover
495
481
class Doc3 (BaseDocument ):
496
482
pass
497
- Doc3 .register (client = client , db = 'nanotestdb' , collection = 'xdoc2' )
483
+ Doc3 .register (client = PYMONGO_CLIENT , db = 'nanotestdb' , collection = 'xdoc2' )
498
484
499
485
self .assertRaises (UnsupportedOperation , d .get_other_field )
500
486
@@ -504,12 +490,11 @@ class Doc3(BaseDocument):
504
490
505
491
class IndexTestCase (unittest .TestCase ):
506
492
def setUp (self ):
507
- if PYMONGO_OK :
508
- pymongo . MongoClient () .drop_database ('nanotestdb' )
493
+ if PYMONGO_CLIENT :
494
+ PYMONGO_CLIENT .drop_database ('nanotestdb' )
509
495
510
- @unittest .skipUnless (PYMONGO_OK , 'pymongo not installed or connection refused' )
496
+ @unittest .skipUnless (PYMONGO_CLIENT , 'pymongo not installed or connection refused' )
511
497
def test_index_definitions (self ):
512
- client = pymongo .MongoClient ()
513
498
514
499
class Doc (BaseDocument ):
515
500
dot_notation = True
@@ -521,7 +506,7 @@ class Doc(BaseDocument):
521
506
[('bar' , pymongo .ASCENDING ), ('foo' , pymongo .DESCENDING )],
522
507
unique = True ),
523
508
]
524
- Doc .register (client = client , db = 'nanotestdb' )
509
+ Doc .register (client = PYMONGO_CLIENT , db = 'nanotestdb' )
525
510
self .assertEqual (3 , len (Doc .get_collection ().index_information ())) # 2 + _id
526
511
527
512
# compare defines indexes vs indexes retured from the database
@@ -537,5 +522,5 @@ class Doc2(Doc):
537
522
__indexes__ = [
538
523
pymongo .IndexModel ('bar' ), # index test on superclass field
539
524
]
540
- Doc2 .register (client = client , db = 'nanotestdb' )
525
+ Doc2 .register (client = PYMONGO_CLIENT , db = 'nanotestdb' )
541
526
self .assertEqual (2 , len (Doc2 .get_collection ().index_information ())) # 1 + _id
0 commit comments