https://dagster.io/ logo
#ask-community
Title
# ask-community
y

Yang

07/08/2022, 10:04 PM
Hi, is there an example somewhere of using
mock_resource
?
p

prha

07/08/2022, 10:11 PM
Here’s an example from the docs site: https://docs.dagster.io/concepts/testing#testing-graphs
mock_resource
is just a thin wrapper around a
mock.MagicMock()
object
y

Yang

07/08/2022, 10:22 PM
Thanks! Sorry I'm not super good at using MagicMock. Do you happen to know how to mock the return_value of the resource?
p

prha

07/08/2022, 10:25 PM
As opposed to using
ResourceDefinition.mock_resource()
, I think you probably want to wrap your own mocked resource then:
Copy code
my_resource = ResourceDefinition(resource_fn=lambda _context: mock.MagicMock(return_value="hiiii"))
y

Yang

07/08/2022, 10:28 PM
oohh ok I'll try that, thanks!
hm it doesn't work...I think I need to mock patch
Copy code
dagster.core.errors.DagsterTypeCheckDidNotPass: Type check failed for op "leadlag_entities" output "result" - expected type "DataFrame". Description: Value of type <class 'unittest.mock.MagicMock'> failed type check for Dagster type DataFrame, expected value to be of Python type pandas.core.frame.DataFrame.
oh well, I ended up patching my actual resource methods
it would be great to get an example sometime for mocking resources with MagicMock, thanks!!
p

prha

07/09/2022, 12:17 AM
Oh sorry. if you include a snippet of what your resource usage looks like, I can help fill something in.
Maybe something like this:
Copy code
@resource()
def mocked_resource():
     # returns an object that has a method generate_dataframe which returns a DataFrame
     foo = mock.MagicMock()
     foo.generate_dataframe.return_value = DataFrame()
     return foo

@op(required_resource_key={"df_generator"})
def my_op(context):
    df = context.resources.df_generator.generate_dataframe()
    ...
    return df

@job(resource_defs={"df_generator": mocked_resource})
def my_job():
    my_op()
y

Yang

07/11/2022, 4:40 PM
Oh I think I see. In my asset, I call this resource method
Copy code
context.resources.secmaster_db.get_entity_universe()
ok thanks!! that's what I didn't get.
dagsir 1
40 Views