Changeset - f7ae0faa0a44
[Not reviewed]
default
0 1 1
drewp@bigasterisk.com - 12 years ago 2013-01-15 21:01:03
drewp@bigasterisk.com
makefile and nosetests path fix. new contextsForStatement
Ignore-this: 424b0fa004ff5366b8035af42d9d9d0d
2 files changed with 25 insertions and 1 deletions:
0 comments (0 inline, 0 general)
light9/rdfdb/rdflibpatch.py
Show inline comments
 
"""
 
this is a proposal for a ConjunctiveGraph method in rdflib
 
"""
 
import sys
 
if sys.path[0] == '/usr/lib/python2.7/dist-packages':
 
    # nosetests puts this in
 
    sys.path = sys.path[1:]
 

	
 
import unittest
 
from rdflib import ConjunctiveGraph, URIRef as U
 

	
 
def patchQuads(graph, deleteQuads, addQuads, perfect=False):
 
    """
 
    Delete the sequence of given quads. Then add the given quads just
 
    like addN would. If perfect is True, we'll error before the
 
    deletes or before the adds (not a real transaction) if any of the
 
    deletes isn't in the graph or if any of the adds was already in
 
    the graph.
 
    """
 
    toDelete = []
 
@@ -64,25 +69,42 @@ def serializeQuad(g):
 
        out += u"%s %s %s %s .\n" % (s.n3(),
 
                                p.n3(),
 
                                _xmlcharref_encode(o.n3()), 
 
                                c.n3())
 
    return out
 

	
 
def inContext(graph, newContext):
 
    """
 
    make a ConjunctiveGraph where all the triples in the given graph
 
    are in newContext
 
    """
 
    return graphFromQuads([(s,p,o,newContext) for s,p,o in graph])
 
    
 

	
 
def contextsForStatement(graph, triple):
 
    return [q[3] for q in graph.quads(triple)]
 

	
 

	
 
A = U("http://a"); B = U("http://b")
 
class TestContextsForStatement(unittest.TestCase):
 
    def testNotFound(self):
 
        g = graphFromQuads([(A,A,A,A)])
 
        self.assertEqual(contextsForStatement(g, (B,B,B)), [])
 
    def testOneContext(self):
 
        g = graphFromQuads([(A,A,A,A), (A,A,B,B)])
 
        self.assertEqual(contextsForStatement(g, (A,A,A)), [A])
 
    def testTwoContexts(self):
 
        g = graphFromQuads([(A,A,A,A), (A,A,A,B)])
 
        self.assertEqual(sorted(contextsForStatement(g, (A,A,A))), sorted([A,B]))
 

	
 

	
 

	
 
class TestGraphFromQuads(unittest.TestCase):
 
    nqOut = '<http://example.com/> <http://example.com/> <http://example.com/> <http://example.com/> .\n'
 
    def testSerializes(self):
 
        n = U("http://example.com/")
 
        g = graphFromQuads([(n,n,n,n)])
 
        out = serializeQuad(g)
 
        self.assertEqual(out.strip(), self.nqOut.strip())
 

	
 
    def testNquadParserSerializes(self):
 
        g = graphFromNQuad(self.nqOut)
 
        self.assertEqual(len(g), 1)
makefile
Show inline comments
 
new file 100644
 
tests:
 
	bin/python `which nosetests` --no-path-adjustment light9.rdfdb.rdflibpatch
0 comments (0 inline, 0 general)