Repository navigation
feat(dataflow): test_streaming_process_storage.py #15025
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
Open
cbonnie
wants to merge
1
commit into
main
Choose a base branch
from
cbonnie-patch-4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+108
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
dataflow/snippets/test/test_streaming_process_storage.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,108 @@ | ||||||||||||||
| # Copyright 2026 Google LLC | ||||||||||||||
| # | ||||||||||||||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||
| # you may not use this file except in compliance with the License. | ||||||||||||||
| # You may obtain a copy of the License at | ||||||||||||||
| # | ||||||||||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||
| # | ||||||||||||||
| # Unless required by applicable law or agreed to in writing, software | ||||||||||||||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||
| # See the License for the specific language governing permissions and | ||||||||||||||
| # limitations under the License. | ||||||||||||||
|
|
||||||||||||||
| from typing import Iterator | ||||||||||||||
| from unittest import mock | ||||||||||||||
| import uuid | ||||||||||||||
|
|
||||||||||||||
| from apache_beam.io import PubsubMessage | ||||||||||||||
| from apache_beam.testing.test_stream import TestStream | ||||||||||||||
| from apache_beam.transforms.window import TimestampedValue | ||||||||||||||
|
|
||||||||||||||
| from google.cloud import storage | ||||||||||||||
|
|
||||||||||||||
| import pytest | ||||||||||||||
|
|
||||||||||||||
| from ..streaming_process_storage import run | ||||||||||||||
|
|
||||||||||||||
| SALES_CSV = """order_id,product,amount | ||||||||||||||
| 1001,widget,19.99 | ||||||||||||||
| 1002,gadget,5.49 | ||||||||||||||
| 1003,widget,19.99 | ||||||||||||||
| 1004,gizmo,42.00 | ||||||||||||||
| """ | ||||||||||||||
|
|
||||||||||||||
| 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) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def notification(bucket_name: str, object_id: str, event_type: str) -> PubsubMessage: | ||||||||||||||
| """Builds a message like the ones that Cloud Storage sends to Pub/Sub.""" | ||||||||||||||
| return PubsubMessage( | ||||||||||||||
| data=b"", | ||||||||||||||
| attributes={ | ||||||||||||||
| "bucketId": bucket_name, | ||||||||||||||
| "objectId": object_id, | ||||||||||||||
| "eventType": event_type, | ||||||||||||||
| }, | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def fake_notifications(bucket_name: str) -> TestStream: | ||||||||||||||
| """Replaces ReadFromPubSub with a bounded stream of notifications.""" | ||||||||||||||
| return ( | ||||||||||||||
| TestStream() | ||||||||||||||
| .advance_watermark_to(0) | ||||||||||||||
| .add_elements( | ||||||||||||||
| [ | ||||||||||||||
| TimestampedValue( | ||||||||||||||
| notification(bucket_name, "input/sales.csv", "OBJECT_FINALIZE"), | ||||||||||||||
| 10, | ||||||||||||||
| ), | ||||||||||||||
| # The pipeline must ignore events other than OBJECT_FINALIZE. | ||||||||||||||
| # This object doesn't exist, so reading it would fail. | ||||||||||||||
| TimestampedValue( | ||||||||||||||
| notification(bucket_name, "input/deleted.csv", "OBJECT_DELETE"), | ||||||||||||||
| 20, | ||||||||||||||
| ), | ||||||||||||||
| ] | ||||||||||||||
| ) | ||||||||||||||
| .advance_watermark_to_infinity() | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| 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) | ||||||||||||||
|
Comment on lines
+83
to
+85
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the test function signature to accept the
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| with mock.patch( | ||||||||||||||
| "apache_beam.io.ReadFromPubSub", | ||||||||||||||
| lambda **kwargs: fake_notifications(bucket_name), | ||||||||||||||
| ): | ||||||||||||||
| run( | ||||||||||||||
| [ | ||||||||||||||
| "--subscription=projects/unused/subscriptions/unused", | ||||||||||||||
| f"--output=gs://{bucket_name}/output/", | ||||||||||||||
| ] | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| # Ensure the pipeline wrote the aggregated results to Cloud Storage. | ||||||||||||||
| blobs = [ | ||||||||||||||
| blob | ||||||||||||||
| for blob in storage_client.list_blobs(bucket_name, prefix="output/sales") | ||||||||||||||
| if blob.name.endswith(".csv") | ||||||||||||||
| ] | ||||||||||||||
| assert blobs | ||||||||||||||
| rows = [] | ||||||||||||||
| for blob in blobs: | ||||||||||||||
| rows.extend(blob.download_as_text().splitlines()) | ||||||||||||||
| assert sorted(rows) == ["gadget,5.49", "gizmo,42.00", "widget,39.98"] | ||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 astorage_clientfixture and inject it into thebucket_namefixture and the test function.References