damccorm commented on code in PR #31701:
URL: https://github.com/apache/beam/pull/31701#discussion_r1672046384
##########
examples/notebooks/blogposts/unittests_in_beam.ipynb:
##########
@@ -0,0 +1,259 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "authorship_tag": "ABX9TyP+whTO0l5Xd2TU4xa2Z7KC",
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "<a
href=\"https://colab.research.google.com/github/svetakvsundhar/beam/blob/testing_blog_post/examples/notebooks/blogposts/unittests_in_beam.ipynb\"
target=\"_parent\"><img
src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In
Colab\"/></a>"
Review Comment:
```suggestion
"<a
href=\"https://colab.research.google.com/github/apache/beam/blob/testing_blog_post/examples/notebooks/blogposts/unittests_in_beam.ipynb\"
target=\"_parent\"><img
src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In
Colab\"/></a>"
```
##########
examples/notebooks/blogposts/unittests_in_beam.ipynb:
##########
@@ -0,0 +1,259 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "authorship_tag": "ABX9TyP+whTO0l5Xd2TU4xa2Z7KC",
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "<a
href=\"https://colab.research.google.com/github/svetakvsundhar/beam/blob/testing_blog_post/examples/notebooks/blogposts/unittests_in_beam.ipynb\"
target=\"_parent\"><img
src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In
Colab\"/></a>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 36,
+ "metadata": {
+ "id": "7DSE6TgWy7PP"
+ },
+ "outputs": [],
+ "source": [
+ "# @title ###### Licensed to the Apache Software Foundation (ASF),
Version 2.0 (the \"License\")\n",
+ "\n",
+ "# Licensed to the Apache Software Foundation (ASF) under one\n",
+ "# or more contributor license agreements. See the NOTICE file\n",
+ "# distributed with this work for additional information\n",
+ "# regarding copyright ownership. The ASF licenses this file\n",
+ "# to you under the Apache License, Version 2.0 (the\n",
+ "# \"License\"); you may not use this file except in compliance\n",
+ "# with the License. You may obtain a copy of the License at\n",
+ "#\n",
+ "# http://www.apache.org/licenses/LICENSE-2.0\n",
+ "#\n",
+ "# Unless required by applicable law or agreed to in writing,\n",
+ "# software distributed under the License is distributed on an\n",
+ "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
+ "# KIND, either express or implied. See the License for the\n",
+ "# specific language governing permissions and limitations\n",
+ "# under the License"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Install the Apache Beam library\n",
+ "\n",
+ "!pip install apache_beam[gcp] --quiet"
+ ],
+ "metadata": {
+ "id": "5W2nuV7uzlPg"
+ },
+ "execution_count": 37,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#The following packages are used to run the example pipelines\n",
+ "\n",
+ "import apache_beam as beam\n",
+ "from apache_beam.io import ReadFromText, WriteToText\n",
+ "from apache_beam.options.pipeline_options import PipelineOptions\n",
+ "\n",
+ "class CustomClass(beam.DoFn):\n",
+ " def custom_function(x):\n",
+ " ...\n",
+ " # returned_record =
requests.get(\"http://my-api-call.com\")\n",
+ " ...\n",
+ " # if len(returned_record)!=10:\n",
+ " # raise ValueError(\"Length of record does not match
expected length\")\n",
+ " return x\n",
+ "\n",
+ " with beam.Pipeline() as p:\n",
+ " result = (\n",
+ " p\n",
+ " | ReadFromText(\"/content/sample_data/anscombe.json\")\n",
+ " | beam.ParDo(lambda x: CustomClass.custom_function(x))\n",
+ " | WriteToText(\"/content/\")\n",
+ " )"
+ ],
+ "metadata": {
+ "id": "Ktk9EVIFzGfP"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**Example Pipeline 1**\n"
+ ],
+ "metadata": {
+ "id": "IVjBkewt1sLA"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# This function is going to return the square the integer at the
first index of our record.\n",
+ "def compute_square(element):\n",
+ " return int(element[1])**2\n",
+ "\n",
+ "with beam.Pipeline() as p1:\n",
+ " result = (\n",
+ " p1\n",
+ " |
ReadFromText(\"/content/sample_data/california_housing_test.csv\",skip_header_lines=1)\n",
+ " | beam.Map(compute_square)\n",
+ " | WriteToText(\"/content/\")\n",
+ " )"
+ ],
+ "metadata": {
+ "id": "oHbSvOUI1pOe"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**Example Pipeline 2**"
+ ],
+ "metadata": {
+ "id": "Mh3nZZ1_12sX"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "with beam.Pipeline() as p2:\n",
+ " result = (\n",
+ " p2\n",
+ " | ReadFromText(\"/content/sample_data/anscombe.json\")\n",
+ " | beam.Map(str.strip)\n",
Review Comment:
I wonder if a better way to do this would be to split `beam.Map(str.strip)`
out into a separate function which can be called from the test. As it is, the
test isn't actually invoking any of the code we've written.
A more interesting example might be:
```
def manipulate_strings(incoming_pcoll):
return incoming_pcoll | beam.Map(str.strip) | beam.Map(str.upper)
```
The functions themselves don't need tested, but the beam transforms do. That
would let you test the actual code you've written below with:
```
with TestPipeline() as p:
inputs = p | beam.Create(strings)
output = manipulate_strings(inputs)
assert_that(output, equal_to(expected))
```
##########
examples/notebooks/blogposts/unittests_in_beam.ipynb:
##########
@@ -0,0 +1,259 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "authorship_tag": "ABX9TyP+whTO0l5Xd2TU4xa2Z7KC",
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "<a
href=\"https://colab.research.google.com/github/svetakvsundhar/beam/blob/testing_blog_post/examples/notebooks/blogposts/unittests_in_beam.ipynb\"
target=\"_parent\"><img
src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In
Colab\"/></a>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 36,
+ "metadata": {
+ "id": "7DSE6TgWy7PP"
+ },
+ "outputs": [],
+ "source": [
+ "# @title ###### Licensed to the Apache Software Foundation (ASF),
Version 2.0 (the \"License\")\n",
+ "\n",
+ "# Licensed to the Apache Software Foundation (ASF) under one\n",
+ "# or more contributor license agreements. See the NOTICE file\n",
+ "# distributed with this work for additional information\n",
+ "# regarding copyright ownership. The ASF licenses this file\n",
+ "# to you under the Apache License, Version 2.0 (the\n",
+ "# \"License\"); you may not use this file except in compliance\n",
+ "# with the License. You may obtain a copy of the License at\n",
+ "#\n",
+ "# http://www.apache.org/licenses/LICENSE-2.0\n",
+ "#\n",
+ "# Unless required by applicable law or agreed to in writing,\n",
+ "# software distributed under the License is distributed on an\n",
+ "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
+ "# KIND, either express or implied. See the License for the\n",
+ "# specific language governing permissions and limitations\n",
+ "# under the License"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Install the Apache Beam library\n",
+ "\n",
+ "!pip install apache_beam[gcp] --quiet"
+ ],
+ "metadata": {
+ "id": "5W2nuV7uzlPg"
+ },
+ "execution_count": 37,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#The following packages are used to run the example pipelines\n",
+ "\n",
+ "import apache_beam as beam\n",
+ "from apache_beam.io import ReadFromText, WriteToText\n",
+ "from apache_beam.options.pipeline_options import PipelineOptions\n",
+ "\n",
+ "class CustomClass(beam.DoFn):\n",
+ " def custom_function(x):\n",
+ " ...\n",
+ " # returned_record =
requests.get(\"http://my-api-call.com\")\n",
+ " ...\n",
+ " # if len(returned_record)!=10:\n",
+ " # raise ValueError(\"Length of record does not match
expected length\")\n",
+ " return x\n",
+ "\n",
+ " with beam.Pipeline() as p:\n",
+ " result = (\n",
+ " p\n",
+ " | ReadFromText(\"/content/sample_data/anscombe.json\")\n",
+ " | beam.ParDo(lambda x: CustomClass.custom_function(x))\n",
+ " | WriteToText(\"/content/\")\n",
+ " )"
+ ],
+ "metadata": {
+ "id": "Ktk9EVIFzGfP"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**Example Pipeline 1**\n"
+ ],
+ "metadata": {
+ "id": "IVjBkewt1sLA"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# This function is going to return the square the integer at the
first index of our record.\n",
+ "def compute_square(element):\n",
+ " return int(element[1])**2\n",
+ "\n",
+ "with beam.Pipeline() as p1:\n",
+ " result = (\n",
+ " p1\n",
+ " |
ReadFromText(\"/content/sample_data/california_housing_test.csv\",skip_header_lines=1)\n",
+ " | beam.Map(compute_square)\n",
+ " | WriteToText(\"/content/\")\n",
+ " )"
+ ],
+ "metadata": {
+ "id": "oHbSvOUI1pOe"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**Example Pipeline 2**"
+ ],
+ "metadata": {
+ "id": "Mh3nZZ1_12sX"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "with beam.Pipeline() as p2:\n",
+ " result = (\n",
+ " p2\n",
+ " | ReadFromText(\"/content/sample_data/anscombe.json\")\n",
+ " | beam.Map(str.strip)\n",
Review Comment:
Basically, I don't like that you could totally change the user code (which
you're supposed to be testing) without a test failing.
--
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]