David de Meij
02/27/2024, 1:25 PMScout
02/27/2024, 1:25 PMbuild_sensor_context
function to create a context for your sensor evaluation during testing. This allows you to simulate the sensor's execution environment. Example from the documentation: python from dagster import build_sensor_context, sensor, RunRequest @sensor(job=my_job) def my_sensor(context): if <some_condition>: yield RunRequest(run_key="unique_key_for_run", run_config={})
In your test, you would build the sensor context and then invoke my_sensor
with that context: python def test_my_sensor(): context = build_sensor_context() result = list(my_sensor(context)) assert <some_condition_about_result>
3. Mock the GraphQL Client: Since you're triggering a job in an external repository, you'll need to mock the GraphQL client's behavior. You can create a mock object that simulates the client and returns expected responses when the sensor attempts to trigger a job via GraphQL. 4. Test the Triggering Logic: With the mocked GraphQL client, you can now test the part of your sensor that is responsible for triggering the external job. Ensure that the correct GraphQL mutation is being called with the expected parameters. 5. Integration Test: If possible, consider writing an integration test that actually triggers the job in a controlled environment. This would involve setting up a test instance of the external repository and ensuring that the sensor correctly triggers the job via the GraphQL API. Remember to clean up any resources and mock objects after your tests to avoid side effects. Please note that the above steps are a general guide based on the principles of testing sensors and using the GraphQL API as described in the documentation. You may need to adapt these steps to fit the specifics of your sensor and the external job it triggers.