Hi, is there an example somewhere of using `mock_r...
# ask-community
y
Hi, is there an example somewhere of using
mock_resource
?
p
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
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
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
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
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
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