class QueryTests(EndpointTestCase):

    def setUp(self):
        super(QueryTests, self).setUp()
        url = "http://%s:%s%s"
        url %= (self.config.host, self.config.endpoint_port, self.config.endpoint_path)

        self.endpoint = Endpoint(url)

    def test_register_namespace(self):
        "It can register a namespace to remember accross all queries"

        nfo_base_uri = 'http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#'
        NFO = Namespace(nfo_base_uri)
        self.endpoint.register_namespace("nfo", NFO)

        self.assertEquals(self.endpoint.get_registered_namespaces_header(),
                "PREFIX nfo: <%s>" % nfo_base_uri)


    def test_graph_query(self):
        sparql = "SELECT ?g WHERE { GRAPH ?g { ?s ?p ?o } } GROUP BY ?g"
        results = self.endpoint.query(sparql)
        self.assertTrue(isinstance(results, ResultSet))

        for result in results:
            if result['g'].value == "http://www.openlinksw.com/schemas/virtrdf#":
                return

        self.assertTrue(False, "Could not find virutoso schema graph")
    def test_init_auto(self):
        "It can be initialized using a string containing the endpoints host, port and path"

        url = "http://%s:%s%s"
        url %= (self.config.host, self.config.endpoint_port, self.config.endpoint_path)

        endpoint = Endpoint(url)

        self.assertEquals(endpoint.host, self.config.host)
        self.assertEquals(endpoint.port, self.config.endpoint_port)
        self.assertEquals(endpoint.path, self.config.endpoint_path)
        self.assertEquals(endpoint.get_full_path(), url)
    def setUp(self):
        self.config = Config("local_test_config.ini",
            default_manager=ISQLWrapperTestManager())

        url = "http://%s:%s%s"
        url %= (self.config.host, self.config.endpoint_port,
            self.config.endpoint_path)

        self.endpoint = Endpoint(url)
        self.isql = ISQLWrapper(self.config.host, self.config.user,
            self.config.password)
        self.isql.execute_cmd("SPARQL CLEAR GRAPH <%s>" % self.config.graph)
Exemple #4
0
    def test_init(self):
        "It is initialized using a given host, port and path"

        Endpoint(self.config.host, self.config.endpoint_port,
                 self.config.endpoint_path)
    def setUp(self):
        super(QueryTests, self).setUp()
        url = "http://%s:%s%s"
        url %= (self.config.host, self.config.endpoint_port, self.config.endpoint_path)

        self.endpoint = Endpoint(url)
class DataTestCase(unittest.TestCase):

    def setUp(self):
        self.config = Config("local_test_config.ini",
            default_manager=ISQLWrapperTestManager())

        url = "http://%s:%s%s"
        url %= (self.config.host, self.config.endpoint_port,
            self.config.endpoint_path)

        self.endpoint = Endpoint(url)
        self.isql = ISQLWrapper(self.config.host, self.config.user,
            self.config.password)
        self.isql.execute_cmd("SPARQL CLEAR GRAPH <%s>" % self.config.graph)

    def assertGraphHasNumberOfTriples(self, n):
        sparql_query = "SELECT * FROM <%s> WHERE {?s ?p ?o}" % self.config.graph
        results = self.endpoint.query(sparql_query)
        self.assertEquals(results.total_rows, n)

    def assertGraphIsEmpty(self):
        self.assertGraphHasNumberOfTriples(0)

    def tearDown(self):
        self.config.save("local_test_config.ini")
        self.isql.execute_cmd("SPARQL CLEAR GRAPH <%s>" % self.config.graph)

    def test_delete_uri(self):
        "Inserts a triple into the triplestore"

        ns = Namespace("https://github.com/juanique/artichoke/ns#")
        graph = self.config.graph

        self.assertGraphIsEmpty()

        self.isql.insert(graph,
            ns["subject"], ns["predicate"], ns["object"])

        self.isql.delete(graph,
            ns["subject"], ns["predicate"], ns["object"])

        self.assertGraphIsEmpty()

    def test_delete_literal(self):
        "Inserts a triple into the triplestore"

        ns = Namespace("https://github.com/juanique/artichoke/ns#")
        graph = self.config.graph

        self.assertGraphIsEmpty()

        self.isql.insert(graph,
            ns["subject"], ns["predicate"], Literal("Hello"))

        self.isql.delete(graph,
            ns["subject"], ns["predicate"], Literal("Hello"))

        self.assertGraphIsEmpty()

    def test_insert(self):
        "Inserts a triple into the triplestore"

        ns = Namespace("https://github.com/juanique/artichoke/ns#")
        graph = self.config.graph

        self.isql.insert(graph,
            ns["subject"], ns["predicate"], ns["object"])

        query = "SELECT * FROM <%s> WHERE {?s ?p ?o}" % graph
        results = self.endpoint.query(query)

        self.assertGraphHasNumberOfTriples(1)
        self.assertEquals(results[0]['s'].value, str(ns["subject"]))
        self.assertEquals(results[0]['p'].value, str(ns["predicate"]))
        self.assertEquals(results[0]['o'].value, str(ns["object"]))