robertwb commented on code in PR #31701:
URL: https://github.com/apache/beam/pull/31701#discussion_r1672783283
##########
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/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>"
+ ]
+ },
+ {
+ "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",
+ " | WriteToText(\"/content/sample_data/\")\n",
+ " )"
+ ],
+ "metadata": {
+ "id": "hmO1Chl51vPG"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**Unit Tests for Pipelines**"
+ ],
+ "metadata": {
+ "id": "uoNJLQl_15gj"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# The following packages are imported for unit testing.\n",
+ "import unittest\n",
+ "import apache_beam as beam\n",
+ "from apache_beam.testing.test_pipeline import TestPipeline\n",
Review Comment:
Is three any advantage to users of using `TestPipeline`?
--
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]