def test__month_start_at_is_always_the_start_of_the_month(self): incoming_data_1 = {'foo': 'bar', '_timestamp': d_tz(2013, 2, 7)} incoming_data_2 = {'foo': 'bar', '_timestamp': d_tz(2013, 3, 14)} record_1 = Record(incoming_data_1) record_2 = Record(incoming_data_2) assert_that(record_1.meta["_month_start_at"], is_(d_tz(2013, 2, 1))) assert_that(record_2.meta["_month_start_at"], is_(d_tz(2013, 3, 1)))
def test_to_mongo(self): record = Record({ 'name': 'bob', '_timestamp': d_tz(2013, 4, 4, 4, 4, 4) }) assert_that(record.to_mongo(), has_key('name')) assert_that(record.to_mongo(), has_key('_timestamp')) assert_that(record.to_mongo(), has_key('_week_start_at'))
def up(db): for name in db.collection_names(): log.info("Migrating collection: {0}".format(name)) collection = db.get_repository(name) query = { "_timestamp": {"$exists": True}, "_week_start_at": {"$exists": False} } for document in collection.find(query): document['_timestamp'] = utc(document['_timestamp']) record = Record(document) collection.save(record.to_mongo())
def up(db): for name in db.collection_names(): log.info("Migrating collection: {0}".format(name)) collection = db[name] query = {"_timestamp": {"$exists": True}, "_month_start_at": {"$exists": False}} for document in collection.find(query): document["_timestamp"] = utc(document["_timestamp"]) if "_week_start_at" in document: document.pop("_week_start_at") if "_updated_at" in document: document.pop("_updated_at") record = Record(document) collection.save(record.to_mongo())
def test_that_a_list_of_records_get_sent_to_mongo_correctly(self): my_records = [ Record({'name': 'Groucho'}), Record({'name': 'Harpo'}), Record({'name': 'Chico'}) ] self.bucket.store(my_records) collection = self.mongo_collection.find() assert_that( list(collection), only_contains(has_entries({'name': 'Groucho'}), has_entries({'name': 'Harpo'}), has_entries({'name': 'Chico'})))
def test_creation(self): incoming_data = {'foo': 'bar', 'zap': 'pow'} some_record = Record(incoming_data) assert_that(some_record.data, has_key("foo")) assert_that(some_record.data, has_value("bar")) assert_that(some_record.data, has_key("zap")) assert_that(some_record.data, has_value("pow"))
def test_that_records_get_sent_to_mongo_correctly(self): my_record = Record({'foo': 'bar'}) self.bucket.store(my_record) collection = self.mongo_collection.find() assert_that(list(collection), only_contains(has_entries({"foo": "bar"})))
def test_data_with__timestamp_gets_a__period_start_ats(self): incoming_data = {'foo': 'bar', '_timestamp': d_tz(2013, 2, 2, 0, 0, 0)} some_record = Record(incoming_data) assert_that(some_record.meta["_week_start_at"], is_(d_tz(2013, 1, 28))) assert_that(some_record.meta["_month_start_at"], is_(d_tz(2013, 2, 1)))
def up(db): for name in db.collection_names(): log.info("Migrating collection: {0}".format(name)) collection = db[name] query = { "_timestamp": { "$exists": True }, "_week_start_at": { "$exists": False } } for document in collection.find(query): document['_timestamp'] = utc(document['_timestamp']) record = Record(document) collection.save(record.to_mongo())
def test__period_start_ats_get_time_zeroed(self): incoming_data = {'foo': 'bar', '_timestamp': d_tz(2013, 2, 7, 7, 7, 7)} meta_info = Record(incoming_data).meta assert_that(meta_info['_week_start_at'].time(), equal_to(datetime.time(0, 0, 0))) assert_that(meta_info['_month_start_at'].time(), equal_to(datetime.time(0, 0, 0)))
def test_data_gets_stored(self, store): self.app.post( '/foo_bucket', data='{"foo": "bar"}', content_type="application/json", headers=[('Authorization', 'Bearer foo_bucket-bearer-token')], ) store.assert_called_with([Record({"foo": "bar"})])
def test__id_gets_stored(self, store): response = self.app.post( '/foo', data='{"_id": "foo"}', content_type="application/json", headers=[('Authorization', 'Bearer foo-bearer-token')], ) assert_that(response, is_ok()) store.assert_called_with([Record({"_id": "foo"})])
def up(db): for name in db.collection_names(): log.info("Migrating collection: {0}".format(name)) collection = db.get_repository(name) query = { "_timestamp": {"$exists": True}, "_day_start_at": {"$exists": False} } is_capped = name.endswith('_realtime') if not is_capped: for document in collection.find(query): document['_timestamp'] = utc(document['_timestamp']) attrs = ['_updated_at', '_week_start_at', '_month_start_at'] for attr in attrs: if attr in document: document.pop(attr) record = Record(document) collection.save(record.to_mongo())
def test__timestamps_get_stored_as_utc_datetimes(self, store): expected_event_with_time = { u'_timestamp': datetime(2014, 1, 2, 3, 49, 0, tzinfo=pytz.utc) } self.app.post( '/bucket', data='{"_timestamp": "2014-01-02T03:49:00+00:00"}', content_type="application/json", headers=[('Authorization', 'Bearer bucket-bearer-token')], ) store.assert_called_with([Record(expected_event_with_time)])
def test_that_a_list_of_objects_get_stored(self): my_objects = [ {"name": "Groucho"}, {"name": "Harpo"}, {"name": "Chico"} ] my_records = [Record(obj) for obj in my_objects] self.bucket.store(my_records) self.mock_repository.save.assert_has_calls([ call({'name': "Groucho"}), call({"name": "Harpo"}), call({"name": "Chico"}) ])
def test_equality(self): assert_that(Record({'foo': 1}), is_(equal_to(Record({'foo': 1}))))
def test_that_a_single_object_gets_stored(self): obj = Record({"name": "Gummo"}) self.bucket.store(obj) self.mock_repository.save.assert_called_once_with({"name": "Gummo"})
def test__timestamp_is_returned_as_datetime(self): incoming_data = {'foo': 'bar', '_timestamp': d_tz(2013, 2, 2, 0, 2, 0)} some_record = Record(incoming_data) assert_that(some_record.data['_timestamp'], is_(d_tz(2013, 2, 2, 0, 2, 0)))
def test_data_without__timestamp_does_not_get__period_start_ats(self): incoming_data = {"foo": "bar"} record = Record(incoming_data) assert_that(record.meta, is_not(has_key("_week_start_at"))) assert_that(record.meta, is_not(has_key("_month_start_at")))