def test_snapshot_list(self): a = TestLinkedList('A') b = TestLinkedList('B') c = TestLinkedList('C') a.next = b b.next = c snapshot = JsonSnapshot() self.assertIsNone(snapshot.find_entity_for_data(b)) self.assertIsNone(snapshot.find_entity_for_data(b)) # Still none snapshot.add_data(a) json_object = snapshot.to_json_object() have_b = snapshot.find_entity_for_data(b) self.assertEquals(2, have_b.id) entity_b = snapshot.make_entity_for_data(b) self.assertEquals(id(have_b), id(entity_b)) have_c = snapshot.find_entity_for_data(c) entity_c = snapshot.make_entity_for_data(c) self.assertEquals(id(have_c), id(entity_c)) self.assertEquals(3, entity_c.id) expect = { '_subject_id': 1, '_type': 'JsonSnapshot', '_entities': [ {'_id': 1, 'name': 'A', '_edges': [{'_to': 2}]}, {'_id': 2, 'name': 'B', '_edges': [{'_to': 3}]}, {'_id': 3, 'name': 'C'}]} self.assertItemsEqual(expect, json_object)
def test_lifecycle(self): """Verify we store multiple objects as a list of snapshots.""" first = TestData('first', 1, TestDetails()) second = TestData('second', 2) journal = TestJournal(StringIO()) journal.store(first) journal.store(second) journal.terminate() decoder = json.JSONDecoder(encoding='ASCII') got_stream = RecordInputStream(StringIO(journal.final_content)) got_str = [e for e in got_stream] got_json = '[{0}]'.format(','.join(got_str)) got = decoder.decode(got_json) self.assertEquals(4, len(got)) snapshot = JsonSnapshot() snapshot.add_data(first) json_object = snapshot.to_json_object() json_object['_timestamp'] = journal.clock.last_time - 1 json_object['_thread'] = threading.current_thread().ident self.assertItemsEqual(json_object, got[1]) snapshot = JsonSnapshot() snapshot.add_data(second) json_object = snapshot.to_json_object() json_object['_timestamp'] = journal.clock.last_time json_object['_thread'] = threading.current_thread().ident self.assertItemsEqual(json_object, got[2])
def test_store(self): """Verify we store objects as JSON snapshots.""" data = TestData('NAME', 1234, TestDetails()) decoder = json.JSONDecoder(encoding='ASCII') snapshot = JsonSnapshot() snapshot.add_data(data) time_function = lambda: 1.23 journal = Journal(time_function) output = StringIO() journal.open_with_file(output) offset = len(output.getvalue()) journal.store(data) contents = output.getvalue() got_stream = RecordInputStream(StringIO(contents[offset:])) got_json_str = got_stream.next() got = decoder.decode(got_json_str) json_object = snapshot.to_json_object() json_object['_timestamp'] = time_function() json_object['_thread'] = threading.current_thread().ident self.assertItemsEqual(json_object, got)