def setUpClass(cls): cls.loadOntology() with open( os.path.join( "tests/fixtures/resource_graphs/unique_graph_shape.json"), "rU") as f: json = JSONDeserializer().deserialize(f) cls.unique_graph = Graph(json["graph"][0]) cls.unique_graph.save() with open( os.path.join( "tests/fixtures/resource_graphs/ambiguous_graph_shape.json" ), "rU") as f: json = JSONDeserializer().deserialize(f) cls.ambiguous_graph = Graph(json["graph"][0]) cls.ambiguous_graph.save() with open( os.path.join( "tests/fixtures/resource_graphs/phase_type_assignment.json" ), "rU") as f: json = JSONDeserializer().deserialize(f) cls.phase_type_assignment_graph = Graph(json["graph"][0]) cls.phase_type_assignment_graph.save()
def setUpClass(cls): geojson_nodeid = "3ebc6785-fa61-11e6-8c85-14109fd34195" cls.loadOntology() with open( os.path.join( "tests/fixtures/resource_graphs/unique_graph_shape.json"), "rU") as f: json = JSONDeserializer().deserialize(f) cls.unique_graph = Graph(json["graph"][0]) cls.unique_graph.save() with open( os.path.join( "tests/fixtures/resource_graphs/ambiguous_graph_shape.json" ), "rU") as f: json = JSONDeserializer().deserialize(f) cls.ambiguous_graph = Graph(json["graph"][0]) cls.ambiguous_graph.save() with open( os.path.join( "tests/fixtures/resource_graphs/phase_type_assignment.json" ), "rU") as f: json = JSONDeserializer().deserialize(f) cls.phase_type_assignment_graph = Graph(json["graph"][0]) cls.phase_type_assignment_graph.save()
def setUpClass(cls): geojson_nodeid = "3ebc6785-fa61-11e6-8c85-14109fd34195" cls.loadOntology() with open( os.path.join( "tests/fixtures/resource_graphs/unique_graph_shape.json"), "rU") as f: json = JSONDeserializer().deserialize(f) cls.unique_graph = Graph(json["graph"][0]) cls.unique_graph.save() with open( os.path.join( "tests/fixtures/resource_graphs/ambiguous_graph_shape.json" ), "rU") as f: json = JSONDeserializer().deserialize(f) cls.ambiguous_graph = Graph(json["graph"][0]) cls.ambiguous_graph.save() with open( os.path.join( "tests/fixtures/resource_graphs/phase_type_assignment.json" ), "rU") as f: json = JSONDeserializer().deserialize(f) cls.phase_type_assignment_graph = Graph(json["graph"][0]) cls.phase_type_assignment_graph.save() # Load the test package to provide resources graph. test_pkg_path = os.path.join(test_settings.TEST_ROOT, "fixtures", "testing_prj", "testing_prj", "pkg") management.call_command("packages", operation="load_package", source=test_pkg_path, yes=True)
def load_resource_graph_file(path_to_file): if isfile(path_to_file) and path_to_file.endswith(suffix): basepath = path_to_file name = basepath.split(os.sep)[-1] with codecs.open(basepath, 'rU', encoding='utf-8') as f: file = json.load(f) resource_graph = Graph(file['graph'][0]) resource_graph.save()
def import_graph(graphs): reporter = GraphImportReporter(graphs) with transaction.atomic(): errors = [] for resource in graphs: reporter.name = resource['name'] reporter.resource_model = resource['isresource'] graph = Graph(resource) if not hasattr(graph, 'cards'): errors.append('{0} graph has no attribute cards'.format( graph.name)) else: if graph.cards == [] or graph.cards == {}: errors.append('{0} graph has no cards'.format(graph.name)) else: graph.save() reporter.update_graphs_saved() if not hasattr(graph, 'cards_x_nodes_x_widgets'): errors.append( '{0} graph has no attribute cards_x_nodes_x_widgets'. format(graph.name)) else: for card_x_node_x_widget in graph.cards_x_nodes_x_widgets: cardxnodexwidget = CardXNodeXWidget.objects.update_or_create( **card_x_node_x_widget) if not hasattr(graph, 'forms'): errors.append('{0} graph has no attribute forms'.format) else: for form in graph.forms: form = Form.objects.update_or_create(**form) reporter.update_forms_saved() if not hasattr(graph, 'forms_x_cards'): errors.append( '{0} graph has no attribute forms_x_cards'.format( graph.name)) else: for form_x_card in graph.forms_x_cards: formxcard = FormXCard.objects.update_or_create( **form_x_card) if not hasattr(graph, 'reports'): errors.append('{0} graph has no attribute reports'.format( graph.name)) else: for report in graph.reports: report = Report.objects.update_or_create(**report) reporter.update_reports_saved() return errors, reporter
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 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 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 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 import_graph(graphs): with transaction.atomic(): for resource in graphs: graph = Graph(resource) graph.save() if not hasattr(graph, 'cards_x_nodes_x_widgets'): print '*********This graph has no attribute cards_x_nodes_x_widgets*********' sys.exit() else: for card_x_node_x_widget in graph.cards_x_nodes_x_widgets: functions = card_x_node_x_widget['functions'] card_x_node_x_widget.pop('functions', None) cardxnodexwidget = CardXNodeXWidget.objects.create(**card_x_node_x_widget) cardxnodexwidget.save() cardxnodexwidget.functions.set(functions) if not hasattr(graph, 'forms'): print '*********This graph has no attribute forms*********' sys.exit() else: for form in graph.forms: form = Form.objects.create(**form) form.save() if not hasattr(graph, 'forms_x_cards'): print '*********This graph has no attribute forms_x_cards*********' sys.exit() else: for form_x_card in graph.forms_x_cards: formxcard = FormXCard.objects.create(**form_x_card) formxcard.save() if not hasattr(graph, 'reports'): print '*********This graph has no attribute reports*********' sys.exit() else: for report in graph.reports: report = Report.objects.create(**report) report.save() return Graph
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('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_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_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('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('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 setUpClass(cls): with open( os.path.join( 'tests/fixtures/resource_graphs/unique_graph_shape.json'), 'rU') as f: json = JSONDeserializer().deserialize(f) cls.unique_graph = Graph(json['graph'][0]) cls.unique_graph.save() with open( os.path.join( 'tests/fixtures/resource_graphs/ambiguous_graph_shape.json' ), 'rU') as f: json = JSONDeserializer().deserialize(f) cls.ambiguous_graph = Graph(json['graph'][0]) cls.ambiguous_graph.save() with open( os.path.join( 'tests/fixtures/resource_graphs/phase_type_assignment.json' ), 'rU') as f: json = JSONDeserializer().deserialize(f) cls.phase_type_assignment_graph = Graph(json['graph'][0]) cls.phase_type_assignment_graph.save()
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 """ root = models.Node.objects.get(pk=self.HERITAGE_RESOURCE_FIXTURE) graph = Graph(root) graph_copy = graph.copy() self.assertEqual(len(graph.nodes), len(graph_copy.nodes)) self.assertEqual(len(graph.edges), len(graph_copy.edges)) self.assertEqual(len(graph.nodegroups), len(graph_copy.nodegroups)) def findNodeByName(graph, name): for key, node in graph.nodes.iteritems(): if node.name == name: return node return None for key, node in graph.nodes.iteritems(): 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 key, newedge in graph_copy.edges.iteritems(): 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_branch_append(self): """ test if a branch is properly appended to a graph """ nodes_count_before = models.Node.objects.count() edges_count_before = models.Edge.objects.count() nodegroups_count_before = models.NodeGroup.objects.count() graph = Graph(self.rootNode) graph.append_branch('P1', graphid=self.NODE_NODETYPE_GRAPHID) graph.save() self.assertEqual(len(graph.nodes), 3) self.assertEqual(len(graph.edges), 2) self.assertEqual(len(graph.nodegroups), 2) self.assertEqual(models.Node.objects.count()-nodes_count_before, 2) self.assertEqual(models.Edge.objects.count()-edges_count_before, 2) self.assertEqual(models.NodeGroup.objects.count()-nodegroups_count_before, 1) for key, edge in graph.edges.iteritems(): self.assertIsNotNone(graph.nodes[edge.domainnode_id]) self.assertIsNotNone(graph.nodes[edge.rangenode_id]) self.assertEqual(edge.domainnode, graph.nodes[edge.domainnode.pk]) self.assertEqual(edge.rangenode, graph.nodes[edge.rangenode.pk]) for key, node in graph.nodes.iteritems(): if node.istopnode: self.assertEqual(node, self.rootNode) appended_branch = graph.append_branch('P1', graphid=self.SINGLE_NODE_GRAPHID) graph.save() self.assertEqual(len(graph.nodes), 4) self.assertEqual(len(graph.edges), 3) self.assertEqual(len(graph.nodegroups), 2) self.assertEqual(models.Node.objects.count()-nodes_count_before, 3) self.assertEqual(models.Edge.objects.count()-edges_count_before, 3) self.assertEqual(models.NodeGroup.objects.count()-nodegroups_count_before, 1) self.assertEqual(appended_branch.root.nodegroup,self.rootNode.nodegroup)
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 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 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 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_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 import_graph(graphs, overwrite_graphs=True): reporter = GraphImportReporter(graphs) def check_default_configs(default_configs, configs): if default_configs is not None: if configs is None: configs = {} else: try: '' in configs # Checking if configs is a dict-like object except AttributeError: configs = JSONDeserializer().deserialize(configs) for default_key in default_configs: if default_key not in configs: configs[default_key] = default_configs[default_key] return configs with transaction.atomic(): errors = [] for resource in graphs: try: if resource['ontology_id'] is not None: if resource['ontology_id'] not in [ str(f['ontologyid']) for f in Ontology.objects.all().values('ontologyid') ]: errors.append( 'The ontologyid of the graph you\'re trying to load does not exist in Arches.' ) reporter.name = resource['name'] reporter.resource_model = resource['isresource'] reporter.graph_id = resource['graphid'] graph = Graph(resource) ontology_classes = [ str(f['source']) for f in OntologyClass.objects.all().values('source') ] for node in list(graph.nodes.values()): if resource['ontology_id'] is not None: if node.ontologyclass not in ontology_classes: errors.append( 'The ontology class of this node does not exist in the indicated ontology scheme.' ) node_config = node.config default_config = DDataType.objects.get( datatype=node.datatype).defaultconfig node.config = check_default_configs( default_config, node_config) if not hasattr(graph, 'cards'): errors.append('{0} graph has no attribute cards'.format( graph.name)) else: if len(Graph.objects.filter(pk=graph.graphid) ) == 0 or overwrite_graphs is True: if hasattr(graph, 'reports'): for report in graph.reports: if report['active']: report_config = report['config'] default_config = ReportTemplate.objects.get( templateid=report['template_id'] ).defaultconfig graph.config = check_default_configs( default_config, report_config) graph.template_id = report['template_id'] graph.save() reporter.update_graphs_saved() else: overwrite_input = input( 'Overwrite {0} (Y/N) ? '.format(graph.name)) if overwrite_input.lower() in ('t', 'true', 'y', 'yes'): graph.save() else: raise GraphImportException( '{0} - already exists. Skipping import.'. format(graph.name)) if not hasattr(graph, 'cards_x_nodes_x_widgets'): errors.append( '{0} graph has no attribute cards_x_nodes_x_widgets'. format(graph.name)) else: for card_x_node_x_widget in graph.cards_x_nodes_x_widgets: card_x_node_x_widget_config = card_x_node_x_widget[ 'config'] default_config = Widget.objects.get( widgetid=card_x_node_x_widget['widget_id'] ).defaultconfig card_x_node_x_widget['config'] = check_default_configs( default_config, card_x_node_x_widget_config) cardxnodexwidget = CardXNodeXWidget.objects.update_or_create( **card_x_node_x_widget) # try/except block here until all graphs have a resource_2_resource_constraints object. try: if not hasattr(graph, 'resource_2_resource_constraints'): errors.append( '{0} graph has no attribute resource_2_resource_constraints' .format(graph.resource_2_resource_constraints)) else: for resource_2_resource_constraint in graph.resource_2_resource_constraints: resource2resourceconstraint = Resource2ResourceConstraint.objects.update_or_create( **resource_2_resource_constraint) except: pass except Exception as e: print(e) return errors, reporter
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 import_graph(graphs): reporter = GraphImportReporter(graphs) def check_default_configs(default_configs, configs): if default_configs != None: if configs == None: configs = {} else: try: configs.has_key('') #Checking if configs is a dict-like object except AttributeError: configs = JSONDeserializer().deserialize(configs) for default_key in default_configs: if default_key not in configs: configs[default_key] = default_configs[default_key] return configs with transaction.atomic(): errors = [] for resource in graphs: reporter.name = resource['name'] reporter.resource_model = resource['isresource'] graph = Graph(resource) for node in graph.nodes.values(): node_config = node.config default_config = DDataType.objects.get(datatype=node.datatype).defaultconfig node.config = check_default_configs(default_config, node_config) if not hasattr(graph, 'cards'): errors.append('{0} graph has no attribute cards'.format(graph.name)) else: if graph.cards == [] or graph.cards == {}: errors.append('{0} graph has no cards'.format(graph.name)) else: graph.save() reporter.update_graphs_saved() if not hasattr(graph, 'cards_x_nodes_x_widgets'): errors.append('{0} graph has no attribute cards_x_nodes_x_widgets'.format(graph.name)) else: for card_x_node_x_widget in graph.cards_x_nodes_x_widgets: card_x_node_x_widget_config = card_x_node_x_widget['config'] default_config = Widget.objects.get(widgetid=card_x_node_x_widget['widget_id']).defaultconfig card_x_node_x_widget['config'] = check_default_configs(default_config, card_x_node_x_widget_config) cardxnodexwidget = CardXNodeXWidget.objects.update_or_create(**card_x_node_x_widget) if not hasattr(graph, 'forms'): errors.append('{0} graph has no attribute forms'.format) else: for form in graph.forms: form = Form.objects.update_or_create(**form) reporter.update_forms_saved() if not hasattr(graph, 'forms_x_cards'): errors.append('{0} graph has no attribute forms_x_cards'.format(graph.name)) else: for form_x_card in graph.forms_x_cards: formxcard = FormXCard.objects.update_or_create(**form_x_card) if not hasattr(graph, 'reports'): errors.append('{0} graph has no attribute reports'.format(graph.name)) else: for report in graph.reports: report_config = report['config'] default_config = ReportTemplate.objects.get(templateid=report['template_id']).defaultconfig report['config'] = check_default_configs(default_config, report_config) report = Report.objects.update_or_create(**report) reporter.update_reports_saved() # try/except block here until all graphs have a resource_2_resource_constraints object. try: if not hasattr(graph, 'resource_2_resource_constraints'): errors.append('{0} graph has no attribute resource_2_resource_constraints'.format(graph.resource_2_resource_constraints)) else: for resource_2_resource_constraint in graph.resource_2_resource_constraints: resource2resourceconstraint = Resource2ResourceConstraint.objects.update_or_create(**resource_2_resource_constraint) except: pass return errors, reporter
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 import_graph(graphs, overwrite_graphs=True): reporter = GraphImportReporter(graphs) def check_default_configs(default_configs, configs): if default_configs != None: if configs == None: configs = {} else: try: configs.has_key('') #Checking if configs is a dict-like object except AttributeError: configs = JSONDeserializer().deserialize(configs) for default_key in default_configs: if default_key not in configs: configs[default_key] = default_configs[default_key] return configs with transaction.atomic(): errors = [] for resource in graphs: try: if resource['ontology_id'] != None: if resource['ontology_id'] not in [str(f['ontologyid']) for f in Ontology.objects.all().values('ontologyid')]: errors.append('The ontologyid of the graph you\'re trying to load does not exist in Arches.') reporter.name = resource['name'] reporter.resource_model = resource['isresource'] graph = Graph(resource) ontology_classes = [str(f['source']) for f in OntologyClass.objects.all().values('source')] for node in graph.nodes.values(): if resource['ontology_id'] != None: if node.ontologyclass not in ontology_classes: errors.append('The ontology class of this node does not exist in the indicated ontology scheme.') node_config = node.config default_config = DDataType.objects.get(datatype=node.datatype).defaultconfig node.config = check_default_configs(default_config, node_config) if not hasattr(graph, 'cards'): errors.append('{0} graph has no attribute cards'.format(graph.name)) else: if graph.cards == [] or graph.cards == {}: errors.append('{0} graph has no cards'.format(graph.name)) else: if len(Graph.objects.filter(pk=graph.graphid)) == 0 or overwrite_graphs == True: graph.save() reporter.update_graphs_saved() else: overwrite_input = raw_input('Overwrite {0} (Y/N) ? '.format(graph.name)) if overwrite_input.lower() in ('t', 'true', 'y', 'yes'): graph.save() else: raise GraphImportException('{0} - already exists. Skipping import.'.format(graph.name)) if not hasattr(graph, 'cards_x_nodes_x_widgets'): errors.append('{0} graph has no attribute cards_x_nodes_x_widgets'.format(graph.name)) else: for card_x_node_x_widget in graph.cards_x_nodes_x_widgets: card_x_node_x_widget_config = card_x_node_x_widget['config'] default_config = Widget.objects.get(widgetid=card_x_node_x_widget['widget_id']).defaultconfig card_x_node_x_widget['config'] = check_default_configs(default_config, card_x_node_x_widget_config) cardxnodexwidget = CardXNodeXWidget.objects.update_or_create(**card_x_node_x_widget) if not hasattr(graph, 'forms'): errors.append('{0} graph has no attribute forms'.format) else: for form in graph.forms: form = Form.objects.update_or_create(**form) reporter.update_forms_saved() if not hasattr(graph, 'forms_x_cards'): errors.append('{0} graph has no attribute forms_x_cards'.format(graph.name)) else: for form_x_card in graph.forms_x_cards: formxcard = FormXCard.objects.update_or_create(**form_x_card) if not hasattr(graph, 'reports'): errors.append('{0} graph has no attribute reports'.format(graph.name)) else: for report in graph.reports: report_config = report['config'] default_config = ReportTemplate.objects.get(templateid=report['template_id']).defaultconfig report['config'] = check_default_configs(default_config, report_config) report = Report.objects.update_or_create(**report) reporter.update_reports_saved() # try/except block here until all graphs have a resource_2_resource_constraints object. try: if not hasattr(graph, 'resource_2_resource_constraints'): errors.append('{0} graph has no attribute resource_2_resource_constraints'.format(graph.resource_2_resource_constraints)) else: for resource_2_resource_constraint in graph.resource_2_resource_constraints: resource2resourceconstraint = Resource2ResourceConstraint.objects.update_or_create(**resource_2_resource_constraint) except: pass except Exception as e: print e return errors, reporter
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_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('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_move_node(self): """ test if a node can be successfully moved to another node in the graph """ # test moving a single node to another branch # this node should be grouped with it's new parent nodegroup graph = Graph(self.rootNode) branch_one = graph.append_branch('P1', graphid=self.NODE_NODETYPE_GRAPHID) branch_two = graph.append_branch('P1', graphid=self.NODE_NODETYPE_GRAPHID) branch_three = graph.append_branch('P1', graphid=self.SINGLE_NODE_GRAPHID) branch_three_nodeid = branch_three.nodes.iterkeys().next() branch_one_rootnodeid = branch_one.root.nodeid graph.move_node(branch_three_nodeid, 'P1', branch_one_rootnodeid) new_parent_nodegroup = None moved_branch_nodegroup = None for node_id, node in graph.nodes.iteritems(): if node_id == branch_one_rootnodeid: new_parent_nodegroup = node.nodegroup if node_id == branch_three_nodeid: moved_branch_nodegroup = node.nodegroup self.assertIsNotNone(new_parent_nodegroup) self.assertIsNotNone(moved_branch_nodegroup) self.assertEqual(new_parent_nodegroup, moved_branch_nodegroup) # test moving a branch to another branch # this branch should NOT be grouped with it's new parent nodegroup branch_two_rootnodeid = branch_two.root.nodeid graph.move_node(branch_one_rootnodeid, 'P1', branch_two_rootnodeid) new_parent_nodegroup = None moved_branch_nodegroup = None for node_id, node in graph.nodes.iteritems(): if node_id == branch_two_rootnodeid: new_parent_nodegroup = node.nodegroup if node_id == branch_one_rootnodeid: moved_branch_nodegroup = node.nodegroup self.assertIsNotNone(new_parent_nodegroup) self.assertIsNotNone(moved_branch_nodegroup) self.assertNotEqual(new_parent_nodegroup, moved_branch_nodegroup) updated_edge = None for edge_id, edge in graph.edges.iteritems(): if (edge.domainnode_id == branch_two_rootnodeid and edge.rangenode_id == branch_one_rootnodeid): updated_edge = edge self.assertIsNotNone(updated_edge) # save and retrieve the graph from the database and confirm that # the graph shape has been saved properly graph.save() graph = Graph(self.rootNode) tree = graph.get_tree() self.assertEqual(len(tree['children']), 1) level_one_node = tree['children'][0] self.assertEqual(branch_two_rootnodeid, level_one_node['node'].nodeid) self.assertEqual(len(level_one_node['children']), 2) for child in level_one_node['children']: if child['node'].nodeid == branch_one_rootnodeid: self.assertEqual(len(child['children']), 2) found_branch_three = False for child in child['children']: if child['node'].nodeid == branch_three_nodeid: found_branch_three = True self.assertTrue(found_branch_three) else: self.assertEqual(len(child['children']), 0)
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 import_graph(graphs, overwrite_graphs=True): reporter = GraphImportReporter(graphs) def check_default_configs(default_configs, configs): if default_configs is not None: if configs is None: configs = {} else: try: "" in configs # Checking if configs is a dict-like object except AttributeError: configs = JSONDeserializer().deserialize(configs) for default_key in default_configs: if default_key not in configs: configs[default_key] = default_configs[default_key] return configs with transaction.atomic(): errors = [] for resource in graphs: if len(list(OntologyClass.objects.all())) > 0: if resource["ontology_id"] is not None and resource[ "ontology_id"] not in [ str(f["ontologyid"]) for f in Ontology.objects.all().values("ontologyid") ]: errors.append( "The ontologyid of the graph you're trying to load does not exist in Arches." ) else: errors.append( "No ontologies have been loaded. Any GraphModel that depends on an ontology cannot be loaded." ) reporter.name = resource["name"] reporter.resource_model = resource["isresource"] reporter.graph_id = resource["graphid"] try: graph = Graph(resource) ontology_classes = [ str(f["source"]) for f in OntologyClass.objects.all().values("source") ] for node in list(graph.nodes.values()): if resource["ontology_id"] is not None: if node.ontologyclass not in ontology_classes: errors.append( "The ontology class of this node does not exist in the indicated ontology scheme." ) node_config = node.config default_config = DDataType.objects.get( datatype=node.datatype).defaultconfig node.config = check_default_configs( default_config, node_config) if not hasattr(graph, "cards"): errors.append("{0} graph has no attribute cards".format( graph.name)) else: if len(Graph.objects.filter(pk=graph.graphid) ) == 0 or overwrite_graphs is True: if hasattr(graph, "reports"): for report in graph.reports: if report["active"]: report_config = report["config"] default_config = ReportTemplate.objects.get( templateid=report["template_id"] ).defaultconfig graph.config = check_default_configs( default_config, report_config) graph.template_id = report["template_id"] graph.save() reporter.update_graphs_saved() else: overwrite_input = input( "Overwrite {0} (Y/N) ? ".format(graph.name)) if overwrite_input.lower() in ("t", "true", "y", "yes"): graph.save() else: raise GraphImportException( "{0} - already exists. Skipping import.". format(graph.name)) if not hasattr(graph, "cards_x_nodes_x_widgets"): errors.append( "{0} graph has no attribute cards_x_nodes_x_widgets". format(graph.name)) else: for card_x_node_x_widget in graph.cards_x_nodes_x_widgets: card_x_node_x_widget_config = card_x_node_x_widget[ "config"] default_config = Widget.objects.get( widgetid=card_x_node_x_widget["widget_id"] ).defaultconfig card_x_node_x_widget["config"] = check_default_configs( default_config, card_x_node_x_widget_config) cardxnodexwidget = CardXNodeXWidget.objects.update_or_create( **card_x_node_x_widget) except GraphImportException as ge: errors.append(ge) except Exception as e: errors.append( f"Could not define graph. Its resources were not loaded.") errors.append(e) # try/except block here until all graphs have a resource_2_resource_constraints object. try: if not hasattr(graph, "resource_2_resource_constraints"): errors.append( "{0} graph has no attribute resource_2_resource_constraints" .format(graph.resource_2_resource_constraints)) else: for resource_2_resource_constraint in graph.resource_2_resource_constraints: resource2resourceconstraint = Resource2ResourceConstraint.objects.update_or_create( **resource_2_resource_constraint) except: pass return errors, reporter
def test_graph_doesnt_polute_db(self): """ test that the mere act of creating a Graph instance doesn't save anything to the database """ graph_obj = { "name": "TEST GRAPH", "subtitle": "ARCHES TEST GRAPH", "author": "Arches", "description": "ARCHES TEST GRAPH", "version": "v1.0.0", "isresource": True, "isactive": False, "iconclass": "fa fa-building", 'nodes':[{ "status": None, "description": "", "name": "ROOT_NODE", "istopnode": True, "ontologyclass": "", "nodeid": "55555555-343e-4af3-8857-f7322dc9eb4b", "nodegroup_id": "", "datatype": "semantic" },{ "status": None, "description": "", "name": "NODE_NAME", "istopnode": False, "ontologyclass": "", "nodeid": "66666666-24c9-4226-bde2-2c40ee60a26c", "nodegroup_id": "66666666-24c9-4226-bde2-2c40ee60a26c", "datatype": "string" }], 'edges':[{ "rangenodeid": "66666666-24c9-4226-bde2-2c40ee60a26c", "name": "", "edgeid": "11111111-d50f-11e5-8754-80e6500ee4e4", "domainnodeid": "55555555-343e-4af3-8857-f7322dc9eb4b", "ontologyproperty": "P2", "description": "" }], 'cards':[{ "name": "NODE_NAME", "description": "", "instructions": "", "helptext": "", "cardinality": "n", "nodegroup_id": "66666666-24c9-4226-bde2-2c40ee60a26c" }] } nodes_count_before = models.Node.objects.count() edges_count_before = models.Edge.objects.count() cards_count_before = models.CardModel.objects.count() nodegroups_count_before = models.NodeGroup.objects.count() graph = Graph(graph_obj) self.assertEqual(models.Node.objects.count()-nodes_count_before, 0) self.assertEqual(models.Edge.objects.count()-edges_count_before, 0) self.assertEqual(models.CardModel.objects.count()-cards_count_before, 0) self.assertEqual(models.NodeGroup.objects.count()-nodegroups_count_before, 0) self.assertEqual(graph_obj['name'], graph.name) self.assertEqual(graph_obj['subtitle'], graph.subtitle) self.assertEqual(graph_obj['author'], graph.author) self.assertEqual(graph_obj['description'], graph.description) self.assertEqual(graph_obj['version'], graph.version) self.assertEqual(graph_obj['isresource'], graph.isresource) self.assertEqual(graph_obj['isactive'], graph.isactive) self.assertEqual(graph_obj['iconclass'], graph.iconclass)
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 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)