mirror of
https://github.com/bspeice/metrik
synced 2025-07-01 22:07:03 -04:00
Get some tests in place for the MongoDB stuff
This commit is contained in:
25
test/mongo_test.py
Normal file
25
test/mongo_test.py
Normal file
@ -0,0 +1,25 @@
|
||||
from unittest import TestCase
|
||||
from pymongo import MongoClient
|
||||
|
||||
from metrik.conf import MONGO_DATABASE, MONGO_PORT, MONGO_HOST
|
||||
from metrik.targets.mongo_target import MongoTarget
|
||||
|
||||
|
||||
class MongoTest(TestCase):
|
||||
def tearDown(self):
|
||||
super(MongoTest, self).tearDown()
|
||||
client = MongoClient(MONGO_HOST, MONGO_PORT)
|
||||
client.drop_database(MONGO_DATABASE)
|
||||
|
||||
|
||||
class MongoTestTest(MongoTest):
|
||||
# Test that the database is dropped correctly after each test
|
||||
def test_round1(self):
|
||||
t = MongoTarget('consistent_collection', 'consistent_id')
|
||||
assert not t.exists()
|
||||
t.persist({'a': 'b'})
|
||||
|
||||
def test_round2(self):
|
||||
t = MongoTarget('consistent_collection', 'consistent_id')
|
||||
assert not t.exists()
|
||||
t.persist({'a': 'b'})
|
34
test/targets/test_mongo_target.py
Normal file
34
test/targets/test_mongo_target.py
Normal file
@ -0,0 +1,34 @@
|
||||
from pymongo import MongoClient
|
||||
from random import randint
|
||||
|
||||
from metrik.targets.mongo_target import MongoTarget
|
||||
from metrik.conf import MONGO_DATABASE, MONGO_HOST, MONGO_PORT
|
||||
from test.mongo_test import MongoTest
|
||||
|
||||
|
||||
class MongoTargetTest(MongoTest):
|
||||
|
||||
def test_exists(self):
|
||||
collection = 'test_collection'
|
||||
id = 1234
|
||||
t = MongoTarget(collection, id)
|
||||
assert not t.exists()
|
||||
|
||||
t.persist({'a': 'b'})
|
||||
assert t.exists()
|
||||
|
||||
db = MongoClient(MONGO_HOST, MONGO_PORT)[MONGO_DATABASE]
|
||||
db_collection = db[collection]
|
||||
db_collection.remove(id)
|
||||
assert not t.exists()
|
||||
|
||||
def test_persist_retrieve(self):
|
||||
collection = 'test_collection'
|
||||
id = 1234
|
||||
t = MongoTarget(collection, id)
|
||||
|
||||
d = {str(k): randint(0, 9999) for k in range(20)}
|
||||
t.persist(d)
|
||||
|
||||
u = MongoTarget(collection, id)
|
||||
assert u.retrieve() == d
|
Reference in New Issue
Block a user