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(BytesIO()) journal.store(first) journal.store(second) journal.terminate() decoder = json.JSONDecoder() got_stream = RecordInputStream(BytesIO(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_object(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_object(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_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_object(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_object(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_snapshot_full_entities(self): """Test snapshotting an entity whose data are all entities.""" snapshot = JsonSnapshot(title='Test Graph') entity_a = snapshot.new_entity(name='Entity Name') expect_a = {'_id': 1, 'name': 'Entity Name'} entity_b = snapshot.new_entity(data=1234, type='scalar') expect_b = {'_id': 2, 'data': 1234, 'type': 'scalar'} entity_c = snapshot.new_entity(data=3.14, type='real') expect_c = {'_id': 3, 'data': 3.14, 'type': 'real'} snapshot.edge_builder.make(entity_a, 'A to B', entity_b) expect_ab = {'_to': entity_b.id, 'label': 'A to B'} snapshot.edge_builder.make(entity_a, 'A to C', entity_c) expect_ac = {'_to': entity_c.id, 'label': 'A to C'} expect_a['_edges'] = [expect_ab, expect_ac] expect = {'_subject_id': 1, '_type': 'JsonSnapshot', '_entities': [expect_a, expect_b, expect_c], 'title': 'Test Graph'} json_obj = snapshot.to_json_object() self.assertItemsEqual(expect, json_obj)
def test_snapshot_make_transitive_entity(self): """Test snapshotting compound entities.""" elem = TestLinkedList('First', next_elem=TestLinkedList('Second')) snapshot = JsonSnapshot() snapshot.make_entity_for_object(elem) entity = snapshot.make_entity_for_object(elem.next) found = snapshot.find_entity_for_object(elem.next) self.assertEquals(found, entity) self.assertEquals(id(found), id(entity)) self.assertItemsEqual( { '_subject_id': 1, '_type': 'JsonSnapshot', '_entities': [{ '_id': 1, 'name': 'First', '_edges': [{ '_to': 2 }] }, { '_id': 2, 'name': 'Second' }] }, snapshot.to_json_object())
def test_snapshot_full_entities(self): """Test snapshotting an entity whose data are all entities.""" snapshot = JsonSnapshot(title='Test Graph') entity_a = snapshot.new_entity(name='Entity Name') expect_a = {'_id': 1, 'name': 'Entity Name'} entity_b = snapshot.new_entity(data=1234, type='scalar') expect_b = {'_id': 2, 'data': 1234, 'type': 'scalar'} entity_c = snapshot.new_entity(data=3.14, type='real') expect_c = {'_id': 3, 'data': 3.14, 'type': 'real'} snapshot.edge_builder.make(entity_a, 'A to B', entity_b) expect_ab = {'_to': entity_b.id, 'label': 'A to B'} snapshot.edge_builder.make(entity_a, 'A to C', entity_c) expect_ac = {'_to': entity_c.id, 'label': 'A to C'} expect_a['_edges'] = [expect_ab, expect_ac] expect = { '_subject_id': 1, '_type': 'JsonSnapshot', '_entities': [expect_a, expect_b, expect_c], 'title': 'Test Graph' } json_obj = snapshot.to_json_object() self.assertItemsEqual(expect, json_obj)
def test_snapshot_edge_builder(self): """Test focused on snapshot.edge_builder property.""" snapshot = JsonSnapshot(title='Test Snapshot') entity_a = snapshot.new_entity(name='Entity A') entity_b = snapshot.new_entity(name='Entity B') expect_a = {'_id': 1, 'name': 'Entity A'} expect_edges = [] snapshot.edge_builder.make_mechanism(entity_a, 'B', entity_b) expect_edges.append({'_to': 2, 'label': 'B', 'relation': 'MECHANISM'}) snapshot.edge_builder.make_control(entity_a, 'field', -321) expect_edges.append({ '_value': -321, 'label': 'field', 'relation': 'CONTROL' }) expect = { '_subject_id': 1, '_type': 'JsonSnapshot', '_entities': [expect_a], 'title': 'Test Snapshot' } json_obj = snapshot.to_json_object() self.assertItemsEqual(expect, json_obj)
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_object(b)) self.assertIsNone(snapshot.find_entity_for_object(b)) # Still none snapshot.add_object(a) json_object = snapshot.to_json_object() have_b = snapshot.find_entity_for_object(b) self.assertEquals(2, have_b.id) entity_b = snapshot.make_entity_for_object(b) self.assertEquals(id(have_b), id(entity_b)) have_c = snapshot.find_entity_for_object(c) entity_c = snapshot.make_entity_for_object(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_process_snapshot(self): """Test the conversion of a snapshot into HTML.""" tail = TestLinkedList(name='tail') head = TestLinkedList(name='head', next_elem=tail) snapshot = JsonSnapshot() snapshot.make_entity_for_object(head) json_snapshot = snapshot.to_json_object() entity_manager = ProcessedEntityManager() processor = ProcessToRenderInfo(HtmlDocumentManager('test_json'), entity_manager) processor.max_uncollapsable_json_lines = 20 processor.max_uncollapsable_metadata_rows = 20 processor.max_uncollapsable_entity_rows = 20 processor.default_force_top_level_collapse = False in_relation = None entity_manager.push_entity_map(json_snapshot['_entities']) html_info = processor.process_entity_id(json_snapshot['_subject_id'], json_snapshot, in_relation) entity_manager.pop_entity_map(json_snapshot['_entities']) expect = """<table> <tr> <th><i>metadata</i></th> <td> <table> <tr><th>_id</th><td>1</td></tr> <tr><th>class</th><td>type TestLinkedList</td></tr> <tr><th>name</th><td>head</td></tr> </table> </td> </tr> <tr> <th>Next</th> <td> <table> <tr> <th><i>metadata</i></th> <td> <table> <tr><th>_id</th><td>2</td></tr> <tr><th>class</th><td>type TestLinkedList</td></tr> <tr><th>name</th><td>tail</td></tr> </table> </td> </tr> </table> </td> </tr> </table> """ # Test without regard to whitespace formatting. self.assertEquals(''.join(expect.split()), ''.join(str(html_info.detail_block).split()))
def test_process_snapshot(self): """Test the conversion of a snapshot into HTML.""" tail = TestLinkedList(name='tail') head = TestLinkedList(name='head', next_elem=tail) snapshot = JsonSnapshot() snapshot.make_entity_for_object(head) json_snapshot = snapshot.to_json_object() entity_manager = ProcessedEntityManager() processor = ProcessToRenderInfo( HtmlDocumentManager('test_json'), entity_manager) processor.max_uncollapsable_json_lines = 20 processor.max_uncollapsable_metadata_rows = 20 processor.max_uncollapsable_entity_rows = 20 processor.default_force_top_level_collapse = False in_relation = None entity_manager.push_entity_map(json_snapshot['_entities']) html_info = processor.process_entity_id(json_snapshot['_subject_id'], json_snapshot, in_relation) entity_manager.pop_entity_map(json_snapshot['_entities']) expect = """<table> <tr> <th><i>metadata</i></th> <td> <table> <tr><th>_id</th><td>1</td></tr> <tr><th>class</th><td>type TestLinkedList</td></tr> <tr><th>name</th><td>head</td></tr> </table> </td> </tr> <tr> <th>Next</th> <td> <table> <tr> <th><i>metadata</i></th> <td> <table> <tr><th>_id</th><td>2</td></tr> <tr><th>class</th><td>type TestLinkedList</td></tr> <tr><th>name</th><td>tail</td></tr> </table> </td> </tr> </table> </td> </tr> </table> """ # Test without regard to whitespace formatting. self.assertEquals(''.join(expect.split()), ''.join(str(html_info.detail_block).split()))
def test_snapshot_make_entity(self): """Test snapshotting JsonSnapshotableEntity objects into entities.""" elem = TestLinkedList('Hello') snapshot = JsonSnapshot() entity = snapshot.make_entity_for_object(elem) found = snapshot.find_entity_for_object(elem) self.assertEquals(found, entity) self.assertEquals(id(found), id(entity)) self.assertItemsEqual({'_subject_id': 1, '_type': 'JsonSnapshot', '_entities': [{'_id': 1, 'name': 'Hello'}]}, snapshot.to_json_object())
def test_snapshot_make_transitive_entity(self): """Test snapshotting compound entities.""" elem = TestLinkedList('First', next_elem=TestLinkedList('Second')) snapshot = JsonSnapshot() snapshot.make_entity_for_object(elem) entity = snapshot.make_entity_for_object(elem.next) found = snapshot.find_entity_for_object(elem.next) self.assertEquals(found, entity) self.assertEquals(id(found), id(entity)) self.assertItemsEqual({'_subject_id': 1, '_type': 'JsonSnapshot', '_entities': [{'_id': 1, 'name': 'First', '_edges':[{'_to': 2}]}, {'_id': 2, 'name': 'Second'}]}, snapshot.to_json_object())
def test_cycle(self): tail = TestLinkedList(name='tail') head = TestLinkedList(name='head', next_elem=tail) tail.next = head snapshot = JsonSnapshot() entity_manager = ProcessedEntityManager() processor = ProcessToRenderInfo(HtmlDocumentManager('test_json'), entity_manager) snapshot.make_entity_for_object(head) json_snapshot = snapshot.to_json_object() self.assertEqual(1, json_snapshot.get('_subject_id')) entity_manager.push_entity_map(json_snapshot.get('_entities')) info = processor.process_entity_id(1, snapshot)
def test_cycle(self): tail = TestLinkedList(name='tail') head = TestLinkedList(name='head', next_elem=tail) tail.next = head snapshot = JsonSnapshot() entity_manager = ProcessedEntityManager() processor = ProcessToRenderInfo( HtmlDocumentManager('test_json'), entity_manager) snapshot.make_entity_for_data(head) json_snapshot = snapshot.to_json_object() self.assertEqual(1, json_snapshot.get('_subject_id')) entity_manager.push_entity_map(json_snapshot.get('_entities')) info = processor.process_entity_id(1, snapshot)
def test_to_json_snapshot_value(self): """Test conversion of values.""" snapshot = JsonSnapshot() # Primitive types dont affect snapshot. self.assertEquals(2, JsonSnapshotHelper.ToJsonSnapshotValue(2, snapshot)) self.assertEquals( 2.0, JsonSnapshotHelper.ToJsonSnapshotValue(2.0, snapshot)) self.assertEquals( '2', JsonSnapshotHelper.ToJsonSnapshotValue('2', snapshot)) self.assertEquals( [1, 2, 3], JsonSnapshotHelper.ToJsonSnapshotValue([1, 2, 3], snapshot)) d = {'a': 'A', 'b': 2, 'c': [1, 2, 3]} self.assertEquals( d, JsonSnapshotHelper.ToJsonSnapshotValue(d, snapshot)) self.assertEquals({'_type': 'JsonSnapshot'}, snapshot.to_json_object()) # Wrapped values dont affect snapshot self.assertEquals( d, JsonSnapshotHelper.ToJsonSnapshotValue(TestWrappedValue(d), snapshot)) self.assertEquals({'_type': 'JsonSnapshot'}, snapshot.to_json_object()) # types dont affect snapshot self.assertEquals( 'type dict', JsonSnapshotHelper.ToJsonSnapshotValue(dict, snapshot)) self.assertEquals({'_type': 'JsonSnapshot'}, snapshot.to_json_object()) # methods dont affect snapshot self.assertEquals( 'Method "test_to_json_snapshot_value"', JsonSnapshotHelper.ToJsonSnapshotValue( self.test_to_json_snapshot_value, snapshot)) self.assertEquals({'_type': 'JsonSnapshot'}, snapshot.to_json_object()) # exceptions dont affect snapshot self.assertEquals( 'KeyError: \'Hello, World\'', JsonSnapshotHelper.ToJsonSnapshotValue(KeyError('Hello, World'), snapshot)) self.assertEquals({'_type': 'JsonSnapshot'}, snapshot.to_json_object()) # JsonSnapshotableEntity definition written into snapshot ll = TestLinkedList('X') ref = JsonSnapshotHelper.ToJsonSnapshotValue(ll, snapshot) self.assertEquals({'_type': 'EntityReference', '_id': 1}, ref) expect = {'_type': 'JsonSnapshot', '_subject_id': 1, '_entities': {1: {'_id': 1, 'class': 'type TestLinkedList', 'name': 'X'}} } self.assertEquals(expect, snapshot.to_json_object()) self.assertEquals(1, snapshot.find_entity_for_object(ll).id)
def test_snapshot_fields(self): """Test snapshotting an entity whose data are simple values.""" snapshot = JsonSnapshot(title='Test Snapshot') entity_a = snapshot.new_entity(name='Entity Name') expect_a = {'_id': 1, 'name': 'Entity Name'} expect_edges = [] snapshot.edge_builder.make(entity_a, 'scalar', 1234) expect_edges.append({'_value': 1234, 'label': 'scalar'}) snapshot.edge_builder.make(entity_a, 'real', 3.14) expect_edges.append({'_value': 3.14, 'label': 'real'}) expect = {'_subject_id': 1, '_type': 'JsonSnapshot', '_entities': [expect_a], 'title': 'Test Snapshot'} json_obj = snapshot.to_json_object() self.assertItemsEqual(expect, json_obj)
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_object(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)
def test_store(self): """Verify we store objects as JSON snapshots.""" data = TestData('NAME', 1234, TestDetails()) decoder = json.JSONDecoder() snapshot = JsonSnapshot() snapshot.add_object(data) time_function = lambda: 1.23 journal = Journal(time_function) output = BytesIO() journal.open_with_file(output) offset = len(output.getvalue()) journal.store(data) contents = output.getvalue() got_stream = RecordInputStream(BytesIO(contents[offset:])) got_json_str = next(got_stream) 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)
def test_snapshot_edge_builder(self): """Test focused on snapshot.edge_builder property.""" snapshot = JsonSnapshot(title='Test Snapshot') entity_a = snapshot.new_entity(name='Entity A') entity_b = snapshot.new_entity(name='Entity B') expect_a = {'_id': 1, 'name': 'Entity A'} expect_edges = [] snapshot.edge_builder.make_mechanism(entity_a, 'B', entity_b) expect_edges.append({'_to': 2, 'label': 'B', 'relation': 'MECHANISM'}) snapshot.edge_builder.make_control(entity_a, 'field', -321) expect_edges.append({'_value': -321, 'label': 'field', 'relation': 'CONTROL'}) expect = {'_subject_id': 1, '_type': 'JsonSnapshot', '_entities': [expect_a], 'title': 'Test Snapshot'} json_obj = snapshot.to_json_object() self.assertItemsEqual(expect, json_obj)