# coding: utf-8

from common.sparql.QueryUtils import QueryUtils
from common.tarql import Tarql


tarql = Tarql(QueryUtils())

basePath = "/home/nikola/projects/semanticElections/data/"

## SECTIONS

queryArgs = {
    "EL": "ep2019" ,
    "TYP" : "ep",
    "TYP_LABEL": "Европейски Парламент 2019",
    "EL_LABEL" : "Избори за Европейски Парламент 2019",
}
infile = '/home/nikola/projects/semanticElections/gdrive/data/cikOpenData/ep2019/sections.txt'
tarql.execute_query(basePath+"tarql/sections_ep.tarql",
                    infile,
                    basePath+"rdf/ep2019/sections.ttl",
                    '-d ; -H',
                    queryArgs)

## PROTOCOLS
queryArgs = {
    "EL": "ep2019" ,
    "TYP" : "" ,
    "RND" : "" ,
    "LINK_HTML" : "https://results.cik.bg/ep2019/protokoli/",
def tarqls_compare(query_path, csv_path, expected_path, tarql_arguments):
    temp = tempfile.NamedTemporaryFile()
    Tarql(QueryUtils()).execute_query(query_path, csv_path, temp.name,
                                      tarql_arguments)
    return (sorted(list(Graph().parse(temp.name, format="ttl"))),
            sorted(list(Graph().parse(expected_path, format="ttl"))))
Example #3
0
        query_with_prefix = self.add_prefixes_to_query(query, graph, graph_prefix)
        query_with_versions = query_with_prefix\
            .replace("${{pipeline_version}}", os.environ["VERSION"] if "VERSION" in os.environ else "") \
            .replace("${{dag_version}}", os.environ["DAG_VERSION"] if "DAG_VERSION" in os.environ else "") \
            .replace("${{triples}}", str(triples))
        self.handle_request(query_with_versions, endpoint, "")

    @staticmethod
    def on_status_code(code):
        statuses = {
            200: True,
            204: True
        }
        return statuses.get(code, False)

    def execute_query(self, query, endpoint):
        if 'statements' in endpoint:
            response = requests.post(endpoint, data={'update': query})
        else:
            response = requests.get(endpoint, params={'query': query}, headers={'Accept': 'application/json'})
        return self.on_status_code(response.status_code), response.text


if __name__ == '__main__':
    executor = Executor(QueryUtils())
    # print executor.get_graph_size("http://trr.ontotext.com/resource/graph/tarql/cordisref-FP7programmes.ttl",
    #                                 "http://pascal:8200/repositories/trr-load-test/statements", "")

    executor.execute_folder("/Users/plamentarkalanov/PyCharmProjects/TRR/TRR/sparql-update/matching/authors", "ru", "http://pascal:8200/repositories/trr-load-test/statements")
    # Executor(QueryUtils()).add_graph_metadata("tarql/jrc-names.ttl", "http://pascal:8200/repositories/trr-load-test/statements")
class ExecutorTest(unittest.TestCase, TestBase):
    qutils = QueryUtils()
    qutils.set_prefixes(
        resource_filename("common.test.resources.sparql",
                          "prefixes-input.ttl"))
    executor = Executor(qutils)
    executor.query_utils.set_prefixes(
        resource_filename("common.test.resources.sparql",
                          "prefixes-input.ttl"))

    def test_read_prefixes(self):
        query_raw = self.get_file_content(
            resource_filename("common.test.resources.sparql.execute",
                              "query1.rq"))
        actual = self.executor.add_prefixes_to_query(query_raw, "")
        expected = self.get_file_content(
            resource_filename("common.test.resources.sparql.execute",
                              "query1-with-prefixes"))
        self.assertEqual(actual, expected)

    def test_clean_graph_with_prefixes(self):
        actual = self.executor.add_prefixes_to_query(
            self.executor.get_clear_graph_template(), "")
        expected = self.get_file_content(
            resource_filename("common.test.resources.sparql.execute",
                              "query1-clear"))
        self.assertEqual(actual, expected)

    def test_list_files(self):
        dir_name = resource_filename("common.test.resources.sparql", "execute")
        files = list(self.executor.list_files(dir_name, "rq"))
        self.assertEqual(["query1.rq", "query2.rq", "query3.rq"], files)

    def test_one_query_execution(self):
        query = resource_string("common.test.resources.sparql-update.query",
                                "500-trr-organization-prefname.ru")
        input_ttl = resource_filename(
            "common.test.resources.sparql-update.input",
            "500-trr-organization-prefname.ru.ttl")
        output_ttl = resource_filename(
            "common.test.resources.sparql-update.output",
            "500-trr-organization-prefname.ru.ttl")
        result = QueryTest(input_ttl, output_ttl, "ttl",
                           query).execute_update_query()
        self.assertEqual(result[0], result[1])

    def test_folder_query_execution(self):
        query = resource_filename("common.test.resources.sparql-update",
                                  "query")
        input_ttl = resource_filename("common.test.resources.sparql-update",
                                      "input")
        output_ttl = resource_filename("common.test.resources.sparql-update",
                                       "output")
        for x, y in QueryFolderTest().execute(query, input_ttl, output_ttl,
                                              self.executor):
            self.assertEqual(x, y)

    # Integration tests
    def it_execute_query_positive(self):
        query = "select * { ?s ?p ?o } limit 10"
        self.assertTrue(
            self.executor.execute_query(
                query,
                self.get_config().get("sparql", "endpoint_trr"))[0])

    def it_execute_query_negative(self):
        query = "select * { ?s ?p ?o } li"
        self.assertFalse(
            self.executor.execute_query(
                query,
                self.get_config().get("sparql", "endpoint_trr"))[0])