How to use the spectacles.validators.sql.Query function in spectacles

To help you get started, we’ve selected a few spectacles examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github spectacles-ci / spectacles / tests / test_sql_validator.py View on Github external
def test_get_running_query_tasks(validator):
    queries = [
        Query(
            query_id="12345",
            lookml_ref=None,
            query_task_id="abc",
            explore_url="https://example.looker.com/x/12345",
        ),
        Query(
            query_id="67890",
            lookml_ref=None,
            query_task_id="def",
            explore_url="https://example.looker.com/x/67890",
        ),
    ]
    validator._running_queries = queries
    assert validator.get_running_query_tasks() == ["abc", "def"]
github spectacles-ci / spectacles / tests / test_sql_validator.py View on Github external
def test_create_and_run_keyboard_interrupt_cancels_queries(validator):
    validator._running_queries = [
        Query(
            query_id="12345",
            lookml_ref=None,
            query_task_id="abc",
            explore_url="https://example.looker.com/x/12345",
        )
    ]
    mock_create_queries = create_autospec(validator._create_queries)
    mock_create_queries.side_effect = KeyboardInterrupt()
    validator._create_queries = mock_create_queries
    mock_cancel_queries = create_autospec(validator._cancel_queries)
    validator._cancel_queries = mock_cancel_queries
    try:
        validator._create_and_run(mode="batch")
    except SpectaclesException:
        mock_cancel_queries.assert_called_once_with(query_task_ids=["abc"])
github spectacles-ci / spectacles / tests / test_sql_validator.py View on Github external
def test_handle_running_query(validator, dimension):
    query_task_id = "sakgwj392jfkajgjcks"
    query = Query(
        query_id="19428",
        lookml_ref=dimension,
        query_task_id=query_task_id,
        explore_url="https://spectacles.looker.com/x/qCJsodAZ2Y22QZLbmD0Gvy",
    )
    query_result = QueryResult(query_task_id=query_task_id, status="running")
    validator._running_queries = [query]
    validator._query_by_task_id[query_task_id] = query
    returned_sql_error = validator._handle_query_result(query_result)

    assert validator._running_queries == [query]
    assert not returned_sql_error
github spectacles-ci / spectacles / spectacles / validators / sql.py View on Github external
def _create_explore_query(self, explore: Explore, model_name: str) -> Query:
        """Creates a single query with all dimensions of an explore"""
        dimensions = [dimension.name for dimension in explore.dimensions]
        query = self.client.create_query(model_name, explore.name, dimensions)
        return Query(query["id"], lookml_ref=explore, explore_url=query["share_url"])
github spectacles-ci / spectacles / spectacles / validators / sql.py View on Github external
def _create_dimension_queries(
        self, explore: Explore, model_name: str
    ) -> List[Query]:
        """Creates individual queries for each dimension in an explore"""
        queries = []
        for dimension in explore.dimensions:
            query = self.client.create_query(model_name, explore.name, [dimension.name])
            query = Query(
                query["id"], lookml_ref=dimension, explore_url=query["share_url"]
            )
            queries.append(query)
        return queries
github spectacles-ci / spectacles / spectacles / validators / sql.py View on Github external
def _create_and_run(self, mode: QueryMode = "batch") -> None:
        """Runs a single validation using a specified mode"""
        queries: List[Query] = []
        try:
            queries = self._create_queries(mode)
            self._run_queries(queries)
        except KeyboardInterrupt:
            logger.info(
                "\n\n" + "Please wait, asking Looker to cancel any running queries..."
            )
            query_tasks = self.get_running_query_tasks()
            self._cancel_queries(query_tasks)
            if query_tasks:
                message = (
                    f"Attempted to cancel {len(query_tasks)} running "
                    f"{'query' if len(query_tasks) == 1 else 'queries'}."
                )
            else:
                message = (