Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve ta_storage.bigquery #1017

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion database/models/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Upload(CodecovBaseModel, MixinBaseClass):
upload_type_id = Column(types.Integer)

@cached_property
def flag_names(self):
def flag_names(self) -> list[str]:
return [f.flag_name for f in self.flags]


Expand Down
27 changes: 15 additions & 12 deletions generated_proto/testrun/ta_testrun_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions protobuf/ta_testrun.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ message TestRun {
PASSED = 0;
FAILED = 1;
SKIPPED = 2;
FLAKY_FAILED = 3;
}

optional Outcome outcome = 6;
Expand All @@ -27,4 +28,8 @@ message TestRun {

optional string filename = 14;
optional string framework = 15;

optional int64 upload_id = 16;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a specific reason why all the fields ar optional, is this for forward compatiblity reasons?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protobuf generally prefers fields to be optional, if a field is marked as required then it's required forever: context

optional bytes flags_hash = 17;
optional bytes test_id = 18;
}
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ grpcio>=1.66.2
httpx
jinja2>=3.1.3
lxml>=5.3.0
mmh3>=5.0.1
mock
multidict>=6.1.0
openai
Expand Down
6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,10 @@ markupsafe==2.1.3
# via jinja2
minio==7.1.13
# via shared
mmh3==4.0.1
# via shared
mmh3==5.0.1
# via
# -r requirements.in
# shared
mock==4.0.3
# via -r requirements.in
monotonic==1.5
Expand Down
18 changes: 13 additions & 5 deletions services/bigquery.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from types import ModuleType
from typing import Dict, List, cast
from typing import Dict, List, Sequence, cast

import polars as pl
from google.api_core import retry
Expand Down Expand Up @@ -108,7 +108,17 @@

self.write_client.batch_commit_write_streams(batch_commit_write_streams_request)

def query(self, query: str, params: dict | None = None) -> List[Dict]:
def query(
self,
query: str,
params: Sequence[
bigquery.ScalarQueryParameter
| bigquery.RangeQueryParameter
| bigquery.ArrayQueryParameter
| bigquery.StructQueryParameter
]
| None = None,
) -> List[Dict]:
"""Execute a BigQuery SQL query and return results.
Try not to write INSERT statements and use the write method instead.

Expand All @@ -125,9 +135,7 @@
job_config = bigquery.QueryJobConfig()

if params:
job_config.query_parameters = [
bigquery.ScalarQueryParameter(k, "STRING", v) for k, v in params.items()
]
job_config.query_parameters = params

Check warning on line 138 in services/bigquery.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/bigquery.py#L138

Added line #L138 was not covered by tests

row_iterator = self.client.query_and_wait(
query, job_config=job_config, retry=retry.Retry(deadline=30)
Expand Down
28 changes: 28 additions & 0 deletions services/tests/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import polars as pl
import pytest
from google.cloud.bigquery import ScalarQueryParameter

import generated_proto.testrun.ta_testrun_pb2 as ta_testrun_pb2
from services.bigquery import BigQueryService
Expand Down Expand Up @@ -63,6 +64,33 @@
assert {row["id"] for row in results} == {1, 2}


sql = """
WITH sample_data AS (
SELECT * FROM UNNEST([
STRUCT(TIMESTAMP '2025-01-01T00:00:00Z' AS timestamp, 1 AS id, 'name' AS name),
STRUCT(TIMESTAMP '2024-12-30T00:00:00Z' AS timestamp, 2 AS id, 'name2' AS name)
])
)
SELECT * FROM sample_data where id = @id
"""


@pytest.mark.skip(reason="This test requires being run using actual working creds")
def test_bigquery_service_params():
bigquery_service = BigQueryService(gcp_config)

Check warning on line 80 in services/tests/test_bigquery.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/tests/test_bigquery.py#L80

Added line #L80 was not covered by tests

results = bigquery_service.query(

Check warning on line 82 in services/tests/test_bigquery.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/tests/test_bigquery.py#L82

Added line #L82 was not covered by tests
sql, params=[ScalarQueryParameter("id", "INT64", 2)]
)

assert len(results) == 1
assert {row["timestamp"] for row in results} == {

Check warning on line 87 in services/tests/test_bigquery.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/tests/test_bigquery.py#L86-L87

Added lines #L86 - L87 were not covered by tests
datetime.fromisoformat("2024-12-30T00:00:00Z"),
}
assert {row["name"] for row in results} == {"name2"}
assert {row["id"] for row in results} == {2}

Check warning on line 91 in services/tests/test_bigquery.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/tests/test_bigquery.py#L90-L91

Added lines #L90 - L91 were not covered by tests


@pytest.mark.skip(reason="This test requires being run using actual working creds")
def test_bigquery_service_polars():
bigquery_service = BigQueryService(gcp_config)
Expand Down
1 change: 1 addition & 0 deletions ta_storage/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ def write_testruns(
upload: Upload,
framework: str | None,
testruns: list[test_results_parser.Testrun],
flaky_test_set: set[str],
):
pass
Loading
Loading