Skip to content

Commit

Permalink
use magic mock instead of mocking entire function
Browse files Browse the repository at this point in the history
  • Loading branch information
riteshghorse committed Feb 9, 2024
1 parent b7d62bc commit 4c816b2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import socket
import subprocess
import sys
import tempfile
import time
import unittest
from unittest.mock import MagicMock

import grpc

Expand All @@ -44,7 +46,6 @@
from apache_beam.testing.util import equal_to
from apache_beam.transforms import environments
from apache_beam.transforms import userstate
from apache_beam.transforms.environments_test import mock_python_sdk_dependencies

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -310,11 +311,12 @@ def create_options(self):

class PortableRunnerInternalTest(unittest.TestCase):
def setUp(self) -> None:
self.actual_python_sdk_dependencies = environments.python_sdk_dependencies
environments.python_sdk_dependencies = mock_python_sdk_dependencies
self.tmp_dir = tempfile.TemporaryDirectory()
tempfile.mkdtemp = MagicMock(return_value=self.tmp_dir.name)

def tearDown(self) -> None:
environments.python_sdk_dependencies = self.actual_python_sdk_dependencies
self.tmp_dir.cleanup()
tempfile.mkdtemp.reset_mock()

def test__create_default_environment(self):
docker_image = environments.DockerEnvironment.default_docker_image()
Expand Down
27 changes: 5 additions & 22 deletions sdks/python/apache_beam/transforms/environments_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,20 @@
import logging
import tempfile
import unittest
from unittest.mock import MagicMock

from apache_beam.options.pipeline_options import PortableOptions
from apache_beam.options.pipeline_options import SetupOptions
from apache_beam.portability import common_urns
from apache_beam.runners import pipeline_context
from apache_beam.runners.portability import stager
from apache_beam.transforms import environments
from apache_beam.transforms.environments import DockerEnvironment
from apache_beam.transforms.environments import EmbeddedPythonEnvironment
from apache_beam.transforms.environments import EmbeddedPythonGrpcEnvironment
from apache_beam.transforms.environments import Environment
from apache_beam.transforms.environments import ExternalEnvironment
from apache_beam.transforms.environments import ProcessEnvironment
from apache_beam.transforms.environments import PyPIArtifactRegistry
from apache_beam.transforms.environments import SubprocessSDKEnvironment

# create a temp directory so that all artifacts are put in the same directory.
tmp_dir = tempfile.mkdtemp()


def mock_python_sdk_dependencies(options, tmp_dir=tmp_dir):
skip_prestaged_dependencies = options.view_as(
SetupOptions).prebuild_sdk_container_engine is not None
return stager.Stager.create_job_resources(
options,
tmp_dir,
pypi_requirements=[
artifact[0] + artifact[1]
for artifact in PyPIArtifactRegistry.get_artifacts()
],
skip_prestaged_dependencies=skip_prestaged_dependencies)


class RunnerApiTest(unittest.TestCase):
def test_environment_encoding(self):
Expand Down Expand Up @@ -103,11 +85,12 @@ def test_default_capabilities(self):

class EnvironmentOptionsTest(unittest.TestCase):
def setUp(self) -> None:
self.actual_python_sdk_dependencies = environments.python_sdk_dependencies
environments.python_sdk_dependencies = mock_python_sdk_dependencies
self.tmp_dir = tempfile.TemporaryDirectory()
tempfile.mkdtemp = MagicMock(return_value=self.tmp_dir.name)

def tearDown(self) -> None:
environments.python_sdk_dependencies = self.actual_python_sdk_dependencies
self.tmp_dir.cleanup()
tempfile.mkdtemp.reset_mock()

def test_process_variables_empty(self):
options = PortableOptions([
Expand Down

0 comments on commit 4c816b2

Please sign in to comment.