def test_node_meta(self): x = rdfuri.Node("http://www.google.com/test", rdfuri.NodeType.XID) y = rdfuri.Node("http://www.google.com/test", rdfuri.NodeType.XID) b = rdfuri.Node("_:a1", rdfuri.NodeType.BLANK) if RUN_TIMEIT: print( "make node time: ", timeit.timeit( 'rdfuri.Node("http://www.google.com/test", rdfuri.NodeType.XID)', globals=globals())) print( "make new blanknode time: ", timeit.timeit('rdfuri.Node("_:a1", rdfuri.NodeType.BLANK)', globals=globals())) self.assertEqual(id(x), id(y))
class BatchConsumerTestCase(unittest.TestCase): sub = rdfuri.Node("http://test.com/123", rdfuri.NodeType.XID) sub2 = rdfuri.Node("http://test.com/456", rdfuri.NodeType.XID) pred = rdfuri.Node("http://test.com/friend", rdfuri.NodeType.PRED) pred2 = rdfuri.Node("http://test.com/friend2", rdfuri.NodeType.PRED) obj = rdfuri.Literal("literal data", "xs:string") def test_init(self): data = dataset.Dataset() batch_consumer = consumer.BatchConsumer(data) # test default destination function self.assertEqual(batch_consumer.destination, dgraphapi.mutate_add_dataset) def test_send(self): dgraphapi.drop_all() data = dataset.Dataset() batch_consumer = consumer.BatchConsumer(data) data.triple(self.sub, self.pred, self.obj) data.triple(self.sub, self.pred, rdfuri.Literal("literal data2", "xs:string")) result = batch_consumer.send() self.assertFalse(data)
class BatchProcessorTestCase(unittest.TestCase): sub = rdfuri.Node("http://test.com/123", rdfuri.NodeType.XID) sub2 = rdfuri.Node("http://test.com/456", rdfuri.NodeType.XID) pred = rdfuri.Node("http://test.com/friend", rdfuri.NodeType.PRED) pred2 = rdfuri.Node("http://test.com/friend2", rdfuri.NodeType.PRED) obj = rdfuri.Literal("literal data", "xs:string") def test_init(self): data = dataset.Dataset() processor = batcher.BatchProcessor(data) # test default destination function self.assertEqual(processor.batch_consumer.destination, dgraphapi.mutate_add_dataset) def test_send(self): dgraphapi.drop_all() data = dataset.Dataset() batch_consumer = consumer.BatchConsumer(data) data.triple(self.sub, self.pred, self.obj) data.triple(self.sub, self.pred, rdfuri.Literal("literal data2", "xs:string")) result = batch_consumer.send() self.assertFalse(data) def test_batch(self): dgraphapi.drop_all() data = dataset.Dataset() processor = batcher.BatchProcessor(data, batch_size=10) batch_count = 0 for i in range(1, 100): data.triple(self.sub, self.pred, rdfuri.Literal("literal data-%s" % i, "xs:string")) processor.increment() if processor.count == 0: batch_count += 1 self.assertTrue(batch_count > 4)
def nodeid(self): if self.peek('_'): bnode_id = self.eat(r_nodeid).group(1) return rdfuri.Node(bnode_id, rdfuri.NodeType.BLANK) return False
def uriref(self): if self.peek('<'): uri = self.eat(r_uriref).group(1) uri = rdfuri.clear_signs(unquote(uri)) return rdfuri.Node(uri, rdfuri.NodeType.XID) return False
class TestDatasetLoading(unittest.TestCase): sub = rdfuri.Node("http://test.com/123", rdfuri.NodeType.XID) sub2 = rdfuri.Node("http://test.com/456", rdfuri.NodeType.XID) pred = rdfuri.Node("http://test.com/friend", rdfuri.NodeType.PRED) pred2 = rdfuri.Node("http://test.com/friend2", rdfuri.NodeType.PRED) obj = rdfuri.Literal("literal data", "xs:string") def test_triple(self): data = dataset.Dataset() data.triple(self.sub, self.pred, self.obj) self.assertTrue(self.obj in data[self.sub][self.pred]) self.assertTrue(self.sub in data.keys()) self.assertTrue(self.pred in data[self.sub].keys()) data.triple(self.sub, self.pred, self.obj) self.assertTrue(len(data[self.sub][self.pred]) == 2) pprint.pprint(data.triples()) def test_are_nodes_set(self): data = dataset.Dataset() data.triple(self.sub, self.pred, self.obj) self.assertFalse(data.are_nodes_set()) def test_lookup_nodes(self): rdfuri.NodeRegistryMeta.clear_registry(rdfuri.NodeType.XID) data = dataset.Dataset() data.triple(self.sub2, self.pred, self.obj) data.triple(self.sub2, self.pred, self.obj) data.triple(self.sub2, self.pred, self.obj) self.assertFalse(data.are_nodes_set()) data.lookup_nodes() self.assertTrue(data.are_nodes_set()) def test_formatter(self): data = dataset.Dataset() data.triple(self.sub, self.pred, self.obj) data.triple(self.sub, self.pred, rdfuri.Literal("literal data2", "xs:string")) data.triple(self.sub, self.pred, rdfuri.Literal("literal data3", "xs:string")) data.lookup_nodes() print(data.formatter.to_rdf()) def test_mutate_dataset(self): data = dataset.Dataset() data.triple(self.sub, self.pred, self.obj) data.triple(self.sub, self.pred, rdfuri.Literal("literal data2", "xs:string")) data.triple(self.sub, self.pred, rdfuri.Literal("literal data3", "xs:string")) dgraphapi.mutate_add_dataset(data) def test_clear(self): data = dataset.Dataset() data.triple(self.sub, self.pred, self.obj) data.triple(self.sub, self.pred, rdfuri.Literal("literal data2", "xs:string")) data.triple(self.sub, self.pred, rdfuri.Literal("literal data3", "xs:string")) data.triple(self.sub, self.pred2, self.obj) data.triple(self.sub2, self.pred, self.obj) data.triple(self.sub2, self.pred, rdfuri.Literal("literal data2", "xs:string")) data.triple(self.sub2, self.pred, rdfuri.Literal("literal data3", "xs:string")) data.triple(self.sub2, self.pred2, self.obj) data.clear()
def test_registry(self): for i in range(10): node = rdfuri.Node() reg = rdfuri.NodeRegistryMeta self.assertTrue(len(reg._registry[node.node_type]) == 10) reg.clear_registry(node.node_type)
def test_clear_registry(self): node = rdfuri.Node() reg = rdfuri.NodeRegistryMeta self.assertTrue(reg._registry[node.node_type]) reg.clear_registry(node.node_type) self.assertFalse(reg._registry[node.node_type])