Skip to content

Commit

Permalink
github_57: tests for mongo_doc_manager created with a namespace_set
Browse files Browse the repository at this point in the history
github_57: added kwargs to all DocManager constructors
  • Loading branch information
Luke Lovett committed Jan 9, 2014
1 parent bdfe9aa commit 9fe303b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
2 changes: 1 addition & 1 deletion mongo_connector/doc_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DocManager():
multiple, slightly different versions of a doc.
"""

def __init__(self, url=None, unique_key='_id'):
def __init__(self, url=None, unique_key='_id', **kwargs):
"""Creates a dictionary to hold document id keys mapped to the
documents as values.
"""
Expand Down
2 changes: 1 addition & 1 deletion mongo_connector/doc_managers/doc_manager_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DocManager():
multiple, slightly different versions of a doc.
"""

def __init__(self, url=None, unique_key='_id'):
def __init__(self, url=None, unique_key='_id', **kwargs):
"""Creates a dictionary to hold document id keys mapped to the
documents as values.
"""
Expand Down
2 changes: 1 addition & 1 deletion mongo_connector/doc_managers/sample_doc_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class DocManager():
opposed to multiple, slightly different versions of a doc.
"""

def __init__(self, url=None, auto_commit=True, unique_key='_id'):
def __init__(self, url=None, auto_commit=True, unique_key='_id', **kwargs):
"""Verify URL and establish a connection.
This method should, if necessarity, verify the url to the backend
Expand Down
63 changes: 61 additions & 2 deletions tests/test_mongo_doc_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,41 @@ def runTest(self):
unittest.TestCase.__init__(self)

@classmethod
def setUpClass(cls):
def setUpClass(cls):
start_single_mongod_instance("30000", "/MC", "MC_log")
cls.MongoDoc = DocManager("localhost:30000")
cls.mongo = Connection("localhost:30000")['test']['test']

cls.namespaces_inc = ["test.test_include1", "test.test_include2"]
cls.namespaces_exc = ["test.test_exclude1", "test.test_exclude2"]
cls.choosy_docman = DocManager(
"localhost:30000",
namespace_set=MongoDocManagerTester.namespaces_inc
)

@classmethod
def tearDownClass(cls):
def tearDownClass(cls):
kill_mongo_proc('localhost', 30000)

def setUp(self):
"""Empty Mongo at the start of every test
"""

self.mongo.remove()

conn = Connection("localhost:30000")
for ns in self.namespaces_inc + self.namespaces_exc:
db, coll = ns.split('.', 1)
conn[db][coll].remove()

def test_namespaces(self):
"""Ensure that a DocManager instantiated with a namespace set
has the correct namespaces
"""

self.assertEqual(set(self.namespaces_inc),
set(self.choosy_docman._namespaces()))

def test_upsert(self):
"""Ensure we can properly insert into Mongo via DocManager.
"""
Expand Down Expand Up @@ -115,6 +136,19 @@ def test_search(self):
self.assertTrue(search[0]['name'] == 'John')
self.assertTrue(search[1]['name'] == 'John Paul')

def test_search_namespaces(self):
"""Test search within timestamp range with a given namespace set
"""

for ns in self.namespaces_inc + self.namespaces_exc:
for i in range(100):
self.choosy_docman.upsert({"_id":i, "ns":ns, "_ts":i})

results = list(self.choosy_docman.search(0, 49))
self.assertEqual(len(results), 100)
for r in results:
self.assertIn(r["ns"], self.namespaces_inc)

def test_get_last_doc(self):
"""Insert documents, verify that get_last_doc() returns the one with
the latest timestamp.
Expand All @@ -134,5 +168,30 @@ def test_get_last_doc(self):
doc = self.MongoDoc.get_last_doc()
self.assertTrue(doc['_id'] == '6')

def test_get_last_doc_namespaces(self):
"""Ensure that get_last_doc returns the latest document in one of
the given namespaces
"""

# latest document is not in included namespace
for i in range(100):
ns = self.namespaces_inc[0] if i%2 == 0 else self.namespaces_exc[0]
self.choosy_docman.upsert({
"_id": i,
"ns": ns,
"_ts": i
})
last_doc = self.choosy_docman.get_last_doc()
self.assertEqual(last_doc["ns"], self.namespaces_inc[0])
self.assertEqual(last_doc["_id"], 98)

# remove latest document so last doc is in included namespace,
# shouldn't change result
db, coll = self.namespaces_inc[0].split(".", 1)
Connection("localhost:30000")[db][coll].remove({"_id": 99})
last_doc = self.choosy_docman.get_last_doc()
self.assertEqual(last_doc["ns"], self.namespaces_inc[0])
self.assertEqual(last_doc["_id"], 98)

if __name__ == '__main__':
unittest.main()

0 comments on commit 9fe303b

Please sign in to comment.