5
5
6
6
import pytest
7
7
8
- try :
9
- import pymongo
10
- from pymongo .errors import AutoReconnect , ConnectionFailure
11
- except ImportError :
12
- pass
13
-
14
8
from pytest_server_fixtures import CONFIG
15
9
from pytest_fixture_config import requires_config
16
10
@@ -36,11 +30,11 @@ def mongo_server(request):
36
30
37
31
For completeness, we tidy up any outstanding mongo temp directories
38
32
at the start and end of each test session
39
-
33
+
40
34
Attributes
41
35
----------
42
36
api (`pymongo.MongoClient`) : PyMongo Client API connected to this server
43
- .. also inherits all attributes from the `workspace` fixture
37
+ .. also inherits all attributes from the `workspace` fixture
44
38
"""
45
39
return _mongo_server (request )
46
40
@@ -68,6 +62,8 @@ class MongoTestServer(TestServer):
68
62
random_port = True
69
63
70
64
def __init__ (self , ** kwargs ):
65
+ global pymongo
66
+ import pymongo
71
67
mongod_dir = tempfile .mkdtemp (dir = self .get_base_dir ())
72
68
super (MongoTestServer , self ).__init__ (workspace = mongod_dir , delete = True , ** kwargs )
73
69
@@ -85,25 +81,25 @@ def get_base_dir():
85
81
@property
86
82
def run_cmd (self ):
87
83
return ['%s/mongod' % CONFIG .mongo_bin ,
88
- '--bind_ip=%s' % self .hostname ,
89
- '--port=%s' % self .port ,
90
- '--dbpath=%s' % self .workspace ,
91
- '--nounixsocket' ,
92
- '--smallfiles' ,
93
- '--syncdelay' , '0' ,
94
- '--nohttpinterface' ,
95
- '--nssize=1' ,
96
- '--nojournal' ,
97
- '--quiet' ,
98
- ]
84
+ '--bind_ip=%s' % self .hostname ,
85
+ '--port=%s' % self .port ,
86
+ '--dbpath=%s' % self .workspace ,
87
+ '--nounixsocket' ,
88
+ '--smallfiles' ,
89
+ '--syncdelay' , '0' ,
90
+ '--nohttpinterface' ,
91
+ '--nssize=1' ,
92
+ '--nojournal' ,
93
+ '--quiet' ,
94
+ ]
99
95
100
96
def check_server_up (self ):
101
97
"""Test connection to the server."""
102
98
print ("Connecting to Mongo at %s:%s" % (self .hostname , self .port ))
103
99
try :
104
100
self .api = pymongo .MongoClient (self .hostname , self .port )
105
101
return True
106
- except (AutoReconnect , ConnectionFailure ) as e :
102
+ except (pymongo . AutoReconnect , pymongo . ConnectionFailure ) as e :
107
103
print (e )
108
104
return False
109
105
0 commit comments