コード例 #1
0
    def test_query_all_properties_and_objects_with_expand_object_properties(
            self):
        triplestore.query_sparql = lambda query, query_params: query

        class Params(dict):
            triplestore_config = {}

        params = Params({})
        params.update({
            "instance_uri": "instance_uri",
            "class_uri": "class_uri",
            "lang": "en",
            "expand_object_properties": "1"
        })

        computed = get_instance.query_all_properties_and_objects(params)
        # Inference was turned-off on 2014-06-04 due to Virtuoso bug. See releases.rst
        # DEFINE input:inference <http://semantica.globo.com/ruleset>

        expected = """
            SELECT DISTINCT ?predicate ?object ?object_label ?super_property isBlank(?object) as ?is_object_blank {
                <instance_uri> a <class_uri> ;
                    ?predicate ?object .
            OPTIONAL { ?predicate rdfs:subPropertyOf ?super_property } .
            OPTIONAL { ?object rdfs:label ?object_label } .
            FILTER((langMatches(lang(?object), "en") OR langMatches(lang(?object), "")) OR (IsURI(?object)) AND !isBlank(?object)) .
            }
            """
        self.assertEqual(strip(computed), strip(expected))
コード例 #2
0
    def test_query_all_properties_and_objects_without_expand_object_properties(self):
        triplestore.query_sparql = lambda query, query_params: query

        class Params(dict):
            triplestore_config = {}

        params = Params({})
        params.update({
            "instance_uri": "instance_uri",
            "class_uri": "class_uri",
            "lang": "en",
            "expand_object_properties": "0"
        })

        computed = get_instance.query_all_properties_and_objects(params)
        # Inference was turned-off on 2014-06-04 due to Virtuoso bug. See releases.rst
        # DEFINE input:inference <http://semantica.globo.com/ruleset>

        expected = """
            SELECT DISTINCT ?predicate ?object  ?super_property isBlank(?object) as ?is_object_blank {
                <instance_uri> a <class_uri> ;
                    ?predicate ?object .
            OPTIONAL { ?predicate rdfs:subPropertyOf ?super_property } .

            FILTER((langMatches(lang(?object), "en") OR langMatches(lang(?object), "")) OR (IsURI(?object)) AND !isBlank(?object)) .
            }
            """
        self.assertEqual(strip(computed), strip(expected))
コード例 #3
0
 def test_count_query_without_extras(self):
     params = self.default_params
     query = Query(params)
     computed = query.to_string(count=True)
     expected = """
     SELECT count(DISTINCT ?subject) as ?total
     WHERE {
         GRAPH <http://some.graph/> { ?subject a <http://some.graph/SomeClass> OPTION(inference "http://semantica.globo.com/ruleset");
                  <http://www.w3.org/2000/01/rdf-schema#label> ?label .
                  }
     }
     """
     self.assertEqual(strip(computed), strip(expected))
コード例 #4
0
 def test_query_with_predicate_as_rdfs_label(self):
     params = self.default_params.copy()
     params["p"] = "rdfs:label"
     query = Query(params)
     computed = query.to_string()
     expected = """
     SELECT DISTINCT ?label, ?subject
     WHERE {
         GRAPH <http://some.graph/> { ?subject a <http://some.graph/SomeClass> OPTION(inference "http://semantica.globo.com/ruleset");
                  <http://www.w3.org/2000/01/rdf-schema#label> ?label .
                  }
     }
     LIMIT 10
     OFFSET 0
     """
     self.assertEqual(strip(computed), strip(expected))
コード例 #5
0
 def test_query_with_sort_by(self):
     params = self.default_params.copy()
     params["sort_by"] = "dbpedia:predicate"
     params["sort_order"] = "asc"
     query = Query(params)
     computed = query.to_string()
     expected = """
     SELECT DISTINCT ?label, ?sort_object, ?subject
     WHERE {
         GRAPH <http://some.graph/> { ?subject a <http://some.graph/SomeClass> OPTION(inference "http://semantica.globo.com/ruleset");
                  <http://www.w3.org/2000/01/rdf-schema#label> ?label .
         OPTIONAL {?subject <http://dbpedia.org/ontology/predicate> ?sort_object} }
     }
     ORDER BY ASC(?sort_object)
     LIMIT 10
     OFFSET 0
     """
     self.assertEqual(strip(computed), strip(expected))
コード例 #6
0
 def test_query_with_predicate_and_object_as_literal(self):
     params = self.default_params.copy()
     params["p"] = "schema:Creature"
     params["o"] = "Xubiru"
     query = Query(params)
     computed = query.to_string()
     expected = """
     SELECT DISTINCT ?label, ?subject
     WHERE {
         GRAPH <http://some.graph/> { ?subject a <http://some.graph/SomeClass> OPTION(inference "http://semantica.globo.com/ruleset");
                  <http://www.w3.org/2000/01/rdf-schema#label> ?label ;
                  <http://schema.org/Creature> ?literal1 .
                  }
         FILTER(str(?literal1) = "Xubiru") .
     }
     LIMIT 10
     OFFSET 0
     """
     self.assertEqual(strip(computed), strip(expected))
コード例 #7
0
 def test_query_with_sort_by_object_of_predicate(self):
     params = self.default_params.copy()
     params["p"] = "schema:another_predicate"
     params["sort_by"] = "schema:another_predicate"
     params["sort_order"] = "desc"
     query = Query(params)
     computed = query.to_string()
     expected = """
     SELECT DISTINCT ?label, ?object, ?subject
     WHERE {
         GRAPH <http://some.graph/> { ?subject a <http://some.graph/SomeClass> OPTION(inference "http://semantica.globo.com/ruleset");
                  <http://www.w3.org/2000/01/rdf-schema#label> ?label ;
                  <http://schema.org/another_predicate> ?object .
                  }
     }
     ORDER BY DESC(?object)
     LIMIT 10
     OFFSET 0
     """
     self.assertEqual(strip(computed), strip(expected))
コード例 #8
0
    def test_query_with_p1_o1_p2_o2(self):
        params = self.default_params.copy()
        params["p1"] = "some:predicate"
        params["o1"] = "some:object"
        params["p2"] = "another:predicate"
        params["o2"] = "?another_object"
        query = Query(params)
        computed = query.to_string()
        expected = """
        SELECT DISTINCT ?another_object, ?label, ?subject
        WHERE {
            GRAPH <http://some.graph/> { ?subject a <http://some.graph/SomeClass> OPTION(inference "http://semantica.globo.com/ruleset");
                     <http://www.w3.org/2000/01/rdf-schema#label> ?label ;
                     another:predicate ?another_object ;
                     some:predicate some:object .
                     }
        }

        LIMIT 10
        OFFSET 0
        """
        self.assertEqual(strip(computed), strip(expected))