damccorm commented on code in PR #32336:
URL: https://github.com/apache/beam/pull/32336#discussion_r1745766872
##########
examples/notebooks/blog/unittests_in_beam.ipynb:
##########
@@ -0,0 +1,359 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "authorship_tag": "ABX9TyNotxIojmTBy/ZYTsbd/4Is",
+ "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/apache/beam/blob/colab_testing_example/examples/notebooks/blog/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/master/examples/notebooks/blog/unittests_in_beam.ipynb\"
target=\"_parent\"><img
src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In
Colab\"/></a>"
```
##########
examples/notebooks/blog/unittests_in_beam.ipynb:
##########
@@ -0,0 +1,359 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "authorship_tag": "ABX9TyNotxIojmTBy/ZYTsbd/4Is",
+ "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/apache/beam/blob/colab_testing_example/examples/notebooks/blog/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": null,
+ "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",
+ "!pip install apache_beam[gcp] --quiet"
+ ],
+ "metadata": {
+ "id": "5W2nuV7uzlPg"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "***Notebook Description***\n",
+ "\n",
+ "This colab notebook is referenced in the Unit Testing in Beam blog
post, an opinionated guide on authoring unit tests for Apache Beam pipelines.
Note that this code is used solely for conveying the best practices of unit
testing, and thus is not intended to be used out of the box in user pipelines.
The code references files native in colab, and thus executing the cells in
order will provide the intended output."
+ ],
+ "metadata": {
+ "id": "_KqwMLN9kRXf"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**Example 1**\n",
+ "\n",
+ "This `DoFn` (and corresponding pipeline) is used to convey a
situation in which a `DoFn` makes an API call. Note that an error is raised
here if the length of the API response (returned_record) is less than length
10."
+ ],
+ "metadata": {
+ "id": "Z8__izORM3r8"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Fake client to simulate an external call\n",
+ "\n",
+ "import time\n",
+ "class Client():\n",
+ " def get_data(self, api):\n",
+ " time.sleep(3)\n",
+ " return [0,1,2,3,4,5,6,7,8,9]\n",
+ "\n",
+ "MyApiCall = Client()"
+ ],
+ "metadata": {
+ "id": "GGPF7cY3Ntyj"
+ },
+ "execution_count": null,
+ "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 MyDoFn(beam.DoFn):\n",
+ " def process(self,element):\n",
+ " returned_record =
MyApiCall.get_data(\"http://my-api-call.com\")\n",
+ " if len(returned_record)!=10:\n",
+ " raise ValueError(\"Length of record does not match
expected length\")\n",
+ " yield returned_record\n",
+ "\n",
+ "with beam.Pipeline() as p:\n",
+ " result = (\n",
+ " p\n",
+ " | ReadFromText(\"/content/sample_data/anscombe.json\")\n",
+ " | beam.ParDo(MyDoFn())\n",
+ " | WriteToText(\"/content/example1\")\n",
+ " )"
+ ],
+ "metadata": {
+ "id": "Ktk9EVIFzGfP"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**Mocking Example**\n",
+ "\n",
+ "The following blocks of code illustrate how we can mock an API
response, to test out the error message we've written. Note that we can use
mocking to avoid making the actual API call in our test."
+ ],
+ "metadata": {
+ "id": "58GVMyMa2PwE"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "!pip install mock # Install the 'mock' module"
+ ],
+ "metadata": {
+ "id": "ESclJ_G-6JcW"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# We import the mock package for mocking functionality.\n",
+ "from unittest.mock import Mock,patch\n",
+ "# from MyApiCall import get_data\n",
+ "import mock\n",
+ "\n",
+ "\n",
+ "# MyApiCall is a function that calls get_data to fetch some data via
an API call.\n",
+ "@patch('MyApiCall.get_data')\n",
+ "def test_error_message_wrong_length(self, mock_get_data):\n",
Review Comment:
I noticed we're not actually invoking these tests ever, so this isn't really
getting shown off. I think it is probably fine, but maybe worth calling out in
a comment (something like `# tests not invoked by colab, but could be run using
https://docs.pytest.org/en/stable/`
--
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]