Пример #1
0
 def test_literal_assignments(self):
     self.assertEqual(
         rdfuri.Literal("""test "a" of 'b' in a string""",
                        "xs:string").dgraph,
         '''"test \\"a\\" of 'b' in a string"^^<xs:string>''')
     self.assertEqual(
         rdfuri.Literal(1, "xs:int").dgraph, '''"1"^^<xs:int>''')
Пример #2
0
 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)
Пример #3
0
 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())
Пример #4
0
 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()
Пример #5
0
 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)
Пример #6
0
 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)
Пример #7
0
 def literal(self):
     if self.peek('"'):
         lit, lang, dtype = self.eat(r_literal).groups()
         if lang:
             lang = lang
         else:
             lang = None
         if dtype:
             dtype = dtype
         else:
             dtype = None
         if lang and dtype:
             raise ParseError("Can't have both a language and a datatype")
         lit = unquote(lit)
         return rdfuri.Literal(lit, dtype, lang)
     return False
Пример #8
0
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)
Пример #9
0
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)
Пример #10
0
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()