~chris-rogers/maus/emr_mc_digitization

« back to all changes in this revision

Viewing changes to tests/py_unit/test_docstore/test_MongoDBDocumentStore.py

Merge from jackson devel branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
Tests for MongoDBDocumentStore module.
3
 
"""
4
 
#  This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
5
 
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.
10
 
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.
15
 
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/>.
18
 
 
19
 
# pylint: disable=C0103
20
 
 
21
 
import pymongo
22
 
from pymongo.errors import AutoReconnect
23
 
import unittest
24
 
 
25
 
from docstore.MongoDBDocumentStore import MongoDBDocumentStore
26
 
 
27
 
class MongoDBDocumentStoreTestCase(unittest.TestCase): # pylint: disable=R0904, C0301
28
 
    """
29
 
    Test class for MongoDBDocumentStore module.
30
 
    """
31
 
 
32
 
    def setUp(self):
33
 
        """ 
34
 
        Create MongoDBDocumentStore with test-specific database.
35
 
        @param self Object reference.
36
 
        """
37
 
        self._host = "localhost"
38
 
        self._port = 27017
39
 
        try:
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()
49
 
        parameters = {
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)
55
 
 
56
 
    def tearDown(self):
57
 
        """
58
 
        Delete test-specific database.
59
 
        @param self Object reference.
60
 
        """
61
 
        self._data_store.disconnect()
62
 
        server = pymongo.Connection(self._host, self._port)
63
 
        server.drop_database(self._database_name)
64
 
        server.disconnect()
65
 
 
66
 
    def test_connect_no_parameters(self):
67
 
        """
68
 
        Test connect with no parameters throws a KeyError as a host
69
 
        is expected.
70
 
        @param self Object reference.
71
 
        """
72
 
        try:
73
 
            self._data_store.connect({})
74
 
        except KeyError:
75
 
            pass
76
 
 
77
 
    def test_connect_bad_host(self):
78
 
        """
79
 
        Test connect with a bad host throws an AutoReconnect error as
80
 
        expected. 
81
 
        @param self Object reference.
82
 
        """
83
 
        parameters = {"mongodb_host":"nonExistant", "mongodb_port":999999}
84
 
        try:
85
 
            self._data_store.connect(parameters)
86
 
        except AutoReconnect:
87
 
            pass
88
 
 
89
 
    def test_connect_bad_port(self):
90
 
        """
91
 
        Test connect with a bad port throws an AutoReconnect error as
92
 
        expected. 
93
 
        @param self Object reference.
94
 
        """
95
 
        parameters = {"mongodb_host":self._host, "mongodb_port":999999}
96
 
        try:
97
 
            self._data_store.connect(parameters)
98
 
        except AutoReconnect:
99
 
            pass
100
 
 
101
 
    def test_connect_no_database_name(self):
102
 
        """
103
 
        Test connect with no database name parameter throws a KeyError.
104
 
        @param self Object reference.
105
 
        """
106
 
        parameters = {
107
 
            "mongodb_host":self._host,
108
 
            "mongodb_port":self._port}
109
 
        try:
110
 
            self._data_store.connect(parameters)
111
 
        except KeyError:
112
 
            pass
113
 
 
114
 
    def test_connect_no_collection_name(self):
115
 
        """
116
 
        Test connect with no collection name parameter throws a KeyError.
117
 
        @param self Object reference.
118
 
        """
119
 
        parameters = {
120
 
            "mongodb_host":self._host,
121
 
            "mongodb_port":self._port,
122
 
            "mongodb_database_name":self._database_name}
123
 
        try:
124
 
            self._data_store.connect(parameters)
125
 
        except KeyError:
126
 
            pass
127
 
 
128
 
    def test_empty_data_store(self):
129
 
        """
130
 
        Test get, delete, clear, len, ids on an empty data store.
131
 
        @param self Object reference.
132
 
        """
133
 
        self.assertEquals(0, len(self._data_store),
134
 
            "Unexpected len")
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()
143
 
 
144
 
    def test_put_get(self):
145
 
        """
146
 
        Test put and get, and also ids and len.
147
 
        @param self Object reference.
148
 
        """
149
 
        # Insert documents.
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)
154
 
        # Validate.
155
 
        self.assertEquals(2, len(self._data_store),
156
 
            "Unexpected len")
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")
165
 
 
166
 
    def test_put_put(self):
167
 
        """
168
 
        Test put then another with the same ID.
169
 
        @param self Object reference.
170
 
        """
171
 
        # Insert document.
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")
176
 
        # Overwrite
177
 
        nudoc = {'a2':'b2', 'c2':'d2'}
178
 
        self._data_store.put("ID", nudoc)
179
 
        # Validate.
180
 
        self.assertEquals(nudoc, self._data_store.get("ID"),
181
 
            "Unexpected document for ID")
182
 
 
183
 
    def test_delete(self):
184
 
        """
185
 
        Test delete.
186
 
        @param self Object reference.
187
 
        """
188
 
        self._data_store.put("ID1", {'a1':'b1', 'c1':'d1'})
189
 
        self._data_store.put("ID2", {'a2':'b2', 'c2':'d2'})
190
 
 
191
 
        self._data_store.delete("ID1")
192
 
        self.assertEquals(1, len(self._data_store),
193
 
            "Unexpected len")
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")
197
 
 
198
 
        self._data_store.delete("ID2")
199
 
        self.assertEquals(0, len(self._data_store),
200
 
            "Unexpected len")
201
 
        ids = self._data_store.ids()
202
 
        self.assertEquals(0, len(ids), "Unexpected number of IDs")
203
 
 
204
 
    def test_clear(self):
205
 
        """
206
 
        Test clear.
207
 
        @param self Object reference.
208
 
        """
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),
213
 
            "Unexpected len")
214
 
        self.assertEquals(0, len(self._data_store.ids()), 
215
 
            "Unexpected number of IDs")
216
 
 
217
 
if __name__ == '__main__':
218
 
    unittest.main()