diff --git a/tests/bigquery/test_parser.py b/tests/bigquery/test_job_change_event.py similarity index 76% rename from tests/bigquery/test_parser.py rename to tests/bigquery/test_job_change_event.py index aae7dffc..59afaf43 100644 --- a/tests/bigquery/test_parser.py +++ b/tests/bigquery/test_job_change_event.py @@ -1,6 +1,8 @@ from datetime import datetime +from unittest.mock import MagicMock, patch import pytz +from google.cloud import bigquery from metaphor.bigquery.job_change_event import JobChangeEvent from metaphor.bigquery.utils import BigQueryResource @@ -69,3 +71,32 @@ def test_parse_log(test_root_dir): output_rows=1, ), ] + + +@patch("google.cloud.bigquery.Client") +def test_fetch_job_query(mock_client: MagicMock): + query = "SELECT * FROM my-project.dataset.table" + mock_client.get_job = lambda job_id, project: bigquery.QueryJob( + job_id=job_id, + query=query, + client=mock_client, + ) + assert ( + JobChangeEvent._fetch_job_query(mock_client, "projects/my-project/jobs/1234") + == query + ) + + +@patch("google.cloud.bigquery.Client") +def test_fetch_job_query_fail(mock_client: MagicMock): + def fail_get_job(job_id, project): + raise ValueError + + mock_client.get_job = fail_get_job + assert not JobChangeEvent._fetch_job_query( + mock_client, "projects/my-project/jobs/1234" + ) + + +def test_fetch_not_a_job(): + assert not JobChangeEvent._fetch_job_query(MagicMock(), "projects/my-project/jobs")