Skip to content

Commit 598d1d4

Browse files
committed
cleanup tests, use one MongoClient
1 parent 4e0dd40 commit 598d1d4

7 files changed

+54
-76
lines changed

requirements-test.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
coverage
44
flake8
55
mock
6+
motor
67
nose
78
tornado

requirements.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
motor
21
pymongo
32
six

test/test_document.py

+40-55
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import unittest
55
import sys
66

7+
import bson
8+
import pymongo
79
import six
810

911
from nanomongo.field import Field
@@ -12,13 +14,7 @@
1214
ConfigurationError, DBRefNotSetError, ExtraFieldError, UnsupportedOperation, ValidationError,
1315
)
1416

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
2218

2319

2420
class DocumentTestCase(unittest.TestCase):
@@ -161,19 +157,18 @@ class Doc(BaseDocument):
161157
[self.assertRaises(TypeError, func) for func in (bad_client, bad_db, bad_col)]
162158
self.assertRaises(ConfigurationError, db_before_client)
163159

164-
@unittest.skipUnless(PYMONGO_OK, 'pymongo not installed or connection refused')
160+
@unittest.skipUnless(PYMONGO_CLIENT, 'pymongo not installed or connection refused')
165161
def test_document_client(self):
166162
"""Pymongo: Test correct client input and document configuration"""
167-
mclient = pymongo.MongoClient()
168163

169164
class Doc(BaseDocument):
170-
client = mclient
165+
client = PYMONGO_CLIENT
171166
db = 'nanomongotest'
172167
dot_notation = True
173168
foo = Field(six.text_type)
174169

175170
class Doc2(Doc):
176-
client = mclient
171+
client = PYMONGO_CLIENT
177172
db = 'nanomongotest'
178173
collection = 'othercollection'
179174
dot_notation = True
@@ -186,27 +181,26 @@ class Doc3(Doc):
186181
dd = Doc2()
187182
ddd = Doc3()
188183
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)
191186
self.assertEqual('doc', d.nanomongo.collection)
192187
self.assertEqual('othercollection', dd.nanomongo.collection)
193188
self.assertNotEqual(d.nanomongo.client, ddd.nanomongo.client)
194189
self.assertFalse(hasattr(dd, 'client') or hasattr(dd, 'db') or
195190
hasattr(dd, 'collection'))
196191

197-
@unittest.skipUnless(PYMONGO_OK, 'pymongo not installed or connection refused')
192+
@unittest.skipUnless(PYMONGO_CLIENT, 'pymongo not installed or connection refused')
198193
def test_document_configuration(self):
199194
"""Pymongo: Test document misconfiguration eg. client, db, collection"""
200-
mclient = pymongo.MongoClient()
201195

202196
class Doc(BaseDocument):
203197
pass
204198

205199
class Doc2(BaseDocument):
206-
client = mclient
200+
client = PYMONGO_CLIENT
207201

208202
class Doc3(BaseDocument): # autoset
209-
client = mclient
203+
client = PYMONGO_CLIENT
210204
db = 'nanotestdb'
211205

212206
self.assertRaises(ConfigurationError, Doc.get_collection)
@@ -216,34 +210,33 @@ class Doc3(BaseDocument): # autoset
216210
self.assertRaises(ConfigurationError, Doc3.get_collection)
217211
# register
218212
self.assertRaises(ConfigurationError, Doc.register)
219-
self.assertRaises(ConfigurationError, Doc.register, **{'client': mclient})
213+
self.assertRaises(ConfigurationError, Doc.register, **{'client': PYMONGO_CLIENT})
220214
self.assertRaises(TypeError, Doc.register,
221-
**{'client': mclient, 'db': mclient['nanotestdb']})
215+
**{'client': PYMONGO_CLIENT, 'db': PYMONGO_CLIENT['nanotestdb']})
222216
self.assertFalse(Doc.nanomongo.registered)
223-
Doc.register(client=mclient, db='nanotestdb')
217+
Doc.register(client=PYMONGO_CLIENT, db='nanotestdb')
224218
self.assertTrue(Doc.nanomongo.registered)
225219
self.assertRaises(ConfigurationError, Doc.register,
226-
**{'client': mclient, 'db': 'nanotestdb'})
220+
**{'client': PYMONGO_CLIENT, 'db': 'nanotestdb'})
227221
m_count = len(Doc.get_collection().database.outgoing_copying_manipulators)
228222
self.assertEqual(1, m_count)
229223

230-
Doc2.register(client=mclient, db='nanotestdb', collection='doc2_collection')
224+
Doc2.register(client=PYMONGO_CLIENT, db='nanotestdb', collection='doc2_collection')
231225

