Skip to content

How do I mock an asset's value when using materialize? #20611

Answered by sryza
sryza asked this question in Q&A
Discussion options

You must be logged in to vote

One way to do this is with a function that generates a source asset with an IO manager that returns the value you want to mock in.

from dagster import IOManager, SourceAsset

def mock_asset(asset_key, value):
    class MockIOManager(IOManager):
        def load_input(self, context):
            return value

        def handle_output(self, context, obj):
            raise NotImplementedError()

    return SourceAsset(key=asset_key, io_manager_def=MockIOManager())

Example usage:

from dagster import asset, materialize

@asset
def asset2(asset1):
    return asset1 + 1


result = materialize(assets=[asset2, mock_asset("asset1", 5)])

assert result.asset_value(asset2.key) == 6

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@AlanBENlabs
Comment options

@sryza
Comment options

Answer selected by sryza
Comment options

You must be logged in to vote
1 reply
@sryza
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
area: asset Related to Software-Defined Assets
3 participants