Пример #1
0
    def test_simple(self):
        """ Try to produce a simple "SELECT ... WHERE ..." query.  """

        expected = canonical(u"SELECT ?s ?p ?o WHERE { ?s ?p ?o }")
        query = select("?s", "?p", "?o").where(("?s", "?p", "?o"))
        result = SparqlTranslator(query).translate()

        # Translated query should be unicode object.
        self.assertTrue(isinstance(result, unicode))

        result = canonical(result)
        self.assertEqual(expected, result)
Пример #2
0
    def test_ask(self):
        """ Try ASK. """

        expected = canonical(u"""
            ASK FROM <http://uri1>
            {
                ?s ?p ?o
            }
        """)

        query = ask().from_(URIRef("http://uri1")).where(("?s", "?p", "?o"))

        result = canonical(SparqlTranslator(query).translate())
        self.assertEqual(expected, result)
Пример #3
0
    def test_union(self):
        """ Try to produce query containing union. """

        expected = canonical(u"""
            SELECT ?s
            WHERE {
                { ?s ?v1 ?v2} UNION { ?s ?v3  ?v4 }
            }
        """)

        query = select("?s").union(("?s", "?v1", "?v2"), ("?s", "?v3", "?v4"))
        result = canonical(SparqlTranslator(query).translate())

        self.assertEqual(expected, result)
Пример #4
0
def test_simple():
    """
    Try to produce a simple "SELECT ... WHERE ..." query.
    """

    expected = canonical(u"SELECT ?s ?p ?o WHERE { ?s ?p ?o }")
    query = select("?s", "?p", "?o").where(("?s", "?p", "?o"))
    result = SparqlTranslator(query).translate()

    # Translated query should be unicode object.
    assert isinstance(result, basestring)

    result = canonical(result)
    assert expected == result
Пример #5
0
    def test_describe(self):
        """ Try to produce DESCRIBE query. """

        expected = canonical(u"""
            DESCRIBE ?s
            FROM <http://uri1>
            WHERE { 
                ?s ?p ?o 
            } LIMIT 10
        """)

        query = describe("?s").where(("?s", "?p", "?o"))
        query.from_("http://uri1").limit(10)
        result = canonical(SparqlTranslator(query).translate())

        self.assertEqual(expected, result)
Пример #6
0
    def test_from(self):
        """ Try to produce query that contains FROM clauses. """

        expected = canonical(u"""
            SELECT ?s ?p ?o
            FROM <http://uri1>
            FROM <http://uri2> 
            WHERE { 
                ?s ?p ?o 
            }
        """)

        query = select("?s", "?p", "?o").where(("?s", "?p", "?o"))
        query.from_("http://uri1", URIRef("http://uri2"))
        result = canonical(SparqlTranslator(query).translate())

        self.assertEqual(expected, result)
Пример #7
0
    def test_subquery(self):
        """ Try to produce query that contains subquery in WHERE clause. """

        expected = canonical(u"""
            SELECT ?s ?p ?o 
            WHERE { 
                ?s ?p ?o. 
                { SELECT ?s WHERE { ?s ?a ?b } LIMIT 3 }
            }
        """)

        subquery = select("?s").where(("?s", "?a", "?b")).limit(3)

        query = select("?s", "?p", "?o").where(("?s", "?p", "?o"), subquery)
        result = canonical(SparqlTranslator(query).translate())

        self.assertEqual(expected, result)
Пример #8
0
def test_from_named():
    """
    Try to produce query that contains FROM & FROM NAMED clauses.
    """

    expected = canonical(u"""
        SELECT ?s ?p ?o
        FROM <http://uri1>
        FROM NAMED <http://uri1>
        FROM NAMED <http://uri2>
        WHERE {
            ?s ?p ?o
        }
    """)

    query = select("?s", "?p", "?o").where(("?s", "?p", "?o"))
    query.from_("http://uri1")
    query.from_named("http://uri1", URIRef("http://uri2"))
    result = canonical(SparqlTranslator(query).translate())

    assert expected == result
Пример #9
0
 def _unicode(self):
     # Importing here to avoid circular imports.
     from surf.query.translator.sparql import SparqlTranslator
     return SparqlTranslator(self).translate()