1
0
mirror of https://github.com/bspeice/metrik synced 2024-12-24 22:48:10 -05:00

Add time- and version-stamps to created objects

This commit is contained in:
bspeice 2016-09-06 18:59:16 -04:00
parent 5e6bc68a33
commit eb0c96b691
4 changed files with 29 additions and 5 deletions

View File

@ -54,7 +54,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_3" default="false" assert-keyword="false" jdk-15="false" project-jdk-name="Python 2.7.12 virtualenv at ~/Development/metrik/py2-virt" project-jdk-type="Python SDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_3" default="false" assert-keyword="false" jdk-15="false" project-jdk-name="Python 3.5.1 virtualenv at C:\Users\Bradlee Speice\Development\metrik\metrik-virtual" project-jdk-type="Python SDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="PythonCompatibilityInspectionAdvertiser">

View File

@ -1,2 +1,2 @@
__version__ = '0.4.1'
__version__ = '0.4.2'
__release__ = __version__

View File

@ -4,6 +4,7 @@ from datetime import datetime
from contextlib import contextmanager
from metrik.conf import get_config
from metrik import __version__ as version
class MongoTarget(Target):
@ -29,9 +30,12 @@ class MongoTarget(Target):
'_id': self.id
}) is not None
def persist(self, dict_object):
def persist(self, dict_object, present=None):
id_dict = dict_object
id_dict['_id'] = self.id
id_dict['_metrik_version'] = version
id_dict['_created_at'] = (present if present is not None
else datetime.now())
with self.get_db() as db:
return db[self.collection].insert_one(id_dict).inserted_id

View File

@ -1,10 +1,11 @@
from pymongo import MongoClient
from random import randint
from datetime import datetime, timedelta
from metrik.targets.mongo import MongoTarget
from metrik.conf import get_config
from test.mongo_test import MongoTest
from metrik import __version__ as version
class MongoTargetTest(MongoTest):
@ -38,4 +39,23 @@ class MongoTargetTest(MongoTest):
# Make sure we can trivially scale correctly
targets = [MongoTarget('test_collection', i) for i in range(10000)]
self.assertEqual(len(targets), 10000)
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'])
one_second_past = present - timedelta(seconds=1)
t = MongoTarget('test_collection', 1235)
t.persist({}, present=one_second_past)
r = t.retrieve()
self.assertEqual(one_second_past, r['_created_at'])