Skip to content

Commit

Permalink
fix WWW tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hussein-awala committed Feb 1, 2023
1 parent 2beb9fe commit c6c8faa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
2 changes: 1 addition & 1 deletion airflow/www/api/experimental/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def trigger_dag(dag_id):
replace_microseconds = to_boolean(data["replace_microseconds"])

try:
dr = trigger.trigger_dag(dag_id, run_id, params, execution_date, replace_microseconds)
dr = trigger.trigger_dag(dag_id, run_id, None, params, execution_date, replace_microseconds)
except AirflowException as err:
log.error(err)
response = jsonify(error=f"{err}")
Expand Down
6 changes: 3 additions & 3 deletions tests/www/api/experimental/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,18 @@ def test_trigger_dag(self):
)
assert 404 == response.status_code

# Test error for bad conf data
# Test error for bad params data
response = self.client.post(
url_template.format("example_bash_operator"),
data=json.dumps({"conf": "This is a string not a dict"}),
data=json.dumps({"params": "This is a string not a dict"}),
content_type="application/json",
)
assert 400 == response.status_code

# Test OK case
response = self.client.post(
url_template.format("example_bash_operator"),
data=json.dumps({"run_id": run_id, "conf": {"param": "value"}}),
data=json.dumps({"run_id": run_id}),
content_type="application/json",
)
self.assert_deprecated(response)
Expand Down
8 changes: 4 additions & 4 deletions tests/www/views/test_views_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ def test_one_run(admin_client, dag_with_runs: list[DagRun], session):
assert resp.json == {
"dag_runs": [
{
"conf": None,
"conf_is_json": False,
"params": None,
"params_is_json": False,
"data_interval_end": "2016-01-02T00:00:00+00:00",
"data_interval_start": "2016-01-01T00:00:00+00:00",
"end_date": timezone.utcnow().isoformat(),
Expand All @@ -210,8 +210,8 @@ def test_one_run(admin_client, dag_with_runs: list[DagRun], session):
"state": "success",
},
{
"conf": None,
"conf_is_json": False,
"params": None,
"params_is_json": False,
"data_interval_end": "2016-01-03T00:00:00+00:00",
"data_interval_start": "2016-01-02T00:00:00+00:00",
"end_date": None,
Expand Down
53 changes: 27 additions & 26 deletions tests/www/views/test_views_trigger_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,35 +70,38 @@ def test_duplicate_run_id(admin_client):
check_content_in_response(f"The run ID {run_id} already exists", response)


def test_trigger_dag_conf(admin_client):
test_dag_id = "example_bash_operator"
conf_dict = {"string": "Hello, World!"}
def test_trigger_dag_prams(admin_client, dag_maker, monkeypatch, session):
test_dag_id = "params_dag"
params_dict = {"string": "Hello, World!"}

admin_client.post(f"trigger?dag_id={test_dag_id}", data={"conf": json.dumps(conf_dict)})
param = Param("default", type="string")
with monkeypatch.context():
with dag_maker(dag_id=test_dag_id, serialized=True, session=session, params={"string": param}):
EmptyOperator(task_id="task1")

with create_session() as session:
run = session.query(DagRun).filter(DagRun.dag_id == test_dag_id).first()
admin_client.post(f"trigger?dag_id={test_dag_id}", data={"params": json.dumps(params_dict)})
run = session.query(DagRun).filter(DagRun.dag_id == test_dag_id).first()
assert run is not None
assert DagRunType.MANUAL in run.run_id
assert run.run_type == DagRunType.MANUAL
assert run.conf == conf_dict
assert run.params == params_dict


def test_trigger_dag_conf_malformed(admin_client):
def test_trigger_dag_params_malformed(admin_client):
test_dag_id = "example_bash_operator"

response = admin_client.post(f"trigger?dag_id={test_dag_id}", data={"conf": '{"a": "b"'})
response = admin_client.post(f"trigger?dag_id={test_dag_id}", data={"params": '{"a": "b"'})
check_content_in_response("Invalid JSON configuration", response)

with create_session() as session:
run = session.query(DagRun).filter(DagRun.dag_id == test_dag_id).first()
assert run is None


def test_trigger_dag_conf_not_dict(admin_client):
def test_trigger_dag_params_not_dict(admin_client):
test_dag_id = "example_bash_operator"

response = admin_client.post(f"trigger?dag_id={test_dag_id}", data={"conf": "string and not a dict"})
response = admin_client.post(f"trigger?dag_id={test_dag_id}", data={"params": "string and not a dict"})
check_content_in_response("must be a dict", response)

with create_session() as session:
Expand Down Expand Up @@ -169,34 +172,32 @@ def test_trigger_dag_form_origin_url(admin_client, test_origin, expected_origin)


@pytest.mark.parametrize(
"request_conf, expected_conf",
"request_params, expected_params",
[
(None, {"example_key": "example_value"}),
({"other": "test_data", "key": 12}, {"other": "test_data", "key": 12}),
],
)
def test_trigger_dag_params_conf(admin_client, request_conf, expected_conf):
def test_trigger_dag_params(admin_client, request_params, expected_params):
"""
Test that textarea in Trigger DAG UI is pre-populated
with json config when the conf URL parameter is passed,
or if a params dict is passed in the DAG
1. Conf is not included in URL parameters -> DAG.conf is in textarea
2. Conf is passed as a URL parameter -> passed conf json is in textarea
with json params when the prams URL parameter is passed
1. Params is not included in URL parameters -> DAG.params is in textarea
2. Params is passed as a URL parameter -> passed params json is in textarea
"""
test_dag_id = "example_bash_operator"
doc_md = "Example Bash Operator"

if not request_conf:
if not request_params:
resp = admin_client.get(f"trigger?dag_id={test_dag_id}")
else:
test_request_conf = json.dumps(request_conf, indent=4)
resp = admin_client.get(f"trigger?dag_id={test_dag_id}&conf={test_request_conf}&doc_md={doc_md}")
test_request_params = json.dumps(request_params, indent=4)
resp = admin_client.get(f"trigger?dag_id={test_dag_id}&params={test_request_params}&doc_md={doc_md}")

expected_dag_conf = json.dumps(expected_conf, indent=4).replace('"', """)
expected_dag_params = json.dumps(expected_params, indent=4).replace('"', """)

check_content_in_response(
f'<textarea class="form-control" name="conf" id="json">{expected_dag_conf}</textarea>',
f'<textarea class="form-control" name="params" id="json">{expected_dag_params}</textarea>',
resp,
)

Expand All @@ -207,8 +208,8 @@ def test_trigger_dag_params_render(admin_client, dag_maker, session, app, monkey
with param value set in DAG.
"""
account = {"name": "account_name_1", "country": "usa"}
expected_conf = {"accounts": [account]}
expected_dag_conf = json.dumps(expected_conf, indent=4).replace('"', "&#34;")
expected_params = {"accounts": [account]}
expected_dag_params = json.dumps(expected_params, indent=4).replace('"', "&#34;")
DAG_ID = "params_dag"
param = Param(
[account],
Expand All @@ -231,7 +232,7 @@ def test_trigger_dag_params_render(admin_client, dag_maker, session, app, monkey
resp = admin_client.get(f"trigger?dag_id={DAG_ID}")

check_content_in_response(
f'<textarea class="form-control" name="conf" id="json">{expected_dag_conf}</textarea>', resp
f'<textarea class="form-control" name="params" id="json">{expected_dag_params}</textarea>', resp
)


Expand Down

0 comments on commit c6c8faa

Please sign in to comment.