def test_appending_a_branch_with_an_invalid_ontology_class(self): graph = Graph.new() graph.name = "TEST GRAPH" graph.subtitle = "ARCHES TEST GRAPH" graph.author = "Arches" graph.description = "ARCHES TEST GRAPH" graph.ontology = models.Ontology.objects.get( pk="e6e8db47-2ccf-11e6-927e-b8f6b115d7dd") graph.version = "v1.0.0" graph.isactive = False graph.iconclass = "fa fa-building" graph.nodegroups = [] graph.root.name = 'ROOT NODE' graph.root.description = 'Test Root Node' graph.root.ontologyclass = 'http://www.cidoc-crm.org/cidoc-crm/E21_Person' graph.root.datatype = 'semantic' graph.save() graph.append_branch( 'http://www.cidoc-crm.org/cidoc-crm/P43_has_dimension', graphid=self.NODE_NODETYPE_GRAPHID) with self.assertRaises(GraphValidationError) as cm: graph.save()
def setUp(self): self.NODE_NODETYPE_GRAPHID = '22000000-0000-0000-0000-000000000001' graph = Graph.new() graph.name = "TEST GRAPH" graph.subtitle = "ARCHES TEST GRAPH" graph.author = "Arches" graph.description = "ARCHES TEST GRAPH" graph.ontology_id = "e6e8db47-2ccf-11e6-927e-b8f6b115d7dd" graph.version = "v1.0.0" graph.isactive = False graph.iconclass = "fa fa-building" graph.nodegroups = [] graph.save() graph.root.name = 'ROOT NODE' graph.root.description = 'Test Root Node' graph.root.ontologyclass = 'E1_CRM_Entity' graph.root.datatype = 'semantic' graph.root.save() graph = Graph.objects.get(graphid=graph.pk) self.appended_branch_1 = graph.append_branch( 'L54_is_same-as', graphid=self.NODE_NODETYPE_GRAPHID) self.appended_branch_2 = graph.append_branch( 'L54_is_same-as', graphid=self.NODE_NODETYPE_GRAPHID) graph.save() self.ROOT_ID = graph.root.nodeid self.GRAPH_ID = str(graph.pk) self.NODE_COUNT = 5 self.client = Client()
def setUp(self): self.NODE_NODETYPE_GRAPHID = '22000000-0000-0000-0000-000000000001' graph = Graph.new() graph.name = "TEST GRAPH" graph.subtitle = "ARCHES TEST GRAPH" graph.author = "Arches" graph.description = "ARCHES TEST GRAPH" graph.ontology_id = "e6e8db47-2ccf-11e6-927e-b8f6b115d7dd" graph.version = "v1.0.0" graph.isactive = False graph.iconclass = "fa fa-building" graph.nodegroups = [] graph.save() graph.root.name = 'ROOT NODE' graph.root.description = 'Test Root Node' graph.root.ontologyclass = 'http://www.cidoc-crm.org/cidoc-crm/E1_CRM_Entity' graph.root.datatype = 'semantic' graph.root.save() graph = Graph.objects.get(graphid=graph.pk) self.appended_branch_1 = graph.append_branch('http://www.ics.forth.gr/isl/CRMdig/L54_is_same-as', graphid=self.NODE_NODETYPE_GRAPHID) self.appended_branch_2 = graph.append_branch('http://www.ics.forth.gr/isl/CRMdig/L54_is_same-as', graphid=self.NODE_NODETYPE_GRAPHID) graph.save() self.ROOT_ID = graph.root.nodeid self.GRAPH_ID = str(graph.pk) self.NODE_COUNT = 5 self.client = Client()
def test_rules_for_appending(self): """ test the rules that control the appending of branches to graphs """ graph = Graph.objects.get(node=self.rootNode) graph.isresource = True self.assertIsNotNone(graph.append_branch('http://www.ics.forth.gr/isl/CRMdig/L54_is_same-as', graphid=self.NODE_NODETYPE_GRAPHID)) graph = Graph.new() graph.root.datatype = 'string' graph.update_node(JSONSerializer().serializeToPython(graph.root)) # create card collector graph to use for appending on to other graphs collector_graph = Graph.new() collector_graph.append_branch('http://www.ics.forth.gr/isl/CRMdig/L54_is_same-as', graphid=self.NODE_NODETYPE_GRAPHID) collector_graph.save()
def test_new_graph(self): name = "TEST NEW GRAPH" author = "ARCHES TEST" graph = Graph.new(name=name,is_resource=True,author=author) self.assertEqual(graph.name, name) self.assertEqual(graph.author, author) self.assertTrue(graph.isresource) self.assertFalse(graph.root.is_collector) self.assertEqual(len(graph.nodes), 1) self.assertEqual(len(graph.cards), 0) self.assertEqual(len(graph.get_nodegroups()), 0) graph = Graph.new(name=name,is_resource=False,author=author) self.assertEqual(graph.name, name) self.assertEqual(graph.author, author) self.assertFalse(graph.isresource) self.assertTrue(graph.root.is_collector) self.assertEqual(len(graph.nodes), 1) self.assertEqual(len(graph.cards), 1) self.assertEqual(len(graph.get_nodegroups()), 1)
def test_new_graph(self): name = "TEST NEW GRAPH" author = "ARCHES TEST" graph = Graph.new(name=name,is_resource=True,author=author) self.assertEqual(graph.name, name) self.assertEqual(graph.author, author) self.assertTrue(graph.isresource) self.assertFalse(graph.root.is_collector) self.assertEqual(len(graph.nodes), 1) self.assertEqual(len(graph.cards), 0) self.assertEqual(len(graph.get_nodegroups()), 0) graph = Graph.new(name=name,is_resource=False,author=author) self.assertEqual(graph.name, name) self.assertEqual(graph.author, author) self.assertFalse(graph.isresource) self.assertTrue(graph.root.is_collector) self.assertEqual(len(graph.nodes), 1) self.assertEqual(len(graph.cards), 1) self.assertEqual(len(graph.get_nodegroups()), 1)
def new(request): if request.method == 'POST': data = JSONDeserializer().deserialize(request.body) isresource = data['isresource'] if 'isresource' in data else False name = _('New Resource') if isresource else _('New Graph') author = request.user.first_name + ' ' + request.user.last_name graph = Graph.new(name=name,is_resource=isresource,author=author) graph.save() return JSONResponse(graph) return HttpResponseNotFound()
def test_get_root_card(self): """ test we can get the right parent card """ graph = Graph.new(name="TEST", is_resource=False, author="TEST") graph.append_branch("http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by", graphid=self.NODE_NODETYPE_GRAPHID) for card in list(graph.cards.values()): if card.nodegroup.parentnodegroup is None: self.assertEqual(graph.get_root_card(), card)
def test_get_root_card(self): """ test we can get the right parent card """ graph = Graph.new(name='TEST',is_resource=False,author='TEST') graph.append_branch('P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) for card in graph.cards.itervalues(): if card.nodegroup.parentnodegroup is None: self.assertEqual(graph.get_root_card(), card)
def test_get_root_card(self): """ test we can get the right parent card """ graph = Graph.new(name='TEST',is_resource=False,author='TEST') graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) for card in graph.cards.itervalues(): if card.nodegroup.parentnodegroup is None: self.assertEqual(graph.get_root_card(), card)
def test_get_root_nodegroup(self): """ test we can get the right parent NodeGroup """ graph = Graph.new(name='TEST',is_resource=False,author='TEST') graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) for node in graph.nodes.itervalues(): if node.is_collector: if node.nodegroup.parentnodegroup is None: self.assertEqual(graph.get_root_nodegroup(), node.nodegroup)
def test_get_root_nodegroup(self): """ test we can get the right parent NodeGroup """ graph = Graph.new(name="TEST", is_resource=False, author="TEST") graph.append_branch("http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by", graphid=self.NODE_NODETYPE_GRAPHID) for node in list(graph.nodes.values()): if node.is_collector: if node.nodegroup.parentnodegroup is None: self.assertEqual(graph.get_root_nodegroup(), node.nodegroup)
def test_get_root_nodegroup(self): """ test we can get the right parent NodeGroup """ graph = Graph.new(name='TEST',is_resource=False,author='TEST') graph.append_branch('P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) for node in graph.nodes.itervalues(): if node.is_collector: if node.nodegroup.parentnodegroup is None: self.assertEqual(graph.get_root_nodegroup(), node.nodegroup)
def post(self, request, graphid=None): ret = {} try: if self.action == 'import_graph': graph_file = request.FILES.get('importedGraph').read() graphs = JSONDeserializer().deserialize(graph_file)['graph'] ret = GraphImporter.import_graph(graphs) else: if graphid is not None: graph = Graph.objects.get(graphid=graphid) data = JSONDeserializer().deserialize(request.body) if self.action == 'new_graph': isresource = data['isresource'] if 'isresource' in data else False name = _('New Resource Model') if isresource else _('New Branch') author = request.user.first_name + ' ' + request.user.last_name ret = Graph.new(name=name,is_resource=isresource,author=author) elif self.action == 'update_node': graph.update_node(data) ret = graph graph.save() elif self.action == 'update_node_layer': nodeid = uuid.UUID(str(data.get('nodeid'))) node = graph.nodes[nodeid] node.config = data['config'] ret = graph node.save() elif self.action == 'append_branch': ret = graph.append_branch(data['property'], nodeid=data['nodeid'], graphid=data['graphid']) graph.save() elif self.action == 'move_node': ret = graph.move_node(data['nodeid'], data['property'], data['newparentnodeid']) graph.save() elif self.action == 'clone_graph': clone_data = graph.copy() ret = clone_data['copy'] ret.save() ret.copy_functions(graph, [clone_data['nodes'], clone_data['nodegroups']]) form_map = ret.copy_forms(graph, clone_data['cards']) ret.copy_reports(graph, [form_map, clone_data['cards'], clone_data['nodes']]) return JSONResponse(ret) except GraphValidationError as e: return JSONResponse({'status':'false','message':e.message, 'title':e.title}, status=500)
def test_delete_node(self): """ test the node delete method """ graph = Graph.new(name='TEST', is_resource=False, author='TEST') graph.append_branch( 'http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) graph.save() node = models.Node.objects.get(graph=graph, name="Node") nodes_count_before = models.Node.objects.count() edges_count_before = models.Edge.objects.count() nodegroups_count_before = models.NodeGroup.objects.count() card_count_before = models.CardModel.objects.count() graph.delete_node(node) nodes_count_after = models.Node.objects.count() edges_count_after = models.Edge.objects.count() nodegroups_count_after = models.NodeGroup.objects.count() card_count_after = models.CardModel.objects.count() self.assertEqual(nodes_count_before - nodes_count_after, 2) self.assertEqual(edges_count_before - edges_count_after, 2) self.assertEqual(nodegroups_count_before - nodegroups_count_after, 1) self.assertEqual(card_count_before - card_count_after, 1) graph = Graph.objects.get(graphid=graph.pk) self.assertEqual(len(graph.nodes), 1) self.assertEqual(len(graph.edges), 0) self.assertEqual(len(graph.cards), 1) self.assertEqual(len(graph.get_nodegroups()), 1) graph.append_branch( 'http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) graph.save() node = models.Node.objects.get(graph=graph, name="Node Type") graph.delete_node(node) graph = Graph.objects.get(graphid=graph.pk) self.assertEqual(len(graph.nodes), 2) self.assertEqual(len(graph.edges), 1) self.assertEqual(len(graph.cards), 2) self.assertEqual(len(graph.get_nodegroups()), 2)
def setUp(self): graph = Graph.new() graph.name = "TEST GRAPH" graph.subtitle = "ARCHES TEST GRAPH" graph.author = "Arches" graph.description = "ARCHES TEST GRAPH" graph.ontology_id = "e6e8db47-2ccf-11e6-927e-b8f6b115d7dd" graph.version = "v1.0.0" graph.isactive = False graph.iconclass = "fa fa-building" graph.save() graph.root.name = 'ROOT NODE' graph.root.description = 'Test Root Node' graph.root.ontologyclass = 'E1_CRM_Entity' graph.root.datatype = 'semantic' graph.root.save() self.rootNode = graph.root
def setUp(self): graph = Graph.new() graph.name = "TEST GRAPH" graph.subtitle = "ARCHES TEST GRAPH" graph.author = "Arches" graph.description = "ARCHES TEST GRAPH" graph.ontology_id = "e6e8db47-2ccf-11e6-927e-b8f6b115d7dd" graph.version = "v1.0.0" graph.isactive = False graph.iconclass = "fa fa-building" graph.save() graph.root.name = 'ROOT NODE' graph.root.description = 'Test Root Node' graph.root.ontologyclass = 'E1_CRM_Entity' graph.root.datatype = 'semantic' graph.root.save() self.rootNode = graph.root
def post(self, request, graphid=None): ret = {} try: if self.action == 'import_graph': graph_file = request.FILES.get('importedGraph').read() graphs = JSONDeserializer().deserialize(graph_file)['graph'] ret = GraphImporter.import_graph(graphs) else: if graphid is not None: graph = Graph.objects.get(graphid=graphid) data = JSONDeserializer().deserialize(request.body) if self.action == 'new_graph': isresource = data['isresource'] if 'isresource' in data else False name = _('New Resource Model') if isresource else _('New Branch') author = request.user.first_name + ' ' + request.user.last_name ret = Graph.new(name=name,is_resource=isresource,author=author) elif self.action == 'update_node': graph.update_node(data) ret = graph graph.save() elif self.action == 'append_branch': ret = graph.append_branch(data['property'], nodeid=data['nodeid'], graphid=data['graphid']) graph.save() elif self.action == 'move_node': ret = graph.move_node(data['nodeid'], data['property'], data['newparentnodeid']) graph.save() elif self.action == 'clone_graph': clone_data = graph.copy() ret = clone_data['copy'] ret.save() ret.copy_functions(graph, [clone_data['nodes'], clone_data['nodegroups']]) form_map = ret.copy_forms(graph, clone_data['cards']) ret.copy_reports(graph, [form_map, clone_data['cards'], clone_data['nodes']]) return JSONResponse(ret) except GraphValidationError as e: return JSONResponse({'status':'false','message':e.message, 'title':e.title}, status=500)
def post(self, request, graphid=None): ret = {} if self.action == 'import_graph': graph_file = request.FILES.get('importedGraph').read() graphs = JSONDeserializer().deserialize(graph_file)['graph'] ret = GraphImporter.import_graph(graphs) else: if graphid is not None: graph = Graph.objects.get(graphid=graphid) data = JSONDeserializer().deserialize(request.body) if self.action == 'new_graph': isresource = data[ 'isresource'] if 'isresource' in data else False name = _('New Resource Model') if isresource else _( 'New Branch') author = request.user.first_name + ' ' + request.user.last_name ret = Graph.new(name=name, is_resource=isresource, author=author) elif self.action == 'update_node': graph.update_node(data) ret = graph graph.save() elif self.action == 'append_branch': ret = graph.append_branch(data['property'], nodeid=data['nodeid'], graphid=data['graphid']) graph.save() elif self.action == 'move_node': ret = graph.move_node(data['nodeid'], data['property'], data['newparentnodeid']) graph.save() elif self.action == 'clone_graph': ret = graph.copy() ret.save() return JSONResponse(ret)
def test_delete_node(self): """ test the node delete method """ graph = Graph.new(name='TEST',is_resource=False,author='TEST') graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) graph.save() node = models.Node.objects.get(graph=graph,name="Node") nodes_count_before = models.Node.objects.count() edges_count_before = models.Edge.objects.count() nodegroups_count_before = models.NodeGroup.objects.count() card_count_before = models.CardModel.objects.count() graph.delete_node(node) nodes_count_after = models.Node.objects.count() edges_count_after = models.Edge.objects.count() nodegroups_count_after = models.NodeGroup.objects.count() card_count_after = models.CardModel.objects.count() self.assertEqual(nodes_count_before-nodes_count_after, 2) self.assertEqual(edges_count_before-edges_count_after, 2) self.assertEqual(nodegroups_count_before-nodegroups_count_after, 1) self.assertEqual(card_count_before-card_count_after, 1) graph = Graph.objects.get(graphid=graph.pk) self.assertEqual(len(graph.nodes), 1) self.assertEqual(len(graph.edges), 0) self.assertEqual(len(graph.cards), 1) self.assertEqual(len(graph.get_nodegroups()), 1) graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) graph.save() node = models.Node.objects.get(graph=graph,name="Node Type") graph.delete_node(node) graph = Graph.objects.get(graphid=graph.pk) self.assertEqual(len(graph.nodes), 2) self.assertEqual(len(graph.edges), 1) self.assertEqual(len(graph.cards), 2) self.assertEqual(len(graph.get_nodegroups()), 2)
def setUp(self): graph = Graph.new() graph.name = "TEST GRAPH" graph.subtitle = "ARCHES TEST GRAPH" graph.author = "Arches" graph.description = "ARCHES TEST GRAPH" graph.ontology_id = "e6e8db47-2ccf-11e6-927e-b8f6b115d7dd" graph.version = "v1.0.0" graph.isactive = False graph.iconclass = "fa fa-building" graph.nodegroups = [] graph.save() graph.root.name = "ROOT NODE" graph.root.description = "Test Root Node" graph.root.ontologyclass = "http://www.cidoc-crm.org/cidoc-crm/E1_CRM_Entity" graph.root.datatype = "semantic" graph.root.save() self.rootNode = graph.root
def test_graph_validation_of_branch_with_ontology_appended_to_graph_with_no_ontology(self): """ test to make sure we can't append a branch with ontology defined to a graph with no ontology defined """ graph = Graph.new() graph.name = "TEST GRAPH" graph.ontology = None graph.save() graph.root.name = 'ROOT NODE' graph.root.description = 'Test Root Node' graph.root.ontologyclass = 'http://www.cidoc-crm.org/cidoc-crm/E1_CRM_Entity' graph.root.datatype = 'semantic' graph.root.save() with self.assertRaises(GraphValidationError) as cm: graph.save() the_exception = cm.exception self.assertEqual(the_exception.code, 1005)
def test_graph_validation_of_branch_with_ontology_appended_to_graph_with_no_ontology(self): """ test to make sure we can't append a branch with ontology defined to a graph with no ontology defined """ graph = Graph.new() graph.name = "TEST GRAPH" graph.ontology = None graph.save() graph.root.name = 'ROOT NODE' graph.root.description = 'Test Root Node' graph.root.ontologyclass = 'http://www.cidoc-crm.org/cidoc-crm/E1_CRM_Entity' graph.root.datatype = 'semantic' graph.root.save() with self.assertRaises(GraphValidationError) as cm: graph.save() the_exception = cm.exception self.assertEqual(the_exception.code, 1005)
def post(self, request, graphid=None): ret = {} if self.action == 'import_graph': graph_file = request.FILES.get('importedGraph').read() graphs = JSONDeserializer().deserialize(graph_file)['graph'] GraphImporter.import_graph(graphs) for graph in graphs: ret = graph else: if graphid is not None: graph = Graph.objects.get(graphid=graphid) data = JSONDeserializer().deserialize(request.body) if self.action == 'new_graph': isresource = data['isresource'] if 'isresource' in data else False name = _('New Resource Model') if isresource else _('New Branch') author = request.user.first_name + ' ' + request.user.last_name ret = Graph.new(name=name,is_resource=isresource,author=author) elif self.action == 'update_node': graph.update_node(data) ret = graph graph.save() elif self.action == 'append_branch': ret = graph.append_branch(data['property'], nodeid=data['nodeid'], graphid=data['graphid']) graph.save() elif self.action == 'move_node': ret = graph.move_node(data['nodeid'], data['property'], data['newparentnodeid']) graph.save() elif self.action == 'clone_graph': ret = graph.copy() ret.save() return JSONResponse(ret)
def setUp(self): self.NODE_NODETYPE_GRAPHID = "22000000-0000-0000-0000-000000000001" if len(Graph.objects.filter(graphid=self.NODE_NODETYPE_GRAPHID)) == 0: # Node Branch graph_dict = { "author": "Arches", "color": None, "deploymentdate": None, "deploymentfile": None, "description": "Represents a single node in a graph", "graphid": "22000000-0000-0000-0000-000000000000", "iconclass": "fa fa-circle", "isactive": True, "isresource": False, "name": "Node", "ontology_id": "e6e8db47-2ccf-11e6-927e-b8f6b115d7dd", "subtitle": "Represents a single node in a graph.", "version": "v1", } GraphModel.objects.create(**graph_dict).save() node_dict = { "config": None, "datatype": "semantic", "description": "Represents a single node in a graph", "graph_id": "22000000-0000-0000-0000-000000000000", "isrequired": False, "issearchable": True, "istopnode": True, "name": "Node", "nodegroup_id": None, "nodeid": "20000000-0000-0000-0000-100000000000", "ontologyclass": "http://www.cidoc-crm.org/cidoc-crm/E1_CRM_Entity", } Node.objects.create(**node_dict).save() # Node/Node Type Branch graph_dict = { "author": "Arches", "color": None, "deploymentdate": None, "deploymentfile": None, "description": "Represents a node and node type pairing", "graphid": "22000000-0000-0000-0000-000000000001", "iconclass": "fa fa-angle-double-down", "isactive": True, "isresource": False, "name": "Node/Node Type", "ontology_id": "e6e8db47-2ccf-11e6-927e-b8f6b115d7dd", "subtitle": "Represents a node and node type pairing", "version": "v1", } GraphModel.objects.create(**graph_dict).save() nodegroup_dict = { "cardinality": "n", "legacygroupid": "", "nodegroupid": "20000000-0000-0000-0000-100000000001", "parentnodegroup_id": None, } NodeGroup.objects.create(**nodegroup_dict).save() card_dict = { "active": True, "cardid": "bf9ea150-3eaa-11e8-8b2b-c3a348661f61", "description": "Represents a node and node type pairing", "graph_id": "22000000-0000-0000-0000-000000000001", "helpenabled": False, "helptext": None, "helptitle": None, "instructions": "", "name": "Node/Node Type", "nodegroup_id": "20000000-0000-0000-0000-100000000001", "sortorder": None, "visible": True, } CardModel.objects.create(**card_dict).save() nodes = [ { "config": None, "datatype": "string", "description": "", "graph_id": "22000000-0000-0000-0000-000000000001", "isrequired": False, "issearchable": True, "istopnode": True, "name": "Node", "nodegroup_id": "20000000-0000-0000-0000-100000000001", "nodeid": "20000000-0000-0000-0000-100000000001", "ontologyclass": "http://www.cidoc-crm.org/cidoc-crm/E1_CRM_Entity", }, { "config": { "rdmCollection": None }, "datatype": "concept", "description": "", "graph_id": "22000000-0000-0000-0000-000000000001", "isrequired": False, "issearchable": True, "istopnode": False, "name": "Node Type", "nodegroup_id": "20000000-0000-0000-0000-100000000001", "nodeid": "20000000-0000-0000-0000-100000000002", "ontologyclass": "http://www.cidoc-crm.org/cidoc-crm/E55_Type", }, ] for node in nodes: Node.objects.create(**node).save() edges_dict = { "description": None, "domainnode_id": "20000000-0000-0000-0000-100000000001", "edgeid": "22200000-0000-0000-0000-000000000001", "graph_id": "22000000-0000-0000-0000-000000000001", "name": None, "ontologyproperty": "http://www.cidoc-crm.org/cidoc-crm/P2_has_type", "rangenode_id": "20000000-0000-0000-0000-100000000002", } Edge.objects.create(**edges_dict).save() graph = Graph.new() graph.name = "TEST GRAPH" graph.subtitle = "ARCHES TEST GRAPH" graph.author = "Arches" graph.description = "ARCHES TEST GRAPH" graph.ontology_id = "e6e8db47-2ccf-11e6-927e-b8f6b115d7dd" graph.version = "v1.0.0" graph.isactive = False graph.iconclass = "fa fa-building" graph.nodegroups = [] graph.save() graph.root.name = "ROOT NODE" graph.root.description = "Test Root Node" graph.root.ontologyclass = "http://www.cidoc-crm.org/cidoc-crm/E1_CRM_Entity" graph.root.datatype = "semantic" graph.root.save() graph = Graph.objects.get(graphid=graph.pk) self.appended_branch_1 = graph.append_branch( "http://www.ics.forth.gr/isl/CRMdig/L54_is_same-as", graphid=self.NODE_NODETYPE_GRAPHID) self.appended_branch_2 = graph.append_branch( "http://www.ics.forth.gr/isl/CRMdig/L54_is_same-as", graphid=self.NODE_NODETYPE_GRAPHID) graph.save() self.ROOT_ID = graph.root.nodeid self.GRAPH_ID = str(graph.pk) self.NODE_COUNT = 5 self.client = Client()
def test_save_and_update_dont_orphan_records_in_the_db(self): """ test that the proper number of nodes, edges, nodegroups, and cards are persisted to the database during save and update opertaions """ nodes_count_before = models.Node.objects.count() edges_count_before = models.Edge.objects.count() nodegroups_count_before = models.NodeGroup.objects.count() card_count_before = models.CardModel.objects.count() # test that data is persisited propertly when creating a new graph graph = Graph.new(is_resource=False) nodes_count_after = models.Node.objects.count() edges_count_after = models.Edge.objects.count() nodegroups_count_after = models.NodeGroup.objects.count() card_count_after = models.CardModel.objects.count() self.assertEqual(nodes_count_after-nodes_count_before, 1) self.assertEqual(edges_count_after-edges_count_before, 0) self.assertEqual(nodegroups_count_after-nodegroups_count_before, 1) self.assertEqual(card_count_after-card_count_before, 1) # test that data is persisited propertly during an append opertation graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) graph.save() nodes_count_after = models.Node.objects.count() edges_count_after = models.Edge.objects.count() nodegroups_count_after = models.NodeGroup.objects.count() card_count_after = models.CardModel.objects.count() self.assertEqual(nodes_count_after-nodes_count_before, 3) self.assertEqual(edges_count_after-edges_count_before, 2) self.assertEqual(nodegroups_count_after-nodegroups_count_before, 2) self.assertEqual(card_count_after-card_count_before, 2) # test that removing a node group by setting it to None, removes it from the db node_to_update = None for node_id, node in graph.nodes.iteritems(): if node.name == 'Node': self.assertTrue(node.is_collector) node_to_update = JSONDeserializer().deserialize(JSONSerializer().serialize(node)) node_to_update['nodegroup_id'] = None graph.update_node(node_to_update.copy()) graph.save() nodegroups_count_after = models.NodeGroup.objects.count() card_count_after = models.CardModel.objects.count() self.assertEqual(nodegroups_count_after-nodegroups_count_before, 1) self.assertEqual(card_count_after-card_count_before, 1) # test that adding back a node group adds it back to the db node_to_update['nodegroup_id'] = node_to_update['nodeid'] graph.update_node(node_to_update) graph.save() nodegroups_count_after = models.NodeGroup.objects.count() card_count_after = models.CardModel.objects.count() self.assertEqual(nodegroups_count_after-nodegroups_count_before, 2) self.assertEqual(card_count_after-card_count_before, 2)
def post(self, request, graphid=None): ret = {} try: if self.action == 'import_graph': graph_file = request.FILES.get('importedGraph').read() graphs = JSONDeserializer().deserialize(graph_file)['graph'] ret = GraphImporter.import_graph(graphs) else: if graphid is not None: graph = Graph.objects.get(graphid=graphid) data = JSONDeserializer().deserialize(request.body) if self.action == 'new_graph': isresource = data[ 'isresource'] if 'isresource' in data else False name = _('New Resource Model') if isresource else _( 'New Branch') author = request.user.first_name + ' ' + request.user.last_name ret = Graph.new(name=name, is_resource=isresource, author=author) elif self.action == 'update_node': updated_values = graph.update_node(data) graph.save() ret = JSONSerializer().serializeToPython(graph) ret['updated_values'] = updated_values elif self.action == 'update_node_layer': nodeid = uuid.UUID(str(data.get('nodeid'))) node = graph.nodes[nodeid] node.config = data['config'] ret = graph node.save() elif self.action == 'append_branch': ret = graph.append_branch(data['property'], nodeid=data['nodeid'], graphid=data['graphid']) ret = ret.serialize() ret['nodegroups'] = graph.get_nodegroups() ret['cards'] = graph.get_cards() ret['widgets'] = graph.get_widgets() graph.save() elif self.action == 'append_node': ret = graph.append_node(nodeid=data['nodeid']) graph.save() elif self.action == 'move_node': ret = graph.move_node(data['nodeid'], data['property'], data['newparentnodeid']) graph.save() elif self.action == 'export_branch': clone_data = graph.copy(root=data) clone_data['copy'].save() ret = {'success': True, 'graphid': clone_data['copy'].pk} elif self.action == 'clone_graph': clone_data = graph.copy() ret = clone_data['copy'] ret.save() ret.copy_functions( graph, [clone_data['nodes'], clone_data['nodegroups']]) elif self.action == 'reorder_nodes': json = request.body if json is not None: data = JSONDeserializer().deserialize(json) if 'nodes' in data and len(data['nodes']) > 0: sortorder = 0 with transaction.atomic(): for node in data['nodes']: no = models.Node.objects.get( pk=node['nodeid']) no.sortorder = sortorder no.save() sortorder = sortorder + 1 ret = data return JSONResponse(ret) except GraphValidationError as e: return JSONResponse( { 'status': 'false', 'success': False, 'message': e.message, 'title': e.title }, status=500)
def test_derive_card_values(self): """ test to make sure we get the proper name and description for display in the ui """ # TESTING A GRAPH graph = Graph.new(name='TEST',is_resource=False,author='TEST') graph.description = 'A test description' self.assertEqual(len(graph.cards), 1) for card in graph.get_cards(): self.assertEqual(card.name, graph.name) self.assertEqual(card.description, graph.description) card.name = 'TEST card name' card.description = 'TEST card description' card.save() for card in graph.get_cards(): self.assertEqual(card.name, 'TEST') self.assertEqual(card.description, 'A test description') graph.append_branch('P1_is_identified_by', graphid=self.SINGLE_NODE_GRAPHID) graph.save() for node in graph.nodes.itervalues(): if node is not graph.root: nodeJson = JSONSerializer().serializeToPython(node) nodeJson['nodegroup_id'] = nodeJson['nodeid'] graph.update_node(nodeJson) graph.save() self.assertEqual(len(graph.get_cards()), 2) for card in graph.get_cards(): if str(card.nodegroup_id) == str(graph.root.nodegroup_id): self.assertEqual(card.name, graph.name) self.assertEqual(card.description, graph.description) else: self.assertTrue(len(graph.nodes[card.nodegroup.pk].name) > 0) self.assertTrue(len(graph.nodes[card.nodegroup.pk].description) > 0) self.assertEqual(card.name, graph.nodes[card.nodegroup.pk].name) self.assertEqual(card.description, graph.nodes[card.nodegroup.pk].description) # TESTING A RESOURCE resource_graph = Graph.new(name='TEST RESOURCE',is_resource=True,author='TEST') resource_graph.description = 'A test resource description' resource_graph.append_branch('P1_is_identified_by', graphid=graph.graphid) resource_graph.save() self.assertEqual(len(resource_graph.get_cards()), 2) for card in resource_graph.get_cards(): if card.nodegroup.parentnodegroup is None: self.assertEqual(card.name, graph.name) self.assertEqual(card.description, graph.description) card.name = 'altered root card name' card.description = 'altered root card description' else: self.assertTrue(len(resource_graph.nodes[card.nodegroup.pk].name) > 0) self.assertTrue(len(resource_graph.nodes[card.nodegroup.pk].description) > 0) self.assertEqual(card.name, resource_graph.nodes[card.nodegroup.pk].name) self.assertEqual(card.description, resource_graph.nodes[card.nodegroup.pk].description) card.name = 'altered child card name' card.description = 'altered child card description' # loop through the cards again now looking for the updated desctiptions for card in resource_graph.get_cards(): if card.nodegroup.parentnodegroup is None: self.assertEqual(card.name, 'altered root card name') self.assertEqual(card.description, 'altered root card description') else: self.assertEqual(card.name, 'altered child card name') self.assertEqual(card.description, 'altered child card description') resource_graph.delete() # TESTING A RESOURCE resource_graph = Graph.new(name='TEST',is_resource=True,author='TEST') resource_graph.description = 'A test description' resource_graph.append_branch('P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) resource_graph.save() self.assertEqual(len(resource_graph.cards), 1) the_card = resource_graph.cards.itervalues().next() for card in resource_graph.get_cards(): self.assertEqual(card.name, the_card.name) self.assertEqual(card.description, the_card.description) # after removing the card name and description, the cards should take on the node name and description the_card.name = '' the_card.description = '' for card in resource_graph.get_cards(): self.assertEqual(card.name, resource_graph.nodes[card.nodegroup.pk].name) self.assertEqual(card.description, resource_graph.nodes[card.nodegroup.pk].description)
def test_derive_card_values(self): """ test to make sure we get the proper name and description for display in the ui """ # TESTING A GRAPH graph = Graph.new(name='TEST',is_resource=False,author='TEST') graph.description = 'A test description' self.assertEqual(len(graph.cards), 1) for card in graph.get_cards(): self.assertEqual(card.name, graph.name) self.assertEqual(card.description, graph.description) card.name = 'TEST card name' card.description = 'TEST card description' card.save() for card in graph.get_cards(): self.assertEqual(card.name, 'TEST') self.assertEqual(card.description, 'A test description') graph.append_branch('P1_is_identified_by', graphid=self.SINGLE_NODE_GRAPHID) graph.save() for node in graph.nodes.itervalues(): if node is not graph.root: nodeJson = JSONSerializer().serializeToPython(node) nodeJson['nodegroup_id'] = nodeJson['nodeid'] graph.update_node(nodeJson) graph.save() self.assertEqual(len(graph.get_cards()), 2) for card in graph.get_cards(): if str(card.nodegroup_id) == str(graph.root.nodegroup_id): self.assertEqual(card.name, graph.name) self.assertEqual(card.description, graph.description) else: self.assertTrue(len(graph.nodes[card.nodegroup.pk].name) > 0) self.assertTrue(len(graph.nodes[card.nodegroup.pk].description) > 0) self.assertEqual(card.name, graph.nodes[card.nodegroup.pk].name) self.assertEqual(card.description, graph.nodes[card.nodegroup.pk].description) # TESTING A RESOURCE resource_graph = Graph.new(name='TEST RESOURCE',is_resource=True,author='TEST') resource_graph.description = 'A test resource description' resource_graph.append_branch('P1_is_identified_by', graphid=graph.graphid) resource_graph.save() self.assertEqual(len(resource_graph.get_cards()), 2) for card in resource_graph.get_cards(): if card.nodegroup.parentnodegroup is None: self.assertEqual(card.name, graph.name) self.assertEqual(card.description, graph.description) card.name = 'altered root card name' card.description = 'altered root card description' else: self.assertTrue(len(resource_graph.nodes[card.nodegroup.pk].name) > 0) self.assertTrue(len(resource_graph.nodes[card.nodegroup.pk].description) > 0) self.assertEqual(card.name, resource_graph.nodes[card.nodegroup.pk].name) self.assertEqual(card.description, resource_graph.nodes[card.nodegroup.pk].description) card.name = 'altered child card name' card.description = 'altered child card description' # loop through the cards again now looking for the updated desctiptions for card in resource_graph.get_cards(): if card.nodegroup.parentnodegroup is None: self.assertEqual(card.name, 'altered root card name') self.assertEqual(card.description, 'altered root card description') else: self.assertEqual(card.name, 'altered child card name') self.assertEqual(card.description, 'altered child card description') resource_graph.delete() # TESTING A RESOURCE resource_graph = Graph.new(name='TEST',is_resource=True,author='TEST') resource_graph.description = 'A test description' resource_graph.append_branch('P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) resource_graph.save() self.assertEqual(len(resource_graph.cards), 1) the_card = resource_graph.cards.itervalues().next() for card in resource_graph.get_cards(): self.assertEqual(card.name, the_card.name) self.assertEqual(card.description, the_card.description) # after removing the card name and description, the cards should take on the node name and description the_card.name = '' the_card.description = '' for card in resource_graph.get_cards(): self.assertEqual(card.name, resource_graph.nodes[card.nodegroup.pk].name) self.assertEqual(card.description, resource_graph.nodes[card.nodegroup.pk].description)
def test_rules_for_appending(self): """ test the rules that control the appending of branches to graphs """ graph = Graph.objects.get(node=self.rootNode) graph.isresource = True self.assertIsNotNone(graph.append_branch('http://www.ics.forth.gr/isl/CRMdig/L54_is_same-as', graphid=self.NODE_NODETYPE_GRAPHID)) # try to append to any other node that is not the root for node in graph.nodes.itervalues(): if node is not graph.root: with self.assertRaises(GraphValidationError): graph.append_branch('http://www.ics.forth.gr/isl/CRMdig/L54_is_same-as', graphid=self.NODE_NODETYPE_GRAPHID, nodeid=node.nodeid) # try to append a non-grouped graph with self.assertRaises(GraphValidationError): graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.SINGLE_NODE_GRAPHID) graph = Graph.objects.get(graphid=self.SINGLE_NODE_GRAPHID) # test that we can't append a single non-grouped node to a graph that is a single non grouped node with self.assertRaises(GraphValidationError): graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.SINGLE_NODE_GRAPHID) graph = Graph.new() graph.root.datatype = 'string' graph.update_node(JSONSerializer().serializeToPython(graph.root)) # test that we can't append a card to a graph that is a card that at it's root is not semantic with self.assertRaises(GraphValidationError): graph.append_branch('http://www.ics.forth.gr/isl/CRMdig/L54_is_same-as', graphid=self.NODE_NODETYPE_GRAPHID) # test that we can't append a card as a child to another card graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.SINGLE_NODE_GRAPHID) for node in graph.nodes.itervalues(): if node != graph.root: with self.assertRaises(GraphValidationError): graph.append_branch('http://www.ics.forth.gr/isl/CRMdig/L54_is_same-as', graphid=self.NODE_NODETYPE_GRAPHID, nodeid=node.nodeid) # create card collector graph to use for appending on to other graphs collector_graph = Graph.new() collector_graph.append_branch('http://www.ics.forth.gr/isl/CRMdig/L54_is_same-as', graphid=self.NODE_NODETYPE_GRAPHID) collector_graph.save() # test that we can't append a card collector on to a graph that is a card graph = Graph.new() with self.assertRaises(GraphValidationError): graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=collector_graph.graphid) # test that we can't append a card collector to another card collector collector_copy = collector_graph.copy()['copy'] with self.assertRaises(GraphValidationError): collector_copy.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=collector_graph.graphid) # test that we can't append a card to a node in a child card within a card collector for node in collector_graph.nodes.itervalues(): if node != collector_graph.root: with self.assertRaises(GraphValidationError): collector_graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=graph.graphid, nodeid=node.nodeid)
def test_save_and_update_dont_orphan_records_in_the_db(self): """ test that the proper number of nodes, edges, nodegroups, and cards are persisted to the database during save and update opertaions """ nodes_count_before = models.Node.objects.count() edges_count_before = models.Edge.objects.count() nodegroups_count_before = models.NodeGroup.objects.count() card_count_before = models.CardModel.objects.count() # test that data is persisited propertly when creating a new graph graph = Graph.new(is_resource=False) nodes_count_after = models.Node.objects.count() edges_count_after = models.Edge.objects.count() nodegroups_count_after = models.NodeGroup.objects.count() card_count_after = models.CardModel.objects.count() self.assertEqual(nodes_count_after-nodes_count_before, 1) self.assertEqual(edges_count_after-edges_count_before, 0) self.assertEqual(nodegroups_count_after-nodegroups_count_before, 1) self.assertEqual(card_count_after-card_count_before, 1) # test that data is persisited propertly during an append opertation graph.append_branch('P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) graph.save() nodes_count_after = models.Node.objects.count() edges_count_after = models.Edge.objects.count() nodegroups_count_after = models.NodeGroup.objects.count() card_count_after = models.CardModel.objects.count() self.assertEqual(nodes_count_after-nodes_count_before, 3) self.assertEqual(edges_count_after-edges_count_before, 2) self.assertEqual(nodegroups_count_after-nodegroups_count_before, 2) self.assertEqual(card_count_after-card_count_before, 2) # test that removing a node group by setting it to None, removes it from the db node_to_update = None for node_id, node in graph.nodes.iteritems(): if node.name == 'Node': self.assertTrue(node.is_collector) node_to_update = JSONDeserializer().deserialize(JSONSerializer().serialize(node)) node_to_update['nodegroup_id'] = None graph.update_node(node_to_update.copy()) graph.save() nodegroups_count_after = models.NodeGroup.objects.count() card_count_after = models.CardModel.objects.count() self.assertEqual(nodegroups_count_after-nodegroups_count_before, 1) self.assertEqual(card_count_after-card_count_before, 1) # test that adding back a node group adds it back to the db node_to_update['nodegroup_id'] = node_to_update['nodeid'] graph.update_node(node_to_update) graph.save() nodegroups_count_after = models.NodeGroup.objects.count() card_count_after = models.CardModel.objects.count() self.assertEqual(nodegroups_count_after-nodegroups_count_before, 2) self.assertEqual(card_count_after-card_count_before, 2)
def test_rules_for_appending(self): """ test the rules that control the appending of branches to graphs """ graph = Graph.objects.get(node=self.rootNode) graph.isresource = True self.assertIsNotNone(graph.append_branch('P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID)) # try to append to any other node that is not the root for node in graph.nodes.itervalues(): if node is not graph.root: with self.assertRaises(ValidationError): graph.append_branch('P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID, nodeid=node.nodeid) # try to append a non-grouped graph with self.assertRaises(ValidationError): graph.append_branch('P1_is_identified_by', graphid=self.SINGLE_NODE_GRAPHID) graph = Graph.objects.get(graphid=self.SINGLE_NODE_GRAPHID) # test that we can't append a single non-grouped node to a graph that is a single non grouped node with self.assertRaises(ValidationError): graph.append_branch('P1_is_identified_by', graphid=self.SINGLE_NODE_GRAPHID) graph = Graph.new() graph.root.datatype = 'string' graph.update_node(JSONSerializer().serializeToPython(graph.root)) # test that we can't append a card to a graph that is a card that at it's root is not semantic with self.assertRaises(ValidationError): graph.append_branch('P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) # test that we can't append a card as a child to another card graph.append_branch('P1_is_identified_by', graphid=self.SINGLE_NODE_GRAPHID) for node in graph.nodes.itervalues(): if node != graph.root: with self.assertRaises(ValidationError): graph.append_branch('P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID, nodeid=node.nodeid) # create card collector graph to use for appending on to other graphs collector_graph = Graph.new() collector_graph.append_branch('P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) collector_graph.save() # test that we can't append a card collector on to a graph that is a card graph = Graph.new() with self.assertRaises(ValidationError): graph.append_branch('P1_is_identified_by', graphid=collector_graph.graphid) # test that we can't append a card collector to another card collector collector_copy = collector_graph.copy() with self.assertRaises(ValidationError): collector_copy.append_branch('P1_is_identified_by', graphid=collector_graph.graphid) # test that we can't append a card to a node in a child card within a card collector for node in collector_graph.nodes.itervalues(): if node != collector_graph.root: with self.assertRaises(ValidationError): collector_graph.append_branch('P1_is_identified_by', graphid=graph.graphid, nodeid=node.nodeid)
def test_copy_graph(self): """ test that a copy of a graph has the same number of nodes and edges and that the primary keys have been changed and that the actual node references are different """ graph = Graph.new(name='TEST RESOURCE') graph.append_branch('P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) graph_copy = graph.copy() self.assertEqual(len(graph_copy.nodes), 3) self.assertEqual(len(graph_copy.edges), 2) self.assertEqual(len(graph_copy.cards), 2) self.assertEqual(len(graph_copy.get_nodegroups()), 2) self.assertEqual(len(graph.nodes), len(graph_copy.nodes)) self.assertEqual(len(graph.edges), len(graph_copy.edges)) self.assertEqual(len(graph.cards), len(graph_copy.cards)) self.assertEqual(len(graph.get_nodegroups()), len(graph_copy.get_nodegroups())) # assert the copied nodegroup heirarchy is maintained for nodegroup in graph_copy.get_nodegroups(): if graph_copy.nodes[nodegroup.pk] is graph_copy.root: parentnodegroup_copy = nodegroup; else: childnodegroup_copy = nodegroup self.assertTrue(parentnodegroup_copy.parentnodegroup is None) self.assertEqual(childnodegroup_copy.parentnodegroup, parentnodegroup_copy) self.assertFalse(parentnodegroup_copy.parentnodegroup_id) self.assertEqual(childnodegroup_copy.parentnodegroup_id, parentnodegroup_copy.pk) # assert the copied node groups are not equal to the originals for nodegroup in graph.get_nodegroups(): if graph.nodes[nodegroup.pk] is graph.root: parentnodegroup = nodegroup; else: childnodegroup = nodegroup self.assertNotEqual(parentnodegroup, parentnodegroup_copy) self.assertNotEqual(parentnodegroup.pk, parentnodegroup_copy.pk) self.assertNotEqual(childnodegroup, childnodegroup_copy) self.assertNotEqual(childnodegroup.pk, childnodegroup_copy.pk) # assert the nodegroups attached to the cards are heirarchically correct for card in graph_copy.cards.itervalues(): if str(card.nodegroup_id) == str(graph_copy.root.nodeid): parentcard_copy = card else: childcard_copy = card self.assertTrue(parentcard_copy.nodegroup is not None) self.assertTrue(childcard_copy.nodegroup is not None) self.assertTrue(parentcard_copy.nodegroup.parentnodegroup is None) self.assertTrue(childcard_copy.nodegroup.parentnodegroup is not None) self.assertEqual(parentcard_copy.nodegroup, childcard_copy.nodegroup.parentnodegroup) def findNodeByName(graph, name): for node in graph.nodes.itervalues(): if node.name == name: return node return None def findCardByName(graph, name): for card in graph.cards.itervalues(): if card.name == name: return card return None for node in graph.nodes.itervalues(): node_copy = findNodeByName(graph_copy, node.name) self.assertIsNotNone(node_copy) self.assertNotEqual(node.pk, node_copy.pk) self.assertNotEqual(id(node), id(node_copy)) self.assertEqual(node.is_collector, node_copy.is_collector) if node.nodegroup != None: self.assertNotEqual(node.nodegroup, node_copy.nodegroup) for card in graph.cards.itervalues(): card_copy = findCardByName(graph_copy, card.name) self.assertIsNotNone(card_copy) self.assertNotEqual(card.pk, card_copy.pk) self.assertNotEqual(id(card), id(card_copy)) self.assertNotEqual(card.nodegroup, card_copy.nodegroup) for newedge in graph_copy.edges.itervalues(): self.assertIsNotNone(graph_copy.nodes[newedge.domainnode_id]) self.assertIsNotNone(graph_copy.nodes[newedge.rangenode_id]) self.assertEqual(newedge.domainnode, graph_copy.nodes[newedge.domainnode.pk]) self.assertEqual(newedge.rangenode, graph_copy.nodes[newedge.rangenode.pk]) with self.assertRaises(KeyError): graph.edges[newedge.pk]
def test_derive_card_values(self): """ test to make sure we get the proper name and description for display in the ui """ # TESTING A GRAPH graph = Graph.new(name='TEST',is_resource=False,author='TEST') graph.description = 'A test description' self.assertEqual(len(graph.cards), 1) for card in graph.get_cards(): self.assertEqual(card['name'], graph.name) self.assertEqual(card['description'], graph.description) card = Card.objects.get(pk=card['cardid']) card.name = 'TEST card name' card.description = 'TEST card description' card.save() for card in graph.get_cards(): self.assertEqual(card['name'], 'TEST') self.assertEqual(card['description'], 'A test description') graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.SINGLE_NODE_GRAPHID) graph.save() for node in graph.nodes.itervalues(): if node is not graph.root: nodeJson = JSONSerializer().serializeToPython(node) nodeJson['nodegroup_id'] = nodeJson['nodeid'] graph.update_node(nodeJson) graph.save() self.assertEqual(len(graph.get_cards()), 2) for card in graph.get_cards(): if str(card['nodegroup_id']) == str(graph.root.nodegroup_id): self.assertEqual(card['name'], graph.name) self.assertEqual(card['description'], graph.description) else: self.assertTrue(len(graph.nodes[card['nodegroup_id']].name) > 0) self.assertTrue(len(graph.nodes[card['nodegroup_id']].description) > 0) self.assertEqual(card['name'], graph.nodes[card['nodegroup_id']].name) self.assertEqual(card['description'], graph.nodes[card['nodegroup_id']].description) # TESTING A RESOURCE resource_graph = Graph.new(name='TEST RESOURCE',is_resource=True,author='TEST') resource_graph.description = 'A test resource description' resource_graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=graph.graphid) resource_graph.save() self.assertEqual(len(resource_graph.get_cards()), 2) for card in resource_graph.get_cards(): cardobj = Card.objects.get(pk=card['cardid']) if cardobj.nodegroup.parentnodegroup is None: self.assertEqual(card['name'], graph.name) self.assertEqual(card['description'], graph.description) else: self.assertEqual(card['name'], resource_graph.nodes[card['nodegroup_id']].name) self.assertEqual(card['description'], resource_graph.nodes[card['nodegroup_id']].description) self.assertTrue(len(resource_graph.nodes[card['nodegroup_id']].name) > 0) self.assertTrue(len(resource_graph.nodes[card['nodegroup_id']].description) > 0) resource_graph.delete() # TESTING A RESOURCE resource_graph = Graph.new(name='TEST',is_resource=True,author='TEST') resource_graph.description = 'A test description' resource_graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) resource_graph.save() self.assertEqual(len(resource_graph.cards), 1) the_card = resource_graph.cards.itervalues().next() for card in resource_graph.get_cards(): self.assertEqual(card['name'], the_card.name) self.assertEqual(card['description'], the_card.description) # after removing the card name and description, the cards should take on the node name and description the_card.name = '' the_card.description = '' for card in resource_graph.get_cards(): self.assertEqual(card['name'], resource_graph.nodes[card['nodegroup_id']].name) self.assertEqual(card['description'], resource_graph.nodes[card['nodegroup_id']].description)
def test_derive_card_values(self): """ test to make sure we get the proper name and description for display in the ui """ # TESTING A GRAPH graph = Graph.new(name='TEST',is_resource=False,author='TEST') graph.description = 'A test description' self.assertEqual(len(graph.cards), 1) for card in graph.get_cards(): self.assertEqual(card['name'], graph.name) self.assertEqual(card['description'], graph.description) card = Card.objects.get(pk=card['cardid']) card.name = 'TEST card name' card.description = 'TEST card description' card.save() for card in graph.get_cards(): self.assertEqual(card['name'], 'TEST') self.assertEqual(card['description'], 'A test description') graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.SINGLE_NODE_GRAPHID) graph.save() for node in list(graph.nodes.values()): if node is not graph.root: nodeJson = JSONSerializer().serializeToPython(node) nodeJson['nodegroup_id'] = nodeJson['nodeid'] graph.update_node(nodeJson) graph.save() self.assertEqual(len(graph.get_cards()), 2) for card in graph.get_cards(): if str(card['nodegroup_id']) == str(graph.root.nodegroup_id): self.assertEqual(card['name'], graph.name) self.assertEqual(card['description'], graph.description) else: self.assertTrue(len(graph.nodes[card['nodegroup_id']].name) > 0) self.assertTrue(len(graph.nodes[card['nodegroup_id']].description) > 0) self.assertEqual(card['name'], graph.nodes[card['nodegroup_id']].name) self.assertEqual(card['description'], graph.nodes[card['nodegroup_id']].description) # TESTING A RESOURCE resource_graph = Graph.new(name='TEST RESOURCE',is_resource=True,author='TEST') resource_graph.description = 'A test resource description' resource_graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=graph.graphid) resource_graph.save() self.assertEqual(len(resource_graph.get_cards()), 2) for card in resource_graph.get_cards(): cardobj = Card.objects.get(pk=card['cardid']) if cardobj.nodegroup.parentnodegroup is None: self.assertEqual(card['name'], graph.name) self.assertEqual(card['description'], graph.description) else: self.assertEqual(card['name'], resource_graph.nodes[card['nodegroup_id']].name) self.assertEqual(card['description'], resource_graph.nodes[card['nodegroup_id']].description) self.assertTrue(len(resource_graph.nodes[card['nodegroup_id']].name) > 0) self.assertTrue(len(resource_graph.nodes[card['nodegroup_id']].description) > 0) resource_graph.delete() # TESTING A RESOURCE resource_graph = Graph.new(name='TEST',is_resource=True,author='TEST') resource_graph.description = 'A test description' resource_graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) resource_graph.save() self.assertEqual(len(resource_graph.cards), 1) the_card = next(iter(list(resource_graph.cards.values()))) for card in resource_graph.get_cards(): self.assertEqual(card['name'], the_card.name) self.assertEqual(card['description'], the_card.description) # after removing the card name and description, the cards should take on the node name and description the_card.name = '' the_card.description = '' for card in resource_graph.get_cards(): self.assertEqual(card['name'], resource_graph.nodes[card['nodegroup_id']].name) self.assertEqual(card['description'], resource_graph.nodes[card['nodegroup_id']].description)
def test_copy_graph(self): """ test that a copy of a graph has the same number of nodes and edges and that the primary keys have been changed and that the actual node references are different """ graph = Graph.new(name='TEST RESOURCE') graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID) graph_copy = graph.copy()['copy'] self.assertEqual(len(graph_copy.nodes), 3) self.assertEqual(len(graph_copy.edges), 2) self.assertEqual(len(graph_copy.cards), 2) self.assertEqual(len(graph_copy.get_nodegroups()), 2) self.assertEqual(len(graph.nodes), len(graph_copy.nodes)) self.assertEqual(len(graph.edges), len(graph_copy.edges)) self.assertEqual(len(graph.cards), len(graph_copy.cards)) self.assertEqual(len(graph.get_nodegroups()), len(graph_copy.get_nodegroups())) # assert the copied nodegroup heirarchy is maintained for nodegroup in graph_copy.get_nodegroups(): if graph_copy.nodes[nodegroup.pk] is graph_copy.root: parentnodegroup_copy = nodegroup; else: childnodegroup_copy = nodegroup self.assertTrue(parentnodegroup_copy.parentnodegroup is None) self.assertEqual(childnodegroup_copy.parentnodegroup, parentnodegroup_copy) self.assertFalse(parentnodegroup_copy.parentnodegroup_id) self.assertEqual(childnodegroup_copy.parentnodegroup_id, parentnodegroup_copy.pk) # assert the copied node groups are not equal to the originals for nodegroup in graph.get_nodegroups(): if graph.nodes[nodegroup.pk] is graph.root: parentnodegroup = nodegroup; else: childnodegroup = nodegroup self.assertNotEqual(parentnodegroup, parentnodegroup_copy) self.assertNotEqual(parentnodegroup.pk, parentnodegroup_copy.pk) self.assertNotEqual(childnodegroup, childnodegroup_copy) self.assertNotEqual(childnodegroup.pk, childnodegroup_copy.pk) # assert the nodegroups attached to the cards are heirarchically correct for card in graph_copy.cards.itervalues(): if str(card.nodegroup_id) == str(graph_copy.root.nodeid): parentcard_copy = card else: childcard_copy = card self.assertTrue(parentcard_copy.nodegroup is not None) self.assertTrue(childcard_copy.nodegroup is not None) self.assertTrue(parentcard_copy.nodegroup.parentnodegroup is None) self.assertTrue(childcard_copy.nodegroup.parentnodegroup is not None) self.assertEqual(parentcard_copy.nodegroup, childcard_copy.nodegroup.parentnodegroup) def findNodeByName(graph, name): for node in graph.nodes.itervalues(): if node.name == name: return node return None def findCardByName(graph, name): for card in graph.cards.itervalues(): if card.name == name: return card return None for node in graph.nodes.itervalues(): node_copy = findNodeByName(graph_copy, node.name) self.assertIsNotNone(node_copy) self.assertNotEqual(node.pk, node_copy.pk) self.assertNotEqual(id(node), id(node_copy)) self.assertEqual(node.is_collector, node_copy.is_collector) if node.nodegroup != None: self.assertNotEqual(node.nodegroup, node_copy.nodegroup) for card in graph.cards.itervalues(): card_copy = findCardByName(graph_copy, card.name) self.assertIsNotNone(card_copy) self.assertNotEqual(card.pk, card_copy.pk) self.assertNotEqual(id(card), id(card_copy)) self.assertNotEqual(card.nodegroup, card_copy.nodegroup) for newedge in graph_copy.edges.itervalues(): self.assertIsNotNone(graph_copy.nodes[newedge.domainnode_id]) self.assertIsNotNone(graph_copy.nodes[newedge.rangenode_id]) self.assertEqual(newedge.domainnode, graph_copy.nodes[newedge.domainnode.pk]) self.assertEqual(newedge.rangenode, graph_copy.nodes[newedge.rangenode.pk]) with self.assertRaises(KeyError): graph.edges[newedge.pk]
def post(self, request, graphid=None): ret = {} try: if self.action == "import_graph": graph_file = request.FILES.get("importedGraph").read() graphs = JSONDeserializer().deserialize(graph_file)["graph"] ret = GraphImporter.import_graph(graphs) else: if graphid is not None: graph = Graph.objects.get(graphid=graphid) data = JSONDeserializer().deserialize(request.body) if self.action == "new_graph": isresource = data[ "isresource"] if "isresource" in data else False name = _("New Resource Model") if isresource else _( "New Branch") author = request.user.first_name + " " + request.user.last_name ret = Graph.new(name=name, is_resource=isresource, author=author) elif self.action == "update_node": old_node_data = graph.nodes.get(uuid.UUID(data["nodeid"])) nodegroup_changed = str( old_node_data.nodegroup_id) != data["nodegroup_id"] updated_values = graph.update_node(data) if "nodeid" in data and nodegroup_changed is False: graph.save(nodeid=data["nodeid"]) else: graph.save() ret = JSONSerializer().serializeToPython(graph) ret["updated_values"] = updated_values ret["default_card_name"] = graph.temp_node_name elif self.action == "update_node_layer": nodeid = uuid.UUID(str(data.get("nodeid"))) node = graph.nodes[nodeid] node.config = data["config"] ret = graph node.save() elif self.action == "append_branch": ret = graph.append_branch(data["property"], nodeid=data["nodeid"], graphid=data["graphid"]) ret = ret.serialize() ret["nodegroups"] = graph.get_nodegroups() ret["cards"] = graph.get_cards() ret["widgets"] = graph.get_widgets() graph.save() elif self.action == "append_node": ret = graph.append_node(nodeid=data["nodeid"]) graph.save() elif self.action == "move_node": ret = graph.move_node(data["nodeid"], data["property"], data["newparentnodeid"]) graph.save() elif self.action == "export_branch": clone_data = graph.copy(root=data) clone_data["copy"].slug = None clone_data["copy"].save() ret = {"success": True, "graphid": clone_data["copy"].pk} elif self.action == "clone_graph": clone_data = graph.copy() ret = clone_data["copy"] ret.slug = None ret.save() ret.copy_functions( graph, [clone_data["nodes"], clone_data["nodegroups"]]) elif self.action == "reorder_nodes": json = request.body if json is not None: data = JSONDeserializer().deserialize(json) if "nodes" in data and len(data["nodes"]) > 0: sortorder = 0 with transaction.atomic(): for node in data["nodes"]: no = models.Node.objects.get( pk=node["nodeid"]) no.sortorder = sortorder no.save() sortorder = sortorder + 1 ret = data return JSONResponse(ret) except GraphValidationError as e: return JSONErrorResponse(e.title, e.message, {"status": "Failed"}) except ModelInactiveError as e: return JSONErrorResponse(e.title, e.message) except RequestError as e: return JSONErrorResponse( _("Elasticsearch indexing error"), _("""If you want to change the datatype of an existing node. Delete and then re-create the node, or export the branch then edit the datatype and re-import the branch.""" ), )
def post(self, request, graphid=None): ret = {} try: if self.action == "import_graph": graph_file = request.FILES.get("importedGraph").read() graphs = JSONDeserializer().deserialize(graph_file)["graph"] ret = GraphImporter.import_graph(graphs) else: if graphid is not None: graph = Graph.objects.get(graphid=graphid) data = JSONDeserializer().deserialize(request.body) if self.action == "new_graph": isresource = data[ "isresource"] if "isresource" in data else False name = _("New Resource Model") if isresource else _( "New Branch") author = request.user.first_name + " " + request.user.last_name ret = Graph.new(name=name, is_resource=isresource, author=author) elif self.action == "update_node": updated_values = graph.update_node(data) graph.save() ret = JSONSerializer().serializeToPython(graph) ret["updated_values"] = updated_values elif self.action == "update_node_layer": nodeid = uuid.UUID(str(data.get("nodeid"))) node = graph.nodes[nodeid] node.config = data["config"] ret = graph node.save() elif self.action == "append_branch": ret = graph.append_branch(data["property"], nodeid=data["nodeid"], graphid=data["graphid"]) ret = ret.serialize() ret["nodegroups"] = graph.get_nodegroups() ret["cards"] = graph.get_cards() ret["widgets"] = graph.get_widgets() graph.save() elif self.action == "append_node": ret = graph.append_node(nodeid=data["nodeid"]) graph.save() elif self.action == "move_node": ret = graph.move_node(data["nodeid"], data["property"], data["newparentnodeid"]) graph.save() elif self.action == "export_branch": clone_data = graph.copy(root=data) clone_data["copy"].save() ret = {"success": True, "graphid": clone_data["copy"].pk} elif self.action == "clone_graph": clone_data = graph.copy() ret = clone_data["copy"] ret.save() ret.copy_functions( graph, [clone_data["nodes"], clone_data["nodegroups"]]) elif self.action == "reorder_nodes": json = request.body if json is not None: data = JSONDeserializer().deserialize(json) if "nodes" in data and len(data["nodes"]) > 0: sortorder = 0 with transaction.atomic(): for node in data["nodes"]: no = models.Node.objects.get( pk=node["nodeid"]) no.sortorder = sortorder no.save() sortorder = sortorder + 1 ret = data return JSONResponse(ret) except GraphValidationError as e: return JSONResponse( { "status": "false", "success": False, "message": e.message, "title": e.title }, status=500)