def test_docs(test_configuration, tmp_path):
    workdir = os.getcwd()
    try:
        os.chdir(tmp_path)
        base_dir = test_configuration.get("base_dir", ".")
        # Ensure GE is installed in our environment
        ge_requirement = test_configuration.get("ge_requirement",
                                                "great_expectations")
        execute_shell_command(f"pip install {ge_requirement}")

        #
        # Build test state
        #

        # DataContext
        context_source_dir = os.path.join(
            base_dir, test_configuration.get("data_context_dir"))
        test_context_dir = os.path.join(tmp_path, "great_expectations")
        shutil.copytree(
            context_source_dir,
            test_context_dir,
        )

        # Test Data
        source_data_dir = os.path.join(base_dir,
                                       test_configuration.get("data_dir"))
        test_data_dir = os.path.join(tmp_path, "test_data")
        shutil.copytree(
            source_data_dir,
            test_data_dir,
        )

        # UAT Script
        script_source = os.path.join(
            test_configuration.get("base_dir"),
            test_configuration.get("user_flow_script"),
        )
        script_path = os.path.join(tmp_path, "test_script.py")
        shutil.copyfile(script_source, script_path)
        # Check initial state

        # Execute test
        res = subprocess.run(["python", script_path], capture_output=True)
        # Check final state
        outs = res.stdout.decode("utf-8")
        errs = res.stderr.decode("utf-8")
        print(outs)
        print(errs)
        assert len(errs) == 0
    except:
        raise
    finally:
        os.chdir(workdir)
def _execute_integration_test(integration_test_fixture, tmp_path):
    """
    Prepare and environment and run integration tests from a list of tests.

    Note that the only required parameter for a test in the matrix is
    `user_flow_script` and that all other parameters are optional.
    """
    workdir = os.getcwd()
    try:
        base_dir = file_relative_path(__file__, "../../")
        os.chdir(tmp_path)
        # Ensure GE is installed in our environment
        execute_shell_command("pip install .")

        #
        # Build test state
        # DataContext
        data_context_dir = integration_test_fixture.data_context_dir
        if data_context_dir:
            context_source_dir = os.path.join(base_dir, data_context_dir)
            test_context_dir = os.path.join(tmp_path, "great_expectations")
            shutil.copytree(
                context_source_dir,
                test_context_dir,
            )

        # Test Data
        data_dir = integration_test_fixture.data_dir
        if data_dir:
            source_data_dir = os.path.join(base_dir, data_dir)
            target_data_dir = os.path.join(tmp_path, "data")
            shutil.copytree(
                source_data_dir,
                target_data_dir,
            )

        # Other files
        # Other files to copy should be supplied as a tuple of tuples with source, dest pairs
        # e.g. (("/source1/file1", "/dest1/file1"), ("/source2/file2", "/dest2/file2"))
        other_files = integration_test_fixture.other_files
        if other_files:
            for file_paths in other_files:
                source_file = os.path.join(base_dir, file_paths[0])
                dest_file = os.path.join(tmp_path, file_paths[1])
                dest_dir = os.path.dirname(dest_file)
                if not os.path.exists(dest_dir):
                    os.makedirs(dest_dir)
                shutil.copyfile(src=source_file, dst=dest_file)

        # UAT Script
        user_flow_script = integration_test_fixture.user_flow_script
        script_source = os.path.join(
            base_dir,
            user_flow_script,
        )
        script_path = os.path.join(tmp_path, "test_script.py")
        shutil.copyfile(script_source, script_path)

        util_script = integration_test_fixture.util_script
        if util_script:
            script_source = os.path.join(base_dir, util_script)
            os.makedirs(os.path.join(tmp_path, "tests/"))
            util_script_path = os.path.join(tmp_path, "tests/test_utils.py")
            shutil.copyfile(script_source, util_script_path)

        # Run script as module, using python's importlib machinery (https://docs.python.org/3/library/importlib.htm)
        loader = importlib.machinery.SourceFileLoader("test_script_module", script_path)
        spec = importlib.util.spec_from_loader("test_script_module", loader)
        test_script_module = importlib.util.module_from_spec(spec)
        loader.exec_module(test_script_module)
    except Exception as e:
        logger.error(str(e))
    finally:
        os.chdir(workdir)
Example #3
0
def _execute_integration_test(test_configuration, tmp_path):
    """
    Prepare and environment and run integration tests from a list of tests.

    Note that the only required parameter for a test in the matrix is
    `user_flow_script` and that all other parameters are optional.
    """
    assert ("user_flow_script"
            in test_configuration.keys()), "a `user_flow_script` is required"
    workdir = os.getcwd()
    try:
        base_dir = test_configuration.get(
            "base_dir", file_relative_path(__file__, "../../"))
        os.chdir(tmp_path)
        # Ensure GE is installed in our environment
        ge_requirement = test_configuration.get("ge_requirement",
                                                "great_expectations")
        execute_shell_command(f"pip install {ge_requirement}")

        #
        # Build test state
        #

        # DataContext
        if test_configuration.get("data_context_dir"):
            context_source_dir = os.path.join(
                base_dir, test_configuration.get("data_context_dir"))
            test_context_dir = os.path.join(tmp_path, "great_expectations")
            shutil.copytree(
                context_source_dir,
                test_context_dir,
            )

        # Test Data
        if test_configuration.get("data_dir") is not None:
            source_data_dir = os.path.join(base_dir,
                                           test_configuration.get("data_dir"))
            test_data_dir = os.path.join(tmp_path, "data")
            shutil.copytree(
                source_data_dir,
                test_data_dir,
            )

        # UAT Script
        script_source = os.path.join(
            base_dir,
            test_configuration.get("user_flow_script"),
        )
        script_path = os.path.join(tmp_path, "test_script.py")
        shutil.copyfile(script_source, script_path)

        # Util Script
        if test_configuration.get("util_script") is not None:
            script_source = os.path.join(
                base_dir,
                test_configuration.get("util_script"),
            )
            util_script_path = os.path.join(tmp_path, "util.py")
            shutil.copyfile(script_source, util_script_path)

        # Check initial state

        # Execute test
        res = subprocess.run(["python", script_path], capture_output=True)
        # Check final state
        expected_stderrs = test_configuration.get("expected_stderrs")
        expected_stdouts = test_configuration.get("expected_stdouts")
        expected_failure = test_configuration.get("expected_failure")
        outs = res.stdout.decode("utf-8")
        errs = res.stderr.decode("utf-8")
        print(outs)
        print(errs)

        if expected_stderrs:
            assert expected_stderrs == errs

        if expected_stdouts:
            assert expected_stdouts == outs

        if expected_failure:
            assert res.returncode != 0
        else:
            assert res.returncode == 0
    except:
        raise
    finally:
        os.chdir(workdir)