def test_run_data_processing_pipeline_happy_path(
        mocker: MockFixture, aml_pipeline_mocks):  # NOQA: F811, E501
    """ Test Processing Pipeline build
        happy Path
    """
    # Load mocks from fixture
    (workspace, aml_compute, mock_workspace_get, mock_pipeline_publish) =\
        aml_pipeline_mocks

    mock_args = mocker.patch('argparse.ArgumentParser.parse_args')
    mock_args.return_value = edict({
        "aml_pipeline_name": "aml_pipeline_name_test",  # NOQA: E501
        "output_pipeline_id_file": "w",
        "skip_preprocessing_execution": False
    })

    mock_aml_pipelines_list = mocker.patch(
        'azureml.pipeline.core.PublishedPipeline.list')  # NOQA: E501
    type(mock_pipeline_publish).name = mocker.PropertyMock(
        return_value='aml_pipeline_name_test')  # NOQA: E501
    type(mock_pipeline_publish).id = mocker.PropertyMock(return_value='1')
    mock_aml_pipelines_list.return_value = [mock_pipeline_publish]
    mocker.patch('azureml.core.Experiment')

    # run as module (start from if __name__ == "__main__")
    runpy.run_module('ml_service.pipelines.'
                     'run_data_processing_pipeline',
                     run_name='__main__')
def test_find_pipeline_by_name(mocker: MockFixture, workspace):
    mock_aml_pipelines = mocker.patch(
        'azureml.pipeline.core.PublishedPipeline.list')
    mock_aml_pipeline = mocker.patch('azureml.pipeline.core.PublishedPipeline')
    name = mocker.PropertyMock(return_value="mock_pipeline_name")
    version = mocker.PropertyMock(return_value="2020-01-12T14:12:06.000")
    type(mock_aml_pipeline).name = name
    type(mock_aml_pipeline).version = version
    mock_aml_pipelines.return_value = {mock_aml_pipeline}

    matched_pipelines = find_pipeline_by_name(
        aml_workspace=workspace, pipeline_name="mock_pipeline_name")
    assert matched_pipelines
示例#3
0
    async def test_ok_wo_sql(self, model_manager: BaseModelManager, mocker: MockFixture):
        fake_fetch = mocker.Mock()
        mocked_get_sql = mocker.patch.object(model_manager, 'get_sql')
        mocked_connection = CoroutineMock()
        mocker.patch.object(model_manager, 'sql')
        mocker.patch.object(
            BaseModelManager,
            'engine',
            mocker.PropertyMock(
                return_value=mocker.Mock(
                    acquire=async_context_mock(return_value=mocked_connection)
                )
            )
        )
        mocked_run_query_with_connection = mocker.patch.object(
            model_manager, 'run_query_with_connection', CoroutineMock()
        )

        compared_return = await model_manager.run_query(fetch=fake_fetch)

        mocked_get_sql.assert_called_once_with()
        assert compared_return == mocked_run_query_with_connection.return_value
        mocked_run_query_with_connection.assert_called_once_with(
            mocked_connection, mocked_get_sql.return_value, fake_fetch
        )
def test_get_aml_compute_already_exists(mocker: MockFixture, workspace):
    expected_compute = mocker.patch('azureml.core.compute.ComputeTarget',
                                    spec=True)
    compute_targets = mocker.PropertyMock(
        return_value={"compute_target": expected_compute})
    type(workspace).compute_targets = compute_targets
    compute = get_compute(workspace=workspace, compute_name="compute_target")
    assert compute == expected_compute
示例#5
0
    async def test_acquire_ok(self, model_manager: BaseModelManager, mocker: MockFixture):
        fake_sql = mocker.Mock()
        fake_fetch = mocker.Mock()
        mocked_run_query_with_connection = mocker.patch.object(
            model_manager, 'run_query_with_connection', CoroutineMock()
        )
        mocked_connection = CoroutineMock()
        mocker.patch.object(
            BaseModelManager,
            'engine',
            mocker.PropertyMock(
                return_value=mocker.Mock(
                    acquire=async_context_mock(return_value=mocked_connection)
                )
            )
        )

        await model_manager.run_query(fake_sql, fake_fetch)

        mocked_run_query_with_connection.assert_called_once_with(mocked_connection, fake_sql, fake_fetch)
