From eb0c96b6915d426e55080ca2beb44dca72329ea4 Mon Sep 17 00:00:00 2001 From: bspeice Date: Tue, 6 Sep 2016 18:59:16 -0400 Subject: [PATCH] Add time- and version-stamps to created objects --- .idea/misc.xml | 2 +- metrik/__init__.py | 2 +- metrik/targets/mongo.py | 6 +++++- test/targets/test_mongo_target.py | 24 ++++++++++++++++++++++-- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index e659c40..fcf3066 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -54,7 +54,7 @@ - + diff --git a/metrik/__init__.py b/metrik/__init__.py index 99aa587..ba0e4af 100644 --- a/metrik/__init__.py +++ b/metrik/__init__.py @@ -1,2 +1,2 @@ -__version__ = '0.4.1' +__version__ = '0.4.2' __release__ = __version__ \ No newline at end of file diff --git a/metrik/targets/mongo.py b/metrik/targets/mongo.py index 9b1045a..d2064ce 100644 --- a/metrik/targets/mongo.py +++ b/metrik/targets/mongo.py @@ -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 diff --git a/test/targets/test_mongo_target.py b/test/targets/test_mongo_target.py index 7fc1779..9814147 100644 --- a/test/targets/test_mongo_target.py +++ b/test/targets/test_mongo_target.py @@ -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) \ No newline at end of file + 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']) \ No newline at end of file