2
Tests for MongoDBDocumentStore module.
4
# This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
6
# MAUS is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
11
# MAUS is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with MAUS. If not, see <http://www.gnu.org/licenses/>.
19
# pylint: disable=C0103
22
from pymongo.errors import AutoReconnect
25
from docstore.MongoDBDocumentStore import MongoDBDocumentStore
27
class MongoDBDocumentStoreTestCase(unittest.TestCase): # pylint: disable=R0904, C0301
29
Test class for MongoDBDocumentStore module.
34
Create MongoDBDocumentStore with test-specific database.
35
@param self Object reference.
37
self._host = "localhost"
40
test_conx = pymongo.Connection(self._host, self._port)
41
except AutoReconnect: # pylint: disable=W0702
42
unittest.TestCase.skipTest(self,
43
"MongoDB server is not accessible")
44
test_conx.disconnect()
45
# Create data store and connect.
46
self._database_name = self.__class__.__name__
47
self._collection_name = self.__class__.__name__
48
self._data_store = MongoDBDocumentStore()
50
"mongodb_host":self._host,
51
"mongodb_port":self._port,
52
"mongodb_database_name":self._database_name,
53
"mongodb_collection_name":self._collection_name}
54
self._data_store.connect(parameters)
58
Delete test-specific database.
59
@param self Object reference.
61
self._data_store.disconnect()
62
server = pymongo.Connection(self._host, self._port)
63
server.drop_database(self._database_name)
66
def test_connect_no_parameters(self):
68
Test connect with no parameters throws a KeyError as a host
70
@param self Object reference.
73
self._data_store.connect({})
77
def test_connect_bad_host(self):
79
Test connect with a bad host throws an AutoReconnect error as
81
@param self Object reference.
83
parameters = {"mongodb_host":"nonExistant", "mongodb_port":999999}
85
self._data_store.connect(parameters)
89
def test_connect_bad_port(self):
91
Test connect with a bad port throws an AutoReconnect error as
93
@param self Object reference.
95
parameters = {"mongodb_host":self._host, "mongodb_port":999999}
97
self._data_store.connect(parameters)
101
def test_connect_no_database_name(self):
103
Test connect with no database name parameter throws a KeyError.
104
@param self Object reference.
107
"mongodb_host":self._host,
108
"mongodb_port":self._port}
110
self._data_store.connect(parameters)
114
def test_connect_no_collection_name(self):
116
Test connect with no collection name parameter throws a KeyError.
117
@param self Object reference.
120
"mongodb_host":self._host,
121
"mongodb_port":self._port,
122
"mongodb_database_name":self._database_name}
124
self._data_store.connect(parameters)
128
def test_empty_data_store(self):
130
Test get, delete, clear, len, ids on an empty data store.
131
@param self Object reference.
133
self.assertEquals(0, len(self._data_store),
135
self.assertEquals(0, len(self._data_store.ids()),
136
"Unexpected number of IDs")
137
self.assertEquals(None, self._data_store.get("ID"),
138
"Expected document to be None")
139
# Expect no exceptions.
140
self._data_store.delete("ID")
141
# Expect no exceptions.
142
self._data_store.clear()
144
def test_put_get(self):
146
Test put and get, and also ids and len.
147
@param self Object reference.
150
doc1 = {'a1':'b1', 'c1':'d1'}
151
self._data_store.put("ID1", doc1)
152
doc2 = {'a2':'b2', 'c2':'d2'}
153
self._data_store.put("ID2", doc2)
155
self.assertEquals(2, len(self._data_store),
157
ids = self._data_store.ids()
158
self.assertEquals(2, len(ids), "Unexpected number of IDs")
159
self.assertTrue("ID1" in ids, "ID1 was not in ids")
160
self.assertTrue("ID2" in ids, "ID2 was not in ids")
161
self.assertEquals(doc1, self._data_store.get("ID1"),
162
"Unexpected document for ID1")
163
self.assertEquals(doc2, self._data_store.get("ID2"),
164
"Unexpected document for ID2")
166
def test_put_put(self):
168
Test put then another with the same ID.
169
@param self Object reference.
172
doc = {'a1':'b1', 'c1':'d1'}
173
self._data_store.put("ID", doc)
174
self.assertEquals(doc, self._data_store.get("ID"),
175
"Unexpected document for ID")
177
nudoc = {'a2':'b2', 'c2':'d2'}
178
self._data_store.put("ID", nudoc)
180
self.assertEquals(nudoc, self._data_store.get("ID"),
181
"Unexpected document for ID")
183
def test_delete(self):
186
@param self Object reference.
188
self._data_store.put("ID1", {'a1':'b1', 'c1':'d1'})
189
self._data_store.put("ID2", {'a2':'b2', 'c2':'d2'})
191
self._data_store.delete("ID1")
192
self.assertEquals(1, len(self._data_store),
194
ids = self._data_store.ids()
195
self.assertEquals(1, len(ids), "Unexpected number of IDs")
196
self.assertTrue(not "ID1" in ids, "ID1 was not in ids")
198
self._data_store.delete("ID2")
199
self.assertEquals(0, len(self._data_store),
201
ids = self._data_store.ids()
202
self.assertEquals(0, len(ids), "Unexpected number of IDs")
204
def test_clear(self):
207
@param self Object reference.
209
self._data_store.put("ID1", {'a1':'b1', 'c1':'d1'})
210
self._data_store.put("ID2", {'a2':'b2', 'c2':'d2'})
211
self._data_store.clear()
212
self.assertEquals(0, len(self._data_store),
214
self.assertEquals(0, len(self._data_store.ids()),
215
"Unexpected number of IDs")
217
if __name__ == '__main__':