def test_multi_line_query(self):
        query = "select ?s ?p ?o\nwhere {?s ?p ?o}"
        graph_uris = ["http://graph/1"]

        expected_finalized_query = "select ?s ?p ?o\nFROM <http://graph/1> where {?s ?p ?o}"
        finalized_query = finalize_query(query=query, graph_uris=graph_uris)

        self.assertEqual(finalized_query, expected_finalized_query)
    def test_multi_line_query_with_trailing_comments(self):
        query = "select ?s ?p ?o # this is the select\nwhere {?s ?p ?o} # this is the where clause"
        graph_uris = ["http://graph/1"]

        expected_finalized_query = "select ?s ?p ?o # this is the select\nFROM <http://graph/1> where {?s ?p ?o} # this is the where clause"
        finalized_query = finalize_query(query=query, graph_uris=graph_uris)

        self.assertEqual(finalized_query, expected_finalized_query)
    def test_one_line_query_with_trailing(self):
        query = "select ?s ?p ?o where {?s ?p ?o} # ignore me please select where {"
        graph_uris = ["http://graph/1"]

        expected_finalized_query = "select ?s ?p ?o FROM <http://graph/1> where {?s ?p ?o} # ignore me please select where {"
        finalized_query = finalize_query(query=query, graph_uris=graph_uris)

        self.assertEqual(finalized_query, expected_finalized_query)
    def test_one_line_query_full_rdf_type_syntax(self):
        query = "select ?s ?p ?o where { ?s ?p ?o; <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <my:type> }"
        graph_uris = ["http://graph/1"]

        expected_finalized_query = "select ?s ?p ?o FROM <http://graph/1> where { ?s ?p ?o; <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <my:type> }"
        finalized_query = finalize_query(query=query, graph_uris=graph_uris)

        self.assertEqual(finalized_query, expected_finalized_query)
    def test_one_line_query_with_trailing_comment(self):
        query = "select ?s ?p ?o where {?s ?p ?o} # hi"
        graph_uris = ["http://graph/1"]

        expected_finalized_query = "select ?s ?p ?o FROM <http://graph/1> where {?s ?p ?o} # hi"
        finalized_query = finalize_query(query=query, graph_uris=graph_uris)

        self.assertEqual(finalized_query, expected_finalized_query)
    def test_multi_line_query_with_comments(self):
        query = "# select ?s ?p ?o where { ?s ?p ?o }\n# that was an old version\nselect ?s ?p ?o  # where { not this where\n# or this where {\nwhere  # this where\n{ ?s  # sub\n?p  # pred\n?o\nobj\n}  # bye\n# test\n# select ?s ?p ?o where { ?s ?p ?o } # limit 100\n limit 1000 #real limit"
        graph_uris = ["http://graph/1", "http://graph/2"]

        expected_finalized_query = "# select ?s ?p ?o where { ?s ?p ?o }\n# that was an old version\nselect ?s ?p ?o  # where { not this where\n# or this where {\nFROM <http://graph/1> FROM <http://graph/2> where  # this where\n{ ?s  # sub\n?p  # pred\n?o\nobj\n}  # bye\n# test\n# select ?s ?p ?o where { ?s ?p ?o } # limit 100\n limit 1000 #real limit"
        finalized_query = finalize_query(query=query, graph_uris=graph_uris)

        self.assertEqual(finalized_query, expected_finalized_query)
    def test_one_line_query_with_limit(self):
        query = "select ?s ?p ?o where {?s ?p ?o} limit 100"
        graph_uris = ["http://graph/1"]

        expected_finalized_query = (
            "select ?s ?p ?o FROM <http://graph/1> where {?s ?p ?o} limit 100"
        )
        finalized_query = finalize_query(query=query, graph_uris=graph_uris)

        self.assertEqual(finalized_query, expected_finalized_query)
    def run_query(self, graph_names: Set[str], query: str) -> QueryResult:
        """Runs a SPARQL query against the latest available graphs given a list of
        graph names.

        Args:
            graph_names: list of graph names to query
            query: query string. This query string should not include any 'from' clause;
                   the graph_names param will be used to inject the correct graph uris
                   by locating the latest acceptable (based on `max_age_min`) graph.

        Returns:
            QueryResult object
        """
        graph_uris_load_times: Dict[str, int] = {}
        for graph_name in graph_names:
            graph_metadata = self._get_latest_graph_metadata(name=graph_name)
            graph_uris_load_times[graph_metadata.uri] = graph_metadata.end_time
        finalized_query = finalize_query(query, graph_uris=list(graph_uris_load_times.keys()))
        query_result_set = self.run_raw_query(finalized_query)
        return QueryResult(graph_uris_load_times, query_result_set)
    def test_multi_line_query_missing_where(self):
        query = "select ?s ?p ?o\n{?s ?p ?o}"
        graph_uris = ["http://graph/1"]

        with self.assertRaises(InvalidQueryException):
            finalize_query(query=query, graph_uris=graph_uris)
    def test_one_line_query_missing_where_due_to_leading_comment(self):
        query = "#select ?s ?p ?o where {?s ?p ?o}"
        graph_uris = ["http://graph/1"]

        with self.assertRaises(InvalidQueryException):
            finalize_query(query=query, graph_uris=graph_uris)
    def test_empty_query(self):
        query = ""
        graph_uris = ["http://graph/1"]

        with self.assertRaises(InvalidQueryException):
            finalize_query(query=query, graph_uris=graph_uris)