2016-08-16 17:21:09 -04:00
|
|
|
from pymongo import MongoClient
|
|
|
|
from random import randint
|
2016-09-06 18:59:16 -04:00
|
|
|
from datetime import datetime, timedelta
|
2016-08-16 17:21:09 -04:00
|
|
|
|
2016-08-19 22:02:19 -04:00
|
|
|
from metrik.targets.mongo import MongoTarget
|
2016-08-24 16:09:54 -04:00
|
|
|
from metrik.conf import get_config
|
2016-08-16 17:21:09 -04:00
|
|
|
from test.mongo_test import MongoTest
|
2016-09-06 18:59:16 -04:00
|
|
|
from metrik import __version__ as version
|
2016-08-16 17:21:09 -04:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2016-08-24 16:09:54 -04:00
|
|
|
db_collection = self.db[collection]
|
2016-08-16 17:21:09 -04:00
|
|
|
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)
|
2016-09-03 09:40:12 -04:00
|
|
|
assert u.retrieve() == d
|
|
|
|
|
|
|
|
def test_scaling(self):
|
|
|
|
# There was an issue in the past where creating too many targets
|
|
|
|
# blew things up because it would allocate threads on construction.
|
|
|
|
# Make sure we can trivially scale correctly
|
|
|
|
targets = [MongoTarget('test_collection', i) for i in range(10000)]
|
|
|
|
|
2016-09-06 18:59:16 -04:00
|
|
|
self.assertEqual(len(targets), 10000)
|
|
|
|
|
|
|
|
def test_metrik_version_saved(self):
|
|
|
|
t = MongoTarget('test_collection', 1234)
|
|
|
|
t.persist({})
|
|
|
|
r = t.retrieve()
|
|
|
|
assert r['_metrik_version'] == version
|
|
|
|
|
|
|
|
def test_created_at_timestamp(self):
|
|
|
|
present = datetime.now()
|
|
|
|
t = MongoTarget('test_collection', 1234)
|
|
|
|
t.persist({})
|
|
|
|
r = t.retrieve()
|
|
|
|
self.assertGreaterEqual(present, r['_created_at'])
|
|
|
|
|
2016-09-06 21:15:36 -04:00
|
|
|
second_prior = (present - timedelta(seconds=1)).replace(microsecond=0)
|
2016-09-06 18:59:16 -04:00
|
|
|
t = MongoTarget('test_collection', 1235)
|
2016-09-06 21:15:36 -04:00
|
|
|
t.persist({}, present=second_prior)
|
2016-09-06 18:59:16 -04:00
|
|
|
r = t.retrieve()
|
2016-09-06 21:15:36 -04:00
|
|
|
self.assertEqual(second_prior, r['_created_at'])
|