232226

233227
class MongoDocumentTestCase(unittest.TestCase):
234228
def setUp(self):
235-
pymongo.MongoClient().drop_database('nanotestdb')
229+
PYMONGO_CLIENT.drop_database('nanotestdb')
236230

237-
@unittest.skipUnless(PYMONGO_OK, 'pymongo not installed or connection refused')
231+
@unittest.skipUnless(PYMONGO_CLIENT, 'pymongo not installed or connection refused')
238232
def test_insert_find(self):
239233
"""Pymongo: Test document insert, find, find_one"""
240-
client = pymongo.MongoClient()
241234

242235
class Doc(BaseDocument):
243236
dot_notation = True
244237
foo = Field(six.text_type)
245238
bar = Field(int, required=False)
246-
Doc.register(client=client, db='nanotestdb')
239+
Doc.register(client=PYMONGO_CLIENT, db='nanotestdb')
247240

248241
self.assertEqual(None, Doc.find_one())
249242
d = Doc(foo=six.u('foo value'))
@@ -255,17 +248,16 @@ class Doc(BaseDocument):
255248
self.assertEqual(1, Doc.find({'foo': 'foo value'}).count())
256249
self.assertEqual(d, Doc.find_one())
257250

258-
@unittest.skipUnless(PYMONGO_OK, 'pymongo not installed or connection refused')
251+
@unittest.skipUnless(PYMONGO_CLIENT, 'pymongo not installed or connection refused')
259252
def test_partial_update(self):
260253
"""Pymongo: Test partial atomic update with save"""
261-
client = pymongo.MongoClient()
262254

263255
class Doc(BaseDocument):
264256
dot_notation = True
265257
foo = Field(six.text_type)
266258
bar = Field(int, required=False)
267259
moo = Field(list)
268-
Doc.register(client=client, db='nanotestdb')
260+
Doc.register(client=PYMONGO_CLIENT, db='nanotestdb')
269261

270262
d = Doc(foo=six.u('foo value'), bar=42)
271263
self.assertRaises(ValidationError, d.save) # no _id yet
@@ -298,17 +290,16 @@ class Doc(BaseDocument):
298290
del d.foo
299291
self.assertRaises(ValidationError, d.save)
300292

301-
@unittest.skipUnless(PYMONGO_OK, 'pymongo not installed or connection refused')
293+
@unittest.skipUnless(PYMONGO_CLIENT, 'pymongo not installed or connection refused')
302294
def test_partial_update_addToSet(self):
303295
"""Pymongo: addToSet functionality with `save`"""
304-
client = pymongo.MongoClient()
305296

306297
class Doc(BaseDocument):
307298
dot_notation = True
308299
foo = Field(list)
309300
bar = Field(dict)
310301
moo = Field(six.text_type)
311-
Doc.register(client=client, db='nanotestdb')
302+
Doc.register(client=PYMONGO_CLIENT, db='nanotestdb')
312303

313304
self.assertRaises(ValidationError, Doc().addToSet, *('$fail', 42))
314305
self.assertRaises(ValidationError, Doc().addToSet, *('bar.$1', 42))
@@ -362,7 +353,7 @@ class Doc2(BaseDocument):
362353
dot_notation = True
363354
optional = Field(dict, required=False)
364355

365-
Doc2.register(client=client, db='nanotestdb')
356+
Doc2.register(client=PYMONGO_CLIENT, db='nanotestdb')
366357
dd = Doc2()
367358
dd.insert()
368359
# addToSet on unset field
@@ -372,16 +363,15 @@ class Doc2(BaseDocument):
372363
dd.save()
373364
self.assertEqual(1, Doc2.find({'optional.sub': 42}).count())
374365

375-
@unittest.skipUnless(PYMONGO_OK, 'pymongo not installed or connection refused')
366+
@unittest.skipUnless(PYMONGO_CLIENT, 'pymongo not installed or connection refused')
376367
def test_sub_diff(self):
377368
"""Test embedded document diff"""
378-
client = pymongo.MongoClient()
379369

380370
class Doc(BaseDocument):
381371
dot_notation = True
382372
foo = Field(six.text_type)
383373
bar = Field(dict)
384-
Doc.register(client=client, db='nanotestdb')
374+
Doc.register(client=PYMONGO_CLIENT, db='nanotestdb')
385375

386376
d = Doc()
387377
d.foo = six.u('foo value')
@@ -401,17 +391,16 @@ class Doc(BaseDocument):
401391
'bar': {'sub_b': 'b', 'sub_c': 'c'}}
402392
self.assertTrue(expected == d == Doc.find_one())
403393