示例#6
0
def test_build_data_processing_os_cmd_pipeline_default_datastore(
        mocker: MockFixture,
        environment_vars,  # NOQA: F811
        aml_pipeline_mocks):  # NOQA: F811
    """ Test Processing Pipeline build
        test branch get default datastore
    """
    # Load mocks and data objects from fixture
    (workspace, aml_compute, mock_workspace_get, mock_pipeline_publish) =\
        aml_pipeline_mocks

    # Reset datastore_name from env_variables
    type(environment_vars).datastore_name =\
        mocker.PropertyMock(return_value=None)
    # From https://realpython.com/python-data-structures/#collectionsnamedtuple-convenient-data-objects  # NOQA: E501
    def_datastore_name = 'default_datastore'
    Def_datastore = namedtuple("Def_datastore", "name")
    ddi = Def_datastore(def_datastore_name)
    # Mock return value of aml_workspace.get_default_datastore().name
    mocker.patch('azureml.core.Workspace.get_default_datastore',
                 return_value=ddi)

    # Create Spies
    spy_pythonscriptstep_create =\
        mocker.patch('azureml.pipeline.steps.PythonScriptStep',
                     wraps=PythonScriptStep)
    spy_pipeline_create = mocker.patch('azureml.pipeline.core.Pipeline',
                                       wraps=Pipeline)

    # run as module (start from if __name__ == "__main__")
    runpy.run_module(
        'ml_service.pipelines.'
        'build_data_processing_os_cmd_pipeline',
        run_name='__main__')
    # Assertions

    # Load Mocked environment variables
    e = Env()

    # Check if the correct workspace retrieved
    mock_workspace_get.assert_called_with(name=e.workspace_name,
                                          resource_group=e.resource_group,
                                          subscription_id=e.subscription_id)

    # Check if PythonScriptStep instantiation was called correctly
    spy_pythonscriptstep_create.\
        assert_called_once_with(allow_reuse=False,
                                runconfig=ANY,
                                arguments=ANY,
                                source_directory=e.sources_directory_train,
                                script_name="preprocess/"
                                            "preprocess_os_cmd_aml.py",
                                name="Preprocess Data with OS cmd",
                                compute_target=ANY)

    assert spy_pythonscriptstep_create.call_args[1]['arguments'][0] ==\
        '--dataset_name'
    assert spy_pythonscriptstep_create.call_args[1]['arguments'][1] ==\
        e.dataset_name
    assert spy_pythonscriptstep_create.call_args[1]['arguments'][2] ==\
        '--datastore_name'
    assert spy_pythonscriptstep_create.call_args[1]['arguments'][3] ==\
        def_datastore_name

    # Check if Pipeline instantiation was called correctly
    spy_pipeline_create.assert_called_once_with(workspace=workspace, steps=ANY)
    assert spy_pipeline_create.call_args[1]['steps'][0]._runconfig ==\
        spy_pythonscriptstep_create.call_args[1]['runconfig']
    assert spy_pipeline_create.call_args[1]['steps'][0]._compute_target ==\
        spy_pythonscriptstep_create.call_args[1]['compute_target']

    # Check if Pipeline publish was called with arguments
    mock_pipeline_publish.\
        assert_called_once_with(name=e.preprocessing_pipeline_name,
                                description="Data preprocessing"
                                            " OS cmd pipeline",
                                version=e.build_id)
示例#7
0
def environment_vars(mocker: MockFixture):
    """ environment_vars
        Auto fixture - will be always present if imported by unit tests

        Sets basic env variables needed.

        Prevents developer's .env file from being used

        It is possible to overwrite their values within a test case with

            type(environment_vars).<env_variable_name> =\
                mocker.PropertyMock(return_value="<env_variable_value>")
    """
    # In search for the instance attribute mock
    environment_vars = MagicMock(spec=Env)

    type(environment_vars).sources_directory_train =\
        mocker.PropertyMock(return_value="ml_model")

    type(environment_vars).aml_env_name =\
        mocker.PropertyMock(return_value="utest_env_name")

    type(environment_vars).aml_env_train_conda_dep_file =\
        mocker.PropertyMock(return_value="utest_conda_dependencies.yml")

    type(environment_vars).dataset_name =\
        mocker.PropertyMock(return_value="utest_dataset")

    type(environment_vars).subscription_id =\
        mocker.PropertyMock(return_value="utest_subid")

    type(environment_vars).resource_group =\
        mocker.PropertyMock(return_value="utest_rg")

    type(environment_vars).workspace_name =\
        mocker.PropertyMock(return_value="utest_workspace")

    type(environment_vars).compute_name =\
        mocker.PropertyMock(return_value="utest_computename")

    type(environment_vars).vm_size =\
        mocker.PropertyMock(return_value="utest_vm_size")

    type(environment_vars).rebuild_env =\
        mocker.PropertyMock(return_value="False")

    type(environment_vars).datastore_name =\
        mocker.PropertyMock(return_value="utest_datastore")

    type(environment_vars).processed_dataset_name =\
        mocker.PropertyMock(return_value="utest_processed_dataset")

    type(environment_vars).app_insights_connection_string =\
        mocker.PropertyMock(return_value="utest_connectionstring")

    type(environment_vars).log_level =\
        mocker.PropertyMock(return_value="DEBUG")

    type(environment_vars).log_sampling_rate =\
        mocker.PropertyMock(return_value="1.0")

    type(environment_vars).trace_sampling_rate =\
        mocker.PropertyMock(return_value="1.0")

    type(environment_vars).metrics_export_interval =\
        mocker.PropertyMock(return_value="15")

    # Returns mocked Env() instance every time it is instanciated
    mocker.patch('ml_service.util.env_variables.Env.__new__',
                 return_value=environment_vars)

    return environment_vars
def test_get_aml_compute_throws_exception(mocker: MockFixture, workspace):
    compute_targets = mocker.PropertyMock(
        side_effect=ComputeTargetException("Error while creating compute!"))
    type(workspace).compute_targets = compute_targets
    with pytest.raises(SystemExit):
        get_compute(workspace=workspace, compute_name="compute_target")
示例#9
0
def test_mocker_has_property_mock_class_as_attribute_for_instantiation():
    from pytest_mock import mock_module, MockFixture

    mocker = MockFixture()
    assert isinstance(mocker.PropertyMock(), mock_module.PropertyMock)