def test_manual_instance():
    defn = PipelineDefinition([the_solid], "test")
    with pytest.raises(
            DagsterInvariantViolationError,
            match=
            "Reconstructable target should be a function or definition produced by a decorated function",
    ):
        reconstructable(defn)
def test_bad_target():
    with pytest.raises(
            DagsterInvariantViolationError,
            match=re.escape(
                "Loadable attributes must be either a JobDefinition, GraphDefinition, PipelineDefinition, "
                "AssetGroup, or RepositoryDefinition. Got None."),
    ):
        reconstructable(not_the_pipeline)
def test_not_defined_in_module(mocker):
    mocker.patch("inspect.getmodule",
                 return_value=types.ModuleType("__main__"))
    with pytest.raises(
            DagsterInvariantViolationError,
            match=re.escape(
                "reconstructable() can not reconstruct jobs or pipelines defined in interactive environments"
            ),
    ):
        reconstructable(get_the_pipeline)
def test_reconstructable_module():
    original_sys_path = sys.path
    try:
        sys.path.insert(0, file_relative_path(__file__, "."))
        from foo import bar_pipeline  # pylint: disable=import-error

        reconstructable(bar_pipeline)

    finally:
        sys.path = original_sys_path
def test_inner_scope():
    def get_the_pipeline_inner():
        return the_pipeline

    with pytest.raises(
            DagsterInvariantViolationError,
            match=
            "Use a function or decorated function defined at module scope",
    ):
        reconstructable(get_the_pipeline_inner)
def test_inner_decorator():
    @pipeline
    def pipe():
        the_solid()

    with pytest.raises(
            DagsterInvariantViolationError,
            match=
            "Use a function or decorated function defined at module scope",
    ):
        reconstructable(pipe)
def test_solid_selection():
    recon_pipe = reconstructable(get_the_pipeline)
    sub_pipe_full = recon_pipe.subset_for_execution(["the_solid"])
    assert sub_pipe_full.solids_to_execute == {"the_solid"}

    sub_pipe_unresolved = recon_pipe.subset_for_execution(["the_solid+"])
    assert sub_pipe_unresolved.solids_to_execute == {"the_solid"}
def test_step_handler_context():
    recon_pipeline = reconstructable(foo_pipline)
    with instance_for_test() as instance:
        run = create_run_for_test(instance)

        args = ExecuteStepArgs(
            pipeline_origin=recon_pipeline.get_python_origin(),
            pipeline_run_id=run.run_id,
            step_keys_to_execute=run.step_keys_to_execute,
            instance_ref=None,
        )
        ctx = StepHandlerContext(
            instance=instance,
            execute_step_args=args,
            step_tags={},
            pipeline_run=run,
        )

        assert ctx.execute_step_args == args
        assert ctx.pipeline_run == run
def test_args_fails():
    with pytest.raises(
            DagsterInvariantViolationError,
            match="Reconstructable target must be callable with no arguments",
    ):
        reconstructable(get_with_args)
def test_lambda():
    with pytest.raises(DagsterInvariantViolationError,
                       match="Reconstructable target can not be a lambda"):
        reconstructable(lambda_version)
def test_decorator():
    recon_pipe = reconstructable(the_pipeline)
    assert pid(recon_pipe.get_definition()) == pid(the_pipeline)
def test_function():
    recon_pipe = reconstructable(get_the_pipeline)
    assert pid(recon_pipe.get_definition()) == pid(the_pipeline)