jonathansudhakar1 opened a new pull request, #40969:
URL: https://github.com/apache/beam/pull/40969

   Fixes #20521.
   
   Several code samples on [Test Your 
Pipeline](https://beam.apache.org/documentation/pipelines/test-your-pipeline/) 
don't run as written. This PR fixes the Python ones that the issue reports, 
plus the Go ones on the same page, which don't compile.
   
   **Python**
   - **TestPipeline snippet:** `with TestPipeline as p:` used the class without 
importing it, and passed the class rather than an instance to `with`. Running 
it raises `NameError`, and with the import added, `TypeError: 'type' object 
does not support the context manager protocol`. The snippet now imports 
`TestPipeline` and instantiates it.
   - **WordCount test** (the issue's "last section"):
     - The placeholder `CountWords` class had only comments in its body, so the 
file didn't parse (`IndentationError`).
     - The test read `WORDS` and `EXPECTED_COUNTS` without `self.`.
     - The input was single words, so `"hi: 5"` could never match.
   
     The test now imports the real `CountWords` from 
`apache_beam/examples/wordcount_debugging.py`, which the old comment already 
linked, and formats its `(word, count)` output. It uses the same input lines as 
the Java example and qualifies the class attributes, so the snippet runs as-is.
   
   **Go**
   - **PAssert snippet:** used Python list syntax (`["elem1", ...]`) instead of 
`[]string{...}`.
   - **Composite transform test:**
     - It didn't compile: it was missing the `fmt` import, counted an undefined 
`col`, and passed a `[]string` to the variadic `passert.Equals` (`cannot use 
wantCounts (variable of type []string) as []any value`). It now uses 
`passert.EqualsList`.
     - Its expected counts didn't match its input. They now match the Java and 
Python versions of the same test.
   - **WordCount test:** same `passert.Equals` problem. Its input was single 
words, so `CountWords` couldn't produce `"hi: 5"`; it now uses the same lines 
as Java.
   
   The Java examples on this page have their own problems: the composite test 
calls `TestPipeline.create()` without `@Rule`, and the WordCount test assigns 
`CountWords` output (`KV<String, Long>`) to a `PCollection<String>`. Fixing 
them needs a Gradle build to verify, so I've kept them out of this PR to keep 
it focused.
   
   ## Testing
   
   I extracted every `{{< highlight py >}}` and `{{< highlight go >}}` block 
from the page with a small script and ran them at `ae065d1302a`, before and 
after the change.
   
   Python (Python 3.13 venv with `apache_beam` installed editable):
   - Before:
     - TestPipeline snippet: `NameError: name 'TestPipeline' is not defined`, 
then `TypeError` with the import added.
     - WordCount test: `IndentationError: expected an indented block after 
class definition`.
   - After:
     - `python block1.py` (the TestPipeline snippet) exits 0.
     - `python -m pytest -v block3.py block4.py` runs the composite Count test 
and the WordCount test: `2 passed`.
     - As a check that the assertion runs, changing `"hi: 5"` to `"hi: 4"` 
makes the WordCount test fail with `BeamAssertException ... unexpected elements 
['hi: 5'], missing elements ['hi: 4']`.
   
   Go (go1.26.5; each snippet placed in a temporary `_test.go` package under 
`sdks/go` with a `ptest.Main` `TestMain`, deleted afterwards):
   - Before: `go vet` fails on the composite test (`undefined: fmt`, then 
`cannot use wantCounts ...`).
   - After:
     - `go vet` passes, and `go test -count=1 -v` passes for:
       - the PAssert snippet, with `output` bound to a real PCollection;
       - the composite test, as-is;
       - the WordCount test, with the omitted `CountWords`/`formatFn` filled in 
from `sdks/go/examples/debugging_wordcount`.
     - All runs used the default Prism runner.
     - Changing one expected value in each test makes all three fail with 
`actual PCollection does not match expected values`.
   
   Docs-only change, so no CHANGES.md entry.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to