I've written a unit test for a sensor that require...
# ask-community
p
I've written a unit test for a sensor that requires a resource (more or less) like so:
Copy code
def test_the_sensor():
  ctx = build_sensor_context(resources={"my_api": mock_api})
  my_sensor(ctx)
with
my_sensor
defined like so
Copy code
@sensor(required_resource_keys={"my_api"})
def my_sensor(ctx):
  my_api = ctx.resources.my_api
  ...
Running
pytest
results in this:
Copy code
TypeError: my_sensor() got an unexpected keyword argument 'my_api'
If I add
my_api: SomeApi
to
my_sensor
arguments, the test passes, but then at runtime in the real dagster instance, I get this:
Copy code
TypeError: my_sensor() missing 1 required positional argument: 'my_api'
I'm running 1.13.13, is this a known issue or am I doing something wrong?
🤖 1
y
Copy code
from dagster import sensor, build_sensor_context, ConfigurableResource


class MyResource(ConfigurableResource):
    value: str


@sensor()
def my_sensor(my_api: MyResource):
    print(my_api.value)


def test_the_sensor():
    ctx = build_sensor_context(resources={"my_api": MyResource(value="foo")})
    my_sensor(ctx)
this should work
p
Right, when I make my sensor function look like this:
Copy code
def my_sensor(ctx, my_api: MyResource):
  pass
the test works, but in the real runtime, I get this:
Copy code
TypeError: my_sensor() missing 1 required positional argument: 'my_api'
y
ah got it. let me try repro
🙏 1
Copy code
from dagster import (
    sensor,
    build_sensor_context,
    ConfigurableResource,
    asset,
    Definitions,
    AssetSelection,
)


class MyResource(ConfigurableResource):
    value: str


@asset
def my_asset():
    return


@sensor(asset_selection=AssetSelection.all())
def my_sensor(context, my_api: MyResource):
    print(my_api.value)


def test_the_sensor():
    ctx = build_sensor_context(resources={"my_api": MyResource(value="foo")})
    my_sensor(ctx)


defs = Definitions(
    assets=[my_asset], sensors=[my_sensor], resources={"my_api": MyResource(value="foo")}
)
this is the full code snippet that i tested and it works in a real dagster instance
p
Ok, presumably I made a mistake somewhere. I'll dig around to see if I can find what I did. Thanks for looking into this. On a related note, this made my sensor fail every single tick which is only visible in the UI. Is there an existing mechanism to allow detecting sensors that fail repeatedly like this (and potentially do something like send a slack message or whatever)?
c
in cloud, there’s alerting based on schedule / sensor state change (IE, when your sensor starts failing, you’ll get an alert, but won’t get spammed with alerts for every single failed sensor tick)
p
Got it, yeah that makes sense. Is this feature based on things we can build from the OSS version?
c
In this particular instance, I don’t think there’s an analogue functionality in OSS - you could maybe ingest the logs from sensors / schedules and build out some alerting on that
👍 1