diff --git a/dataflow/snippets/tests/test_batch_process_storage.py b/dataflow/snippets/tests/test_batch_process_storage.py new file mode 100644 index 00000000000..36aa3374a28 --- /dev/null +++ b/dataflow/snippets/tests/test_batch_process_storage.py @@ -0,0 +1,50 @@ + # 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 pathlib import Path + + from ..batch_process_storage import parse_record, run + + + def test_parse_record() -> None: + assert parse_record("1001,widget,19.99") == ("widget", 19.99) + + + def test_run_sums_sales_per_product(tmp_path: Path) -> None: + input_dir = tmp_path / "input" + input_dir.mkdir() + (input_dir / "sales_1.csv").write_text( + "order_id,product,amount\n" + "1001,widget,19.99\n" + "1002,gadget,5.50\n" + "1003,widget,0.01\n" + ) + (input_dir / "sales_2.csv").write_text( + "order_id,product,amount\n2001,gadget,4.50\n2002,gizmo,100\n" + ) + output_dir = tmp_path / "output" + + # No --runner flag, so Apache Beam uses the local DirectRunner. + run([f"--input={input_dir}/*.csv", f"--output={output_dir}/sales"]) + + output_files = sorted(output_dir.glob("sales-*.csv")) + assert output_files + + rows = [] + for output_file in output_files: + header, *file_rows = output_file.read_text().splitlines() + assert header == "product,total_sales" + rows.extend(file_rows) + + # The header row of each input file is skipped, not summed or emitted. + assert sorted(rows) == ["gadget,10.00", "gizmo,100.00", "widget,20.00"]