Hi, I am trying to test an ops that access the ru...
# ask-community
t
Hi, I am trying to test an ops that access the run_config My job looks like:
Copy code
@op(out={
        'start_datetime': Out(dagster_type=pd.Timestamp),
    })
def calculate_build_times(context: OpExecutionContext):
    job_frequency = context.op_config['job_frequency']
	start_datetime = pd.Timestamp(context.run_config.get('job_start_datetime'), tz='UTC') \
		if context.run_config.get('job_start_datetime') else pd.Timestamp.utcnow().floor('D')
		
	return start_datetime
My test is:
Copy code
def test_calculate_build_times():
    with build_op_context(
            op_config={
                'job_frequency': 'DAILY',
                'job_start_datetime': '2022-12-01',
            }) as context:
        start_datetime = calculate_build_times(context)

    assert start_datetime == pd.Timestamp('2022-12-01', tz='UTC')
The test fails because
context.run_config.get('job_start_datetime')
returns
None
How can I set the
run_config
in the context?
🤖 1
c
Hey Thierry--you're setting config correctly in the
build_op_context
call. In your op, you'll need to replace every
context.run_config
with
context.op_config
.
t
@claire Maybe I wasn't clear enough in exposing the problem, the config
job_start_datetime
can be overridden in the Launchpad UI when I want to run the job manually, hence the use of
run_config
. I'm trying to test different scenario with for my ops based on the configuration passed in the Launchpad. The way I by passed the problem is to have a function
get_run_config
that access the run_config, then I can mock that function to return the type of config I want but I was wondering whether there was a more elegant way to address my problem Do you have any other idea?
c
Hi Thierry. Run config gets mapped down to the specific op, so specifying op config within
build_op_context
and accessing config within your op via
context.op_config
is the recommended pattern. Any run config that is specified via Dagit's launchpad will be applied to the
context.op_config
object.
t
Thanks, I tested it and it didn't work so I must have done something wrong. I will test again tomorrow and let you know.
c
You might need to define a
config_schema
field on your op
t
Thanks, I didn't have that for sure