def test_mutable_edge_set_add_edge(mutable_edge_set): node1 = Node('1') node2 = Node('2') edge = Edge('e1', (node1, node2)) mutable_edge_set.add_edge(edge) assert edge in mutable_edge_set assert not mutable_edge_set.is_empty()
def test_cy_id(): start_cy_id = Edge._comparable_counter + 1 edge_list = [ Edge(str(i), (Node(str(i**i)), Node(str((i + 1) * (i - 1))))) for i in range(10) ] assert all(edge_list[i].cy_id == f'{Edge._PREFIX}_{start_cy_id + i }' for i in range(10))
def test_mutable_edge_set_remove_edge(mutable_edge_set: MutableEdgeSet): node1 = Node('1') node2 = Node('2') edge = Edge('e1', (node1, node2)) mutable_edge_set.add_edge(edge) mutable_edge_set.remove_edge(edge) assert edge not in mutable_edge_set assert mutable_edge_set.is_empty()
def test_duplicate_edges(mutable_edge_set: MutableEdgeSet): node1 = Node('1') node2 = Node('2') edge = Edge('e1', (node1, node2)) edge2 = Edge('e1', (node1, node2)) mutable_edge_set.add_edge(edge, edge2) assert edge in mutable_edge_set assert len(mutable_edge_set) == 1
def test_mutable_edge_set_add_edges(mutable_edge_set: MutableEdgeSet): edge_list = [] for i in range(10): node1 = Node(f'{i}_{i}') node2 = Node(f'{i}') edge_list.append(Edge(f'e{i}', (node1, node2))) mutable_edge_set.add_edge(*edge_list) assert all(edge in mutable_edge_set for edge in edge_list) assert len(edge_list) == len(mutable_edge_set)
def test_mutable_edge_set_remove_edges(mutable_edge_set: MutableEdgeSet): edge_list = [] for i in range(10): node2 = Node(f'{i}') node1 = Node(f'{i}_{i}') edge_list.append(Edge(f'e{i}', (node1, node2))) mutable_edge_set.add_edge(*edge_list) mutable_edge_set.remove_edge(*edge_list) assert all(edge not in mutable_edge_set for edge in edge_list) assert mutable_edge_set.is_empty()
def test_unresolvable_edge(mutable_graph: MutableGraph): n1 = Node('n1') n2 = Node('n2') edge = Edge('n1->n2', (n1, n2)) with pytest.raises(ValueError): mutable_graph.add_edge(edge=edge) with pytest.raises(ValueError): mutable_graph.add_edge('n1->n2', (n1, n2)) with pytest.raises(Exception): mutable_graph.add_edge('n1->n2', ('n1', 'n2')) with pytest.raises(TypeError): mutable_graph.add_edge('n1->n2', (True, False))
def test_mutable_node_set_remove_node(mutable_node_set): node1 = Node('1') mutable_node_set.add_node(node1) mutable_node_set.remove_node(node1) assert node1 not in mutable_node_set assert mutable_node_set.is_empty()
def test_cy_id(): start_cy_id = Node._comparable_counter + 1 node_list = [ Node(str(i)) for i in range(10) ] assert all(node_list[i].cy_id == f'{Node._PREFIX}_{start_cy_id + i}'for i in range(10))
def test_mutable_graph_delete_node_conflict_delete_nothing( mutable_graph: MutableGraph): last_num = 10 common_node = Node(f'{last_num}') mutable_graph.add_node(common_node) edge_list = [] for i in range(last_num): node = mutable_graph.add_node(f'{i}') edge_list.append( mutable_graph.add_edge(edge=Edge(i, (common_node, node)))) assert len(edge_list) == len(mutable_graph.E) assert len(mutable_graph.V) == last_num + 1 assert common_node in mutable_graph.V assert all(edge in mutable_graph.E for edge in edge_list) assert all( all(node in mutable_graph.V for node in edge) for edge in edge_list) mutable_graph.remove_node(common_node) assert len(edge_list) == len(mutable_graph.E) assert len(mutable_graph.V) == last_num + 1 assert all(edge in mutable_graph.E for edge in edge_list) assert all( all(node in mutable_graph.V for node in edge) for edge in edge_list) assert common_node in mutable_graph.V
def test_duplicate_nodes(mutable_node_set: MutableNodeSet): node1 = Node('1') mutable_node_set.add_node(node1) mutable_node_set.add_node(node1) assert node1 in mutable_node_set assert len(mutable_node_set) == 1
def test_mutable_node_set_add_nodes(mutable_node_set: MutableNodeSet): node_list = [] for i in range(10): node_list.append(Node(f'{i}')) mutable_node_set.add_node(*node_list) assert all(node in mutable_node_set for node in node_list) assert len(mutable_node_set) == len(node_list)
def test_mutable_node_set_remove_nodes(mutable_node_set): node_list = [] for i in range(10): node_list.append(Node(f'{i}')) mutable_node_set.add_node(*node_list) mutable_node_set.remove_node(*node_list) assert all(node not in mutable_node_set for node in node_list) assert mutable_node_set.is_empty()
def test_generating_changes(empty_recorder): first_line_no = 1 empty_recorder.add_record(first_line_no) change_list = empty_recorder.get_change_list() assert len(change_list) == 1 first_change = change_list[0] assert first_change[empty_recorder._LINE_HEADER] == first_line_no assert first_change[empty_recorder._VARIABLE_HEADER] is None assert first_change[empty_recorder._ACCESS_HEADER] is None first_var_id = ('main', 'var_1') first_var_id_string = empty_recorder.register_variable(first_var_id) first_var_value = 1 empty_recorder.add_vc_to_last_record(first_var_id_string, first_var_value) target_type = empty_recorder._TYPE_MAPPING[Number] assert first_change[empty_recorder._VARIABLE_HEADER] is not None assert first_change[empty_recorder._VARIABLE_HEADER][first_var_id_string][ empty_recorder._REPR_HEADER] == empty_recorder.custom_repr( first_var_value, target_type, set()) assert first_change[empty_recorder._VARIABLE_HEADER][first_var_id_string][ empty_recorder._TYPE_HEADER] == target_type accessed_var_value = Node('1') empty_recorder.add_ac_to_last_record(accessed_var_value) target_type = empty_recorder._TYPE_MAPPING[Node] assert first_change[empty_recorder._ACCESS_HEADER] is not None assert first_change[empty_recorder._ACCESS_HEADER][0][ empty_recorder._REPR_HEADER] == empty_recorder.custom_repr( accessed_var_value, target_type, set()) assert first_change[empty_recorder._ACCESS_HEADER][0][ empty_recorder._TYPE_HEADER] == target_type default_start = empty_recorder.get_processed_change_list()[0] assert default_start[empty_recorder._LINE_HEADER] == 0 assert all( item[empty_recorder._TYPE_HEADER] == empty_recorder._INIT_TYPE_STRING and item[empty_recorder._REPR_HEADER] is None and item[empty_recorder._COLOR_HEADER] is not None for item in default_start[empty_recorder._VARIABLE_HEADER].values())
def gen_edge(uid, id1, id2): return Edge(uid, (Node(id1), Node(id2)))
def test_mutable_graph_add_edge(mutable_graph: MutableGraph): mutable_graph.add_edge('1', ('n1', 'n2')) assert Edge('1', (Node('n1'), Node('n2'))) in mutable_graph assert len(mutable_graph.E) == 1
def test_mutable_graph_add_node(mutable_graph: MutableGraph): mutable_graph.add_node('1') assert Node('1') in mutable_graph assert len(mutable_graph.V) == 1
def test_simple_graph_parsing(simple_graph_js): simple_graph = Graph.graph_generator(simple_graph_js) assert len(simple_graph.edges) == 16 assert Edge(0, (Node('n0'), Node('n1'))) in simple_graph assert Edge(1, (Node('n1'), Node('n2'))) in simple_graph assert Edge(2, (Node('n1'), Node('n3'))) in simple_graph assert Edge(3, (Node('n2'), Node('n7'))) in simple_graph assert Edge(4, (Node('n2'), Node('n11'))) in simple_graph assert Edge(5, (Node('n3'), Node('n4'))) in simple_graph assert Edge(6, (Node('n3'), Node('n16'))) in simple_graph assert Edge(7, (Node('n4'), Node('n5'))) in simple_graph assert Edge(8, (Node('n4'), Node('n6'))) in simple_graph assert Edge(9, (Node('n6'), Node('n8'))) in simple_graph assert Edge(10, (Node('n8'), Node('n9'))) in simple_graph assert Edge(11, (Node('n8'), Node('n10'))) in simple_graph assert Edge(12, (Node('n11'), Node('n12'))) in simple_graph assert Edge(13, (Node('n12'), Node('n13'))) in simple_graph assert Edge(14, (Node('n13'), Node('n14'))) in simple_graph assert Edge(15, (Node('n13'), Node('n15'))) in simple_graph assert len(simple_graph.nodes) == 17 for i in range(17): assert Node('n%d' % i) in simple_graph
def test_mutable_node_set_add_node(mutable_node_set: MutableNodeSet): node1 = Node('1') mutable_node_set.add_node(node1) assert node1 in mutable_node_set assert not mutable_node_set.is_empty()
def test_multiple_node_set(multiple_nodes): node_set, _ = NodeSet.generate_node_set(multiple_nodes) assert len(node_set) == 3 assert Node('n1') in node_set assert Node('n2') in node_set assert Node('n3') in node_set
def test_single_node(single_node): node_set = Node(single_node[0]['data']['id']) assert str(node_set) == 'Node(n1)'
def test_json_dump(empty_recorder): result_string = [{ 'accesses': None, 'line': _anything, 'variables': { 'main\u200b@var_1': { 'color': '#A6CEE3', 'repr': None, 'type': 'init' }, 'main\u200b@var_2': { 'color': '#1F78B4', 'repr': None, 'type': 'init' }, 'main\u200b@var_3': { 'color': '#B2DF8A', 'repr': None, 'type': 'init' }, 'main\u200b@var_4': { 'color': '#33A02C', 'repr': None, 'type': 'init' } } }, { 'accesses': [{ 'color': '#B15928', 'id': _anything, 'properties': { 'color': '#828282', 'python_id': _anything, 'repr': [], 'type': 'Mapping' }, 'python_id': _anything, 'repr': 'Node(1)', 'type': 'Node' }], 'line': _anything, 'variables': { 'main\u200b@var_1': { 'color': '#A6CEE3', 'python_id': _anything, 'repr': '1', 'type': 'Number' }, 'main\u200b@var_2': { 'color': '#1F78B4', 'id': _anything, 'properties': { 'color': '#828282', 'python_id': _anything, 'repr': [], 'type': 'Mapping' }, 'python_id': _anything, 'repr': 'Node(1)', 'type': 'Node' }, 'main\u200b@var_3': { 'color': '#B2DF8A', 'repr': None, 'type': 'init' }, 'main\u200b@var_4': { 'color': '#33A02C', 'python_id': _anything, 'repr': 'None', 'type': 'None' } } }, { 'accesses': [{ 'color': '#B15928', 'python_id': _anything, 'repr': [{ 'key': { 'color': '#828282', 'python_id': _anything, 'repr': '1', 'type': 'Number' }, 'value': { 'color': '#828282', 'python_id': _anything, 'repr': [{ 'key': { 'color': '#828282', 'python_id': _anything, 'repr': '2', 'type': 'Number' }, 'value': { 'color': '#828282', 'python_id': _anything, 'repr': '3', 'type': 'Number' } }, { 'key': { 'color': '#828282', 'python_id': _anything, 'repr': '4', 'type': 'Number' }, 'value': { 'color': '#828282', 'python_id': _anything, 'repr': '5', 'type': 'Number' } }], 'type': 'Mapping' } }, { 'key': { 'color': '#828282', 'python_id': _anything, 'repr': [{ 'color': '#828282', 'python_id': _anything, 'repr': '6', 'type': 'Number' }, { 'color': '#828282', 'python_id': _anything, 'repr': '7', 'type': 'Number' }, { 'color': '#828282', 'python_id': _anything, 'repr': '8', 'type': 'Number' }], 'type': 'Tuple' }, 'value': { 'color': '#828282', 'python_id': _anything, 'repr': '9', 'type': 'Number' } }], 'type': 'Mapping' }], 'line': _anything, 'variables': { 'main\u200b@var_1': { 'color': '#A6CEE3', 'python_id': _anything, 'repr': '1', 'type': 'Number' }, 'main\u200b@var_2': { 'color': '#1F78B4', 'id': _anything, 'properties': { 'color': '#828282', 'python_id': _anything, 'repr': [], 'type': 'Mapping' }, 'python_id': _anything, 'repr': 'Node(1)', 'type': 'Node' }, 'main\u200b@var_3': { 'color': '#B2DF8A', 'python_id': _anything, 'repr': "'ab'", 'type': 'String' }, 'main\u200b@var_4': { 'color': '#33A02C', 'python_id': _anything, 'repr': 'None', 'type': 'None' } } }, { 'accesses': None, 'line': _anything, 'variables': None }] first_line_no = 1 empty_recorder.add_record(first_line_no) first_var_id = ('main', 'var_1') first_var_id_string = empty_recorder.register_variable(first_var_id) first_var_value = 1 empty_recorder.add_vc_to_last_record(first_var_id_string, first_var_value) second_var_id = ('main', 'var_2') second_var_id_string = empty_recorder.register_variable(second_var_id) second_var_value = Node('1') empty_recorder.add_vc_to_last_record(second_var_id_string, second_var_value) first_accessed_var_value = Node('1') empty_recorder.add_ac_to_last_record(first_accessed_var_value) second_line_no = 2 empty_recorder.add_record(second_line_no) third_var_id = ('main', 'var_3') third_var_id_string = empty_recorder.register_variable(third_var_id) third_var_value = 'abc' empty_recorder.add_vc_to_last_record(third_var_id_string, third_var_value) fourth_var_id = ('main', 'var_4') fourth_var_id_string = empty_recorder.register_variable(fourth_var_id) fourth_var_value = None empty_recorder.add_vc_to_previous_record(fourth_var_id_string, fourth_var_value) second_accessed_var_value = {1: {2: 3, 4: 5}, (6, 7, 8): 9} empty_recorder.add_ac_to_last_record(second_accessed_var_value) third_line_no = 20 empty_recorder.add_record(third_line_no) third_var_value = 'ab' empty_recorder.add_vc_to_previous_record(third_var_id_string, third_var_value) assert json.loads(empty_recorder.get_change_list_json()) == result_string
def test_process_variable_state(empty_recorder): var_id_string = empty_recorder.register_variable(('namespace', 'var')) class A: pass test_mapping = [ # simple individuals 1, Number, 1.2, Number, 'abc', str, Node('id'), Node, Edge('ide', (Node('id1'), Node('id2'))), Edge, # simple containers [1, 2], List, (3, 4), Tuple, deque([5, 6]), Deque, Counter([1, 2, 2, 3, 4, 5, 5, 5]), Counter, None, type(None), {6, 7, 8}, Set, # which includes Set, set, KeyView(dict_keys), ItemView(dict_items), # frozenset, MutableSet { 6: 7, 8: 9 }.items(), Set, { 6: 7, 8: 9 }.keys(), Set, { 10: 11, 12: 13 }, Mapping, # which includes mappingproxy (not sure what that is), MutableMapping, dict range(1, 10), Sequence, # which includes tuple, str, range, memoryview, MutableSequence, list, bytearray # wildcard A(), object ] for i in range(0, len(test_mapping), 2): state_info_mapping = empty_recorder.process_variable_state( var_id_string, test_mapping[i]) assert state_info_mapping[ empty_recorder._TYPE_HEADER] == empty_recorder._TYPE_MAPPING[ test_mapping[i + 1]]