class DatabaseTestCase(TestCase): def setUp(self): try: rmtree('/tmp/ajgudb') except OSError: pass os.makedirs('/tmp/ajgudb') self.graph = AjguDB('/tmp/ajgudb') def tearDown(self): self.graph.close() rmtree('/tmp/ajgudb') def test_create_vertex(self): v = self.graph.vertex.create('test') self.assertTrue(v) def test_idem(self): v = self.graph.vertex.create('test') idem = self.graph.vertex.get(v.uid) self.assertEqual(v, idem) def test_different_same_type(self): v1 = self.graph.vertex.create('test') v2 = self.graph.vertex.create('test') self.assertNotEqual(v1, v2) def test_different_different_type(self): start = self.graph.vertex.create('test') end = self.graph.vertex.create('test') edge = start.link('link', end) self.assertNotEqual(start, edge) def test_get_or_create(self): v = self.graph.vertex.get_or_create('test') self.assertIsNotNone(v) def test_get_or_create_twice(self): v1 = self.graph.vertex.get_or_create(label='test', key='value') v2 = self.graph.vertex.get_or_create(label='test', key='value') self.assertEqual(v1, v2) def test_create_and_get_vertex(self): v1 = self.graph.vertex.create('test') v2 = self.graph.vertex.get(v1.uid) self.assertTrue(v1, v2) def test_create_with_properties_and_get_vertex(self): v = self.graph.vertex.create('test', key='value') v = self.graph.vertex.get(v.uid) self.assertEqual(v['key'], 'value') def test_create_modify_and_get_edge(self): start = self.graph.vertex.create('test') end = self.graph.vertex.create('test') edge = start.link('edge', end, hello='world') edge = self.graph.edge.get(edge.uid) self.assertTrue(edge['hello'] == 'world') self.assertEqual(edge.start(), start) self.assertEqual(edge.end(), end) def test_create_edge_and_check_vertices_edges(self): start = self.graph.vertex.create('start') end = self.graph.vertex.create('end') start.link('edge', end) # retrieve start and end start = self.graph.vertex.get(start.uid) end = self.graph.vertex.get(end.uid) self.assertTrue(start.outgoings()) self.assertTrue(end.incomings()) def test_set_get_set_get(self): self.graph.set('key', 'value') self.assertEqual(self.graph.get('key'), 'value') self.graph.set('key', 'value deux') self.assertEqual(self.graph.get('key'), 'value deux') def test_set_get(self): self.graph.set('key', 'value') self.assertEqual(self.graph.get('key'), 'value') def test_set_get_dict(self): expected = dict(key='value') self.graph.set('key', expected) self.assertEqual(self.graph.get('key'), expected) def test_set_remove_get(self): self.graph.set('key', 'value') self.graph.remove('key') with self.assertRaises(KeyError): self.graph.get('key') def test_remove(self): with self.assertRaises(KeyError): self.graph.remove('key') def test_delete_vertex(self): start = self.graph.vertex.create('start') end = self.graph.vertex.create('end') start.link('edge', end) start.delete() self.assertEqual(len(end.incomings()), 0) with self.assertRaises(KeyError): self.graph.vertex.get(start.uid) def test_delete_edge(self): start = self.graph.vertex.create('start') end = self.graph.vertex.create('end') edge = start.link('edge', end) start.delete() self.assertEqual(len(start.outgoings()), 0) self.assertEqual(len(end.incomings()), 0) with self.assertRaises(KeyError): self.graph.edge.get(edge.uid) def test_update_vertex(self): start = self.graph.vertex.create('start', key='value') self.assertEqual(start['key'], 'value') start['key'] = 'monkey' start.save() self.assertEqual(start['key'], 'monkey') def test_update_edge(self): start = self.graph.vertex.create('start') end = self.graph.vertex.create('end') edge = start.link('link', end, key='value') self.assertEqual(edge['key'], 'value') edge['key'] = 'monkey' edge.save() self.assertEqual(edge['key'], 'monkey')
graph.index(start, start['concept']) # get or create end end = Vertex(ref=end, concept=wordify(end)) new, end = graph.get_or_create(end) if new: graph.index(end, end['concept']) edge = start.link(end, relation=relation) graph.save(edge) if args['conceptnet'] and args['search']: query = args['QUERY'] query = ' '.join(query) for uid, score in graph.like(query): msg = '(uid: %s)\t(score: %s)\t%s' concept = graph.get(uid)['concept'] print msg % (uid, score, concept) if args['wikidata'] and args['load']: with open(args['FILENAME']) as f: lines = iter(f) next(lines) # drop first line for line in lines: try: from json import loads entity = loads(line.strip()[:-1]) except: pass else: if entity['type'] != 'item': continue