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 __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"}]) self.el2 = Jelm(objects=[{ "type": "node", "id": "n1" }, { "type": "node", "id": "n2" }]) self.same = False
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 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 test_add_object(): el = Jelm() el.add_object({"type": "node", "id": "n1"}) el.add_object(Node(id="n2")) el.add_object({"type": "edge", "source": "n1", "target": "n2"}) el.add_object(Node(id="n3", attributes={"priority": "low"})) with pytest.raises(ValueError): el.add_object({"no": "type"}) with pytest.raises(ValueError): el.add_object({"type": "wrong"}) with pytest.raises(ValueError): el.add_object(10) el.add_edge("n3", "n2") el.add_node("n4", {"order": "latest"}) assert len(set([type(o) for o in el.objects])) > 1 assert isinstance(el.objects[0], Node) assert isinstance(el.objects[2], Edge)
def transform_init(el): el_from_nodes = Jelm(metadata=el.metadata, nodes=el.nodes) assert el_from_nodes == el return el_from_nodes
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
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 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 __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 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 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())
def __init__(self): self.el1 = Jelm() self.el2 = Jelm() self.same = True
def __init__(self): self.el1 = Jelm(metadata={"author": "John Doe"}) self.el2 = Jelm(metadata={"author": "Jane Doe"}) self.same = False