Alex Berndt
03/06/2024, 9:15 AMScout
03/06/2024, 9:15 AMpython from dagster import resource, asset, Field, StringSource, build_asset_context # Define a resource for the production API client @resource({ "api_key": Field(StringSource, description="API key for the external API") }) def api_client_resource(context): api_key = context.resource_config["api_key"] # Replace 'ExternalAPIClient' with your actual client class return ExternalAPIClient(api_key) # Define a mock resource for testing @resource def mock_api_client_resource(_): # Replace 'MockExternalAPIClient' with your mock client class return MockExternalAPIClient() # Define an asset that uses the API client resource @asset(required_resource_keys={"api_client"}) def my_asset(context): api_client = context.resources.api_client # Use the API client to interact with the external API data = api_client.get_data() return data # Example test that materializes the asset using the mocked resource def test_my_asset(): with build_asset_context(resources={"api_client": mock_api_client_resource}) as context: result = my_asset(context=context) # Perform assertions on 'result'
In this example, ExternalAPIClient
would be a class that interacts with the real external API, and MockExternalAPIClient
would be a class that simulates the API for testing purposes. The api_client_resource
function creates an instance of the real API client using the provided API key, while mock_api_client_resource
creates an instance of the mock client. When running tests, you use build_asset_context
to provide the mock_api_client_resource
to the asset, which allows you to test the asset's logic without needing real API credentials. Please note that the actual implementation of ExternalAPIClient
and MockExternalAPIClient
will depend on the specifics of the external API you're interacting with and how you want to simulate it for testing.Alex Berndt
03/06/2024, 9:17 AMScout
03/06/2024, 9:17 AMpython from pydantic import BaseModel from dagster import asset, build_asset_context from dagster_pydantic import PydanticConfigurableResource # Define a Pydantic model for the API client configuration class APIClientConfig(BaseModel): api_key: str # Define a resource for the production API client using the Pydantic model class APIClientResource(PydanticConfigurableResource[APIClientConfig]): def __init__(self, config: APIClientConfig): # Replace 'ExternalAPIClient' with your actual client class self.client = ExternalAPIClient(config.api_key) # Define a mock resource for testing class MockAPIClientResource(PydanticConfigurableResource): def __init__(self): # Replace 'MockExternalAPIClient' with your mock client class self.client = MockExternalAPIClient() # Define an asset that uses the API client resource @asset(required_resource_keys={"api_client"}) def my_asset(context): api_client = context.resources.api_client.client # Use the API client to interact with the external API data = api_client.get_data() return data # Example test that materializes the asset using the mocked resource def test_my_asset(): with build_asset_context(resources={"api_client": MockAPIClientResource()}) as context: result = my_asset(context=context) # Perform assertions on 'result'
In this updated example, APIClientConfig
is a Pydantic model that defines the configuration schema for the API client resource. APIClientResource
and MockAPIClientResource
are resource classes that inherit from PydanticConfigurableResource
, with APIClientResource
taking an instance of APIClientConfig
as its configuration. When you materialize the asset in your tests, you instantiate MockAPIClientResource
directly without any configuration, since it's a mock that doesn't require real credentials. Please ensure that you have the dagster_pydantic
package installed to use PydanticConfigurableResource
. If it's not already installed, you can add it to your project with pip install dagster_pydantic
. Remember to replace ExternalAPIClient
and MockExternalAPIClient
with the actual implementations that correspond to your production and mock API clients, respectively.