Repository navigation
Conversation
Add automated test for the Cloud Storage to DataFlow streaming sample.
There was a problem hiding this comment.
Code Review
This pull request introduces a new test file, test_streaming_process_storage.py, to verify the streaming process storage pipeline using Apache Beam. The feedback suggests refactoring the module-level storage.Client() initialization into a pytest fixture to avoid test discovery failures in environments without active Google Cloud credentials, and updating the test function signature to accept this new fixture.
| storage_client = storage.Client() | ||
|
|
||
|
|
||
| @pytest.fixture(scope="function") | ||
| def bucket_name() -> Iterator[str]: | ||
| bucket = storage_client.create_bucket(f"test-bucket-{uuid.uuid4()}") | ||
| try: | ||
| yield bucket.name | ||
| finally: | ||
| bucket.delete(force=True) |
There was a problem hiding this comment.
Initializing storage.Client() at the module level can cause test discovery to fail in environments where active Google Cloud credentials are not configured. It is safer and more idiomatic to define a storage_client fixture and inject it into the bucket_name fixture and the test function.
| storage_client = storage.Client() | |
| @pytest.fixture(scope="function") | |
| def bucket_name() -> Iterator[str]: | |
| bucket = storage_client.create_bucket(f"test-bucket-{uuid.uuid4()}") | |
| try: | |
| yield bucket.name | |
| finally: | |
| bucket.delete(force=True) | |
| @pytest.fixture(scope="session") | |
| def storage_client() -> storage.Client: | |
| return storage.Client() | |
| @pytest.fixture(scope="function") | |
| def bucket_name(storage_client: storage.Client) -> Iterator[str]: | |
| bucket = storage_client.create_bucket(f"test-bucket-{{uuid.uuid4()}}") | |
| try: | |
| yield bucket.name | |
| finally: | |
| bucket.delete(force=True) |
References
- Maintain consistency in test structures by matching the patterns employed by existing or neighboring tests in the same directory, such as using a try...finally block with a teardown function for resource cleanup.
| def test_streaming_process_storage(bucket_name: str) -> None: | ||
| bucket = storage_client.bucket(bucket_name) | ||
| bucket.blob("input/sales.csv").upload_from_string(SALES_CSV) |
There was a problem hiding this comment.
Update the test function signature to accept the storage_client fixture so that it is properly injected and used within the test.
| def test_streaming_process_storage(bucket_name: str) -> None: | |
| bucket = storage_client.bucket(bucket_name) | |
| bucket.blob("input/sales.csv").upload_from_string(SALES_CSV) | |
| def test_streaming_process_storage(storage_client: storage.Client, bucket_name: str) -> None: | |
| bucket = storage_client.bucket(bucket_name) | |
| bucket.blob("input/sales.csv").upload_from_string(SALES_CSV) |
Add automated test for the Cloud Storage to DataFlow streaming sample.
Description
Bug # http://b/589715201
Checklist
Testing
Compliance & Style
Post-Approval Actions