https://dagster.io/ logo
Title
y

Yang

11/07/2022, 4:55 PM
hi! I'd like to mock some ops inside a graph that I'm testing. I tried this
@op
def mock_extract_vdata(metric):
    data = pd.DataFrame({
        "orgpermid": [0, 1, 2],
        "rawdata0": [1, 1.2, 2.2]
    })
    return data


def test_graph():
    with (
        patch("esg.assets.pai.pai_graph.extract_valid_data",
                new=mock_extract_vdata)):
        result = pai_graph.pai_metrics.execute_in_process(
            input_values={
                "fiscal_year": 2020,
                "allfindata": allfindata},
            resources={
                "esg_outputter": mock_esg_outputter.configured({})})
but it still called the real op.
hello, still would be great to be able to do this, thanks!
r

rex

11/09/2022, 5:09 PM
@chris
c

chris

11/10/2022, 12:08 AM
hmmm interesting
I think that this is a result of the fact that the graph is constructed at module initialization - a potential workaround could be mocking the
compute_fn
of the op like so:
@op
def mock_extract_vdata(metric):
    data = pd.DataFrame({
        "orgpermid": [0, 1, 2],
        "rawdata0": [1, 1.2, 2.2]
    })
    return data

def test_graph():
    with (
        patch("esg.assets.pai.pai_graph.extract_valid_data.compute_fn",
                new=mock_extract_vdata.compute_fn)):
        result = pai_graph.pai_metrics.execute_in_process(
            input_values={
                "fiscal_year": 2020,
                "allfindata": allfindata},
            resources={
                "esg_outputter": mock_esg_outputter.configured({})})
Might have the mocking syntax a bit messed up there, but something like that should work I think
y

Yang

11/10/2022, 7:19 AM
I tried a couple variations, but I always got this error
can't set attribute
elif autospec is not None:
            # spec is ignored, new *must* be default, spec_set is treated
            # as a boolean. Should we check spec is not None and that spec_set
            # is a bool?
            if new is not DEFAULT:
                raise TypeError(
                    "autospec creates the mock for you. Can't specify "
                    "autospec and new."
                )
            if original is DEFAULT:
                raise TypeError("Can't use 'autospec' with create=True")
            spec_set = bool(spec_set)
            if autospec is True:
                autospec = original

            new = create_autospec(autospec, spec_set=spec_set,
                                  _name=self.attribute, **kwargs)
        elif kwargs:
            # can't set keyword args when we aren't creating the mock
            # XXXX If new is a Mock we could call new.configure_mock(**kwargs)
            raise TypeError("Can't pass kwargs to a mock we aren't creating")

        new_attr = new

        self.temp_original = original
        self.is_local = local
        self._exit_stack = contextlib.ExitStack()
        try:
>           setattr(self.target, self.attribute, new_attr)
E           AttributeError: can't set attribute

/opt/homebrew/Caskroom/miniforge/base/envs/py39_env_x86/lib/python3.9/site-packages/mock/mock.py:1511: AttributeError
c

chris

11/11/2022, 1:30 AM
hm gotcha. I'll try and spend some time investigating tomorrow
y

Yang

11/11/2022, 4:03 PM
oh ok great thanks!
hey @chris, sorry to bug you, but it would still be great to be able to do this! thanks
c

chris

11/17/2022, 8:29 PM
😅 slipped my mind Yang, been putting out a fire but will get to it I promise
:rainbow-daggy: 1