|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from datetime import datetime as dt |
| 16 | +import os |
| 17 | + |
| 18 | +from google.cloud import bigquery, storage |
| 19 | +from google.genai.types import JobState |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +import submit_with_bq |
| 24 | +import submit_with_gcs |
| 25 | + |
| 26 | + |
| 27 | +os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True" |
| 28 | +os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1" |
| 29 | +# The project name is included in the CICD pipeline |
| 30 | +# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name" |
| 31 | +BQ_OUTPUT_DATASET = f"{os.environ['GOOGLE_CLOUD_PROJECT']}.gen_ai_batch_prediction" |
| 32 | +GCS_OUTPUT_BUCKET = "python-docs-samples-tests" |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture(scope="session") |
| 36 | +def bq_output_uri(): |
| 37 | + table_name = f"text_output_{dt.now().strftime('%Y_%m_%d_T%H_%M_%S')}" |
| 38 | + table_uri = f"{BQ_OUTPUT_DATASET}.{table_name}" |
| 39 | + |
| 40 | + yield f"bq://{table_uri}" |
| 41 | + |
| 42 | + bq_client = bigquery.Client() |
| 43 | + bq_client.delete_table(table_uri, not_found_ok=True) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture(scope="session") |
| 47 | +def gcs_output_uri(): |
| 48 | + prefix = f"text_output/{dt.now()}" |
| 49 | + |
| 50 | + yield f"gs://{GCS_OUTPUT_BUCKET}/{prefix}" |
| 51 | + |
| 52 | + storage_client = storage.Client() |
| 53 | + bucket = storage_client.get_bucket(GCS_OUTPUT_BUCKET) |
| 54 | + blobs = bucket.list_blobs(prefix=prefix) |
| 55 | + for blob in blobs: |
| 56 | + blob.delete() |
| 57 | + |
| 58 | + |
| 59 | +def test_batch_prediction_with_bq(bq_output_uri) -> None: |
| 60 | + response = submit_with_bq.generate_content(output_uri=bq_output_uri) |
| 61 | + assert response == JobState.JOB_STATE_SUCCEEDED |
| 62 | + |
| 63 | + |
| 64 | +def test_batch_prediction_with_gcs(gcs_output_uri) -> None: |
| 65 | + response = submit_with_gcs.generate_content(output_uri=gcs_output_uri) |
| 66 | + assert response == JobState.JOB_STATE_SUCCEEDED |
0 commit comments