404-
@unittest.skipUnless(PYMONGO_OK, 'pymongo not installed or connection refused')
394+
@unittest.skipUnless(PYMONGO_CLIENT, 'pymongo not installed or connection refused')
405395
def test_auto_update(self):
406396
"""Test auto_update keyword workings"""
407-
client = pymongo.MongoClient()
408397

409398
class Doc(BaseDocument):
410399
dot_notation = True
411400
foo = Field(six.text_type)
412401
bar = Field(datetime.datetime, auto_update=True)
413402
moo = Field(int, required=False)
414-
Doc.register(client=client, db='nanotestdb')
403+
Doc.register(client=PYMONGO_CLIENT, db='nanotestdb')
415404

416405
dt = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
417406
d = Doc(foo=six.u('foo value'), bar=dt)
@@ -422,14 +411,13 @@ class Doc(BaseDocument):
422411
d.save()
423412
self.assertNotEqual(d.bar, dt_after_insert)
424413

425-
@unittest.skipUnless(PYMONGO_OK, 'pymongo not installed or connection refused')
414+
@unittest.skipUnless(PYMONGO_CLIENT, 'pymongo not installed or connection refused')
426415
def test_document_dbref(self):
427416
"""Test get_dbref functionality"""
428-
client = pymongo.MongoClient()
429417

430418
class Doc(BaseDocument):
431419
pass
432-
Doc.register(client=client, db='nanotestdb')
420+
Doc.register(client=PYMONGO_CLIENT, db='nanotestdb')
433421

434422
d = Doc()
435423
self.assertRaises(AssertionError, d.get_dbref)
@@ -439,16 +427,15 @@ class Doc(BaseDocument):
439427
dd = d.get_collection().database.dereference(dbref)
440428
self.assertEqual(d['_id'], dd['_id'])
441429

442-
@unittest.skipUnless(PYMONGO_OK, 'pymongo not installed or connection refused')
430+
@unittest.skipUnless(PYMONGO_CLIENT, 'pymongo not installed or connection refused')
443431
@unittest.skipUnless(six.PY2, 'test irrelevant on PY3')
444432
def test_string_types(self):
445433
"""Test text type case (as mongodb-pymongo returned string type is always unicode)"""
446-
client = pymongo.MongoClient()
447434

448435
class Doc(BaseDocument):
449436
foo = Field(six.binary_type)
450437
bar = Field(six.text_type)
451-
Doc.register(client=client, db='nanotestdb')
438+
Doc.register(client=PYMONGO_CLIENT, db='nanotestdb')
452439
d = Doc(foo=six.binary_type('value \xc3\xbc'), bar=six.u('value \xfc'))
453440
d.insert()
454441
dd = Doc.find_one(d['_id'])
@@ -458,22 +445,21 @@ class Doc(BaseDocument):
458445

459446
def test_dbref_getter_methods(self):
460447
"""Test the creation and function of ``get_<field_name>_field`` methods"""
461-
client = pymongo.MongoClient()
462448

463449
class Doc(BaseDocument):
464450
foo = Field(six.text_type)
465451
self = Field(bson.DBRef, required=False, document_class='Doc')
466452
self2 = Field(bson.DBRef, required=False, document_class='test.test_document.Doc')
467453
other = Field(bson.DBRef, required=False)
468-
Doc.register(client=client, db='nanotestdb')
454+
Doc.register(client=PYMONGO_CLIENT, db='nanotestdb')
469455
# temporarily attach Doc to module so document class import can find it
470456
sys.modules[__name__].Doc = Doc
471457

472458
# to test DBRef document class auto discover
473459
# different naming so it wont clash with other Doc2 defined here
474460
class XDoc2(BaseDocument):
475461
pass
476-
XDoc2.register(client=client, db='nanotestdb')
462+
XDoc2.register(client=PYMONGO_CLIENT, db='nanotestdb')
477463

478464
d = Doc(foo=six.text_type('1337'))
479465
self.assertTrue(hasattr(d, 'get_self_field'))
@@ -494,7 +480,7 @@ class XDoc2(BaseDocument):
494480
# new subclass using same collection as XDoc2 to test undecided discover
495481
class Doc3(BaseDocument):
496482
pass
497-
Doc3.register(client=client, db='nanotestdb', collection='xdoc2')
483+
Doc3.register(client=PYMONGO_CLIENT, db='nanotestdb', collection='xdoc2')
498484

499485
self.assertRaises(UnsupportedOperation, d.get_other_field)
500486

@@ -504,12 +490,11 @@ class Doc3(BaseDocument):
504490

505491
class IndexTestCase(unittest.TestCase):
506492
def setUp(self):
507-
if PYMONGO_OK:
508-
pymongo.MongoClient().drop_database('nanotestdb')
493+
if PYMONGO_CLIENT:
494+
PYMONGO_CLIENT.drop_database('nanotestdb')
509495

510-
@unittest.skipUnless(PYMONGO_OK, 'pymongo not installed or connection refused')
496+
@unittest.skipUnless(PYMONGO_CLIENT, 'pymongo not installed or connection refused')
511497
def test_index_definitions(self):
512-
client = pymongo.MongoClient()
513498

514499
class Doc(BaseDocument):
515500
dot_notation = True
@@ -521,7 +506,7 @@ class Doc(BaseDocument):
521506
[('bar', pymongo.ASCENDING), ('foo', pymongo.DESCENDING)],
522507
unique=True),
523508
]
524-
Doc.register(client=client, db='nanotestdb')
509+
Doc.register(client=PYMONGO_CLIENT, db='nanotestdb')
525510
self.assertEqual(3, len(Doc.get_collection().index_information())) # 2 + _id
526511

527512
# compare defines indexes vs indexes retured from the database
@@ -537,5 +522,5 @@ class Doc2(Doc):
537522
__indexes__ = [
538523
pymongo.IndexModel('bar'), # index test on superclass field
539524
]
540-
Doc2.register(client=client, db='nanotestdb')
525+
Doc2.register(client=PYMONGO_CLIENT, db='nanotestdb')
541526
self.assertEqual(2, len(Doc2.get_collection().index_information())) # 1 + _id

test/test_document_motor.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from nanomongo.field import Field
1111
from nanomongo.document import BaseDocument
1212

13+
from . import PYMONGO_CLIENT
14+
1315
try:
1416
import motor
1517
MOTOR_CLIENT = motor.MotorClient()
@@ -23,7 +25,7 @@ class MotorDocumentTestCase(tornado.testing.AsyncTestCase):
2325

2426
def setUp(self):
2527
self.io_loop = tornado.ioloop.IOLoop.current()
26-
pymongo.MongoClient().drop_database('nanotestdb')
28+
PYMONGO_CLIENT.drop_database('nanotestdb')
2729

2830
@unittest.skipUnless(MOTOR_CLIENT, 'motor not installed or connection refused')
2931
@unittest.skipIf(SKIP_MOTOR, 'NANOMONGO_SKIP_MOTOR is set')

test/test_py3_sugar.py

+7-14
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
from nanomongo.document import BaseDocument, Field
44
from nanomongo.errors import ConfigurationError
55

6-
try:
7-
import pymongo
8-
pymongo.MongoClient()
9-
PYMONGO_OK = True
10-
except ImportError:
11-
PYMONGO_OK = False
6+
from . import PYMONGO_CLIENT
127

138

149
class PY3SugarTestCase(unittest.TestCase):
@@ -24,21 +19,19 @@ class Doc(BaseDocument, dot_notation=True):
2419
d.foo = 'bar'
2520
self.assertTrue('foo' in d and 'bar' is d.foo)
2621

27-
@unittest.skipUnless(PYMONGO_OK, 'pymongo not installed or connection refused')
22+
@unittest.skipUnless(PYMONGO_CLIENT, 'pymongo not installed or connection refused')
2823
def test_document_register_configuration(self):
2924
"""Test MongoDB client/db/collection config as class kwargs
3025
"""
31-
client = pymongo.MongoClient()
3226

33-
class Doc(BaseDocument, client=client, db='nanomongotest'):
27+
class Doc(BaseDocument, client=PYMONGO_CLIENT, db='nanomongotest'):
3428
pass
3529

36-
class Doc2(Doc, dot_notation=True, client=client, db='nanomongotest',
37-
collection='othercollection'):
30+
class Doc2(Doc, dot_notation=True, client=PYMONGO_CLIENT, db='nanomongotest', collection='othercollection'):
3831
pass
3932

4033
self.assertTrue(Doc.nanomongo.registered)
41-
self.assertEqual(client.nanomongotest.doc, Doc.get_collection())
34+
self.assertEqual(PYMONGO_CLIENT.nanomongotest.doc, Doc.get_collection())
4235
self.assertRaises(ConfigurationError, Doc.register,
43-
**{'client': client, 'db': 'nanomongotest'})
44-
self.assertEqual(client.nanomongotest.othercollection, Doc2.get_collection())
36+
**{'client': PYMONGO_CLIENT, 'db': 'nanomongotest'})
37+
self.assertEqual(PYMONGO_CLIENT.nanomongotest.othercollection, Doc2.get_collection())

0 commit comments

Comments
 (0)