David de Meij
02/27/2024, 4:40 PMresource 'constants' provided on both the definition and invocation context. Please provide on only one or the other.
Test code:
from dagster import build_asset_context
def test_my_asset():
context = build_asset_context(resources={"constants": {"gcs_prefix": "<gs://new_prefix/>"}})
result = my_asset(context)
# Your assertions here
Asset code:
constants = make_values_resource(
gcs_prefix=Field(
str,
default_value="<gs://prefix/>"
description="GCS prefix",
),
)
@asset(
resource_defs={"constants": constants},
…
)
Scout
02/27/2024, 4:40 PMconstants
is being provided in both the asset definition and the invocation context when you're trying to test your asset. In Dagster, a resource should only be provided once, either in the asset definition or in the test context, but not both. Based on the documentation, when you want to test an asset that requires resources, you should specify the resources in the test context without redefining them in the asset definition. Here's how you can adjust your test code to avoid the error: python from dagster import build_asset_context def test_my_asset(): # Define the resource configuration for the test context test_constants_resource = make_values_resource( gcs_prefix=Field( str, default_value="<gs://new_prefix/>", description="GCS prefix for testing", ), ) # Build the asset context with the test resource configuration context = build_asset_context(resources={"constants": test_constants_resource}) # Invoke the asset with the test context result = my_asset(context) # Your assertions here
And your asset code should look like this: python # Define the resource outside of the asset definition constants = make_values_resource( gcs_prefix=Field( str, default_value="<gs://prefix/>", description="GCS prefix", ), ) # Use the resource in the asset definition @asset(resource_defs={"constants": constants}) def my_asset(context): # Asset logic here ...
In the test, you are creating a new instance of the constants
resource with the test-specific configuration and passing it to the build_asset_context
function. This way, the asset will use the test resource configuration when it is invoked in the test, and you won't encounter the error about providing the resource in both the definition and invocation context.