def assert_edge_as_obj_added(el: Jelm): n = el.get_node("n1") assert "n2" in n.neighbors.keys() assert "n1" in el.get_node("n2").neighbors assert "n2" in n.target_neighbors.keys()
def __init__(self): node_list1 = [{ "type": "node", "id": "n{}".format(i) } for i in range(20)] edge_list1 = [{ "type": "edge", "source": "n{}".format(ns[0]), "target": "n{}".format(ns[1]), } for ns in [(0, 1), (1, 2), (4, 1), (10, 8), (9, 3), (18, 6)]] self.el1 = Jelm(objects=[*node_list1, *edge_list1]) node_list2 = [{ "type": "node", "id": "n{}".format(i) } for i in range(20)] edge_list2 = [{ "type": "edge", "source": "n{}".format(ns[0]), "target": "n{}".format(ns[1]), "attributes": { "second": True }, } for ns in [(10, 8), (1, 2), (0, 1), (9, 3), (18, 6), (4, 1)]] self.el2 = Jelm(objects=[*node_list2, *edge_list2]) self.same = False
def assert_edge_jelm_obj_added(el: Jelm): n = el.get_node("n1") assert "n2" in n.neighbors.keys() assert "n1" in el.get_node("n2").neighbors assert "n2" in n.target_neighbors.keys() edge_ids = [e.id for e in n.neighbors["n2"]] assert "fing" in edge_ids
def __init__(self): self.el1 = Jelm(objects=[{ "type": "node", "id": "n{}".format(i) } for i in range(20)]) self.el2 = Jelm(objects=[{ "type": "node", "id": "n{}".format(i) } for i in range(20)]) self.same = True
def __init__(self): self.el1 = Jelm(objects=[{ "type": "node", "id": "n1", "attributes": { "fing": True } }]) self.el2 = Jelm(objects=[{"type": "node", "id": "n1"}]) self.same = False
def __init__(self): self.el1 = Jelm(objects=[{"type": "node", "id": "n1"}]) self.el2 = Jelm(objects=[{ "type": "node", "id": "n1" }, { "type": "node", "id": "n2" }]) self.same = False
def neighbor_check(el: Jelm): for nid, n in el.nodes.items(): for nid2 in n.neighbors.keys(): assert nid in el.get_node(nid2).neighbors.keys() for nid3 in n.target_neighbors.keys(): assert nid in el.get_node(nid3).source_neighbors.keys() return el
def test_neighborhood(): el1 = Jelm(metadata={"fing": "boo"}, objects=[{ "type": "node", "id": "n1" }]) el2 = el1.get_neighborhood([]) assert el2 != el1 assert el2 == Jelm(metadata={"fing": "boo"}) assert el1 == el1.get_neighborhood(["n1", "n2"], raise_if_missing=False)
def transform_init(el): el_from_nodes = Jelm(metadata=el.metadata, nodes=el.nodes) assert el_from_nodes == el return el_from_nodes
def filter_none(el: Jelm): el2 = el.attribute_filter(lambda x: True) print(el) print(el2) print(el == el2) return el2
def repr_check(el: Jelm): repr_string = el.__repr__() assert "jelm" in repr_string assert str(len(el.nodes.keys())) in repr_string return el
def full_jelm_exporter(): django_dump = get_django_dump_of_app("teach") el = Jelm(metadata={"created": datetime.datetime.now().isoformat()}) plus_edges = [] for django_record_dic in django_dump: record_inst = DjangoRecord.from_django_dump_record(django_record_dic) record_inst.add_jelm_nodes_to_instance(el) plus_edges += record_inst.jelm_edges() for e in plus_edges: el.add_object(e) return el.dict()
def test_init(): el = Jelm(metadata={"author": "John Doe"}, objects=[]) assert isinstance(el.objects, list) assert isinstance(el.metadata, dict) el2 = Jelm(metadata={"author": "John Doe"}, nodes={}) assert el == el2 el3 = Jelm() assert not (el == el3) el4_1 = Jelm(nodes={"id1": Node(id="n1")}) el4_2 = Jelm(objects=[{"type": "node", "id": "n1"}]) assert el4_1 == el4_2
def add_jelm_nodes_to_instance(self, el: Jelm): record_atts = self.attributes.copy() if not self.teach_jelm_cls.is_edge: for add_edge_dic in self.teach_jelm_cls.additional_edges: attid = add_edge_dic["attid"] try: record_atts.pop(attid) except KeyError as e: pass el.add_node( id=self.jelm_id, attributes={ "pk": self.django_id, "model": self.model_name, **record_atts, }, )
class NetwokCaseTemplate: # make this to a fixture el1 = Jelm() el2 = Jelm() same = True def evaluate_fun( self, non_altering_function: Optional[Callable] = None, altering_function: Optional[Callable] = None, assert_alteration: Optional[Callable] = None, catch_alteration_exception: Optional[Callable] = None, ): el_in = self.el1.copy() if altering_function is not None: try: el_out = altering_function(el_in) except Exception as e: if catch_alteration_exception is None: raise e # pragma: no cover else: catch_alteration_exception(el_in, e) else: assert el_out != self.el1 assert_alteration(el_out) if non_altering_function is not None: el_out = non_altering_function(el_in) assert el_out == self.el1 assert (el_out == self.el2) == self.same def __repr__(self): return "Network pair case: {}".format( type(self).__name__) # pragma: no cover
def test_iter(): el = Jelm( metadata={"author": "John Doe"}, objects=[ {"type": "node", "id": "n1"}, {"type": "node", "id": "n2"}, {"type": "edge", "source": "n1", "target": "n2"}, ], ) for idx, o in enumerate(el): if idx < 2: assert isinstance(o, Node) else: assert isinstance(o, Edge)
def __init__(self): node_list1 = [{ "type": "node", "id": "n{}".format(i) } for i in range(20)] edge_list1 = [{ "type": "edge", "source": "n{}".format(ns[0]), "target": "n{}".format(ns[1]), } for ns in [(0, 1), (1, 2), (4, 1), (10, 8), (9, 3), (18, 6)]] self.el1 = Jelm(objects=[*node_list1, *edge_list1]) self.el2 = self.el1 self.same = True
def __init__(self, node_count=200, edge_count=500): import random random.seed(42) node_list1 = [{ "type": "node", "id": "n{}".format(i) } for i in range(node_count)] edge_list1 = [ { "type": "edge", "source": "n{}".format(ns[0]), "target": "n{}".format(ns[1]), } for ns in [random.sample(range(node_count), 2) for _ in range(edge_count)] ] self.el1 = Jelm(objects=[*node_list1, *edge_list1]) self.el2 = self.el1 self.same = True
def filter_none(el: Jelm): el.attribute_filter(lambda x: True) return el
def __init__(self): self.el1 = Jelm() self.el2 = Jelm() self.same = True
def add_node_as_obj(el: Jelm): el.add_object({"type": "node", "id": "n10"}) return el
def __init__(self): self.el1 = Jelm(metadata={"author": "John Doe"}) self.el2 = Jelm(metadata={"author": "Jane Doe"}) self.same = False
def add_edge_jelm_obj(el: Jelm): el.add_object(Edge(source="n1", target="n2", id="fing")) return el
def assert_node_as_obj_added(el: Jelm): assert isinstance(el.get_node("n10"), Node)
def catch_node_as_obj_add(el: Jelm, e): assert isinstance(e, ValueError) assert isinstance(el.get_node("n10"), Node)
def check_drop(el: Jelm): nel = el.get_neighborhood(["n1", "n2"])
def add_edge_as_obj(el: Jelm): el.add_object({"type": "edge", "source": "n1", "target": "n2"}) return el
def test_attribute_filter(): node_list1 = [{"type": "node", "id": "n{}".format(i)} for i in range(1, 4)] edge_list1 = [ { "type": "edge", "source": "n1", "target": "n2", "attributes": { "good_edge": True }, }, { "type": "edge", "source": "n1", "target": "n3", "attributes": { "good_edge": False }, }, { "type": "edge", "source": "n2", "target": "n3", "attributes": { "good_edge": False }, }, ] el1 = Jelm(objects=[*node_list1, *edge_list1]) def filter_fun(atts): is_good_edge = atts.get("good_edge") if is_good_edge is None: return True return is_good_edge expected_el = Jelm(objects=[ { "type": "node", "id": "n1" }, { "type": "node", "id": "n2" }, { "type": "node", "id": "n3" }, { "type": "edge", "source": "n1", "target": "n2", "attributes": { "good_edge": True }, }, ]) assert expected_el == el1.attribute_filter(filter_fun)
def get_whole_graph(el: Jelm): return el.get_neighborhood(el.nodes.keys())
def test_eq(jelm_pair_case: NetwokCaseTemplate): jelm_pair_case.evaluate_fun(non_altering_function=lambda x: x) assert not (10 == Jelm()) assert not ("fing" == Jelm())