kaxil edited a comment on issue #8505:
URL: https://github.com/apache/airflow/pull/8505#issuecomment-617470493
> We can create context - `catch_jinja_params()` (similar to
`count_queries`, `contextlib.redirect_stdout(io.StringIO())` ) that will help
us introduce these assertions into current tests. Thanks to this, we will still
have assertions for the HTML code, but also more precise for the Jinja params
only.
>
> ```python
> class TestVersionView(TestBase):
> def test_version(self):
> with catch_jinja_params() as jinja_params:
> resp = self.client.get('version', data=dict(
> username='test',
> password='test'
> ), follow_redirects=True)
> self.check_content_in_response('Version Info', resp)
>
> self.assertEqual(jinja_params.template_name,
'airflow/version.html')
> self.assertEqual(jinja_params.params, dict(
> airflow_version=version.version,
> git_version=mock.ANY,
> scheduler_job=mock.ANY,
> title='Version Info'
> ))
> ```
>
> However, I only wanted to present the general concept in this PR.
I like the idea. Without that we would have to do it like the following
which works too but less ideal:
```python
@mock.patch('airflow.www.views.dagbag.get_dag')
@mock.patch('airflow.www.views.BaseView.render_template')
def test_extra_link_in_gantt_view(self, mock_render_template,
get_dag_function):
from tests.test_utils.mock_operators import Dummy2TestOperator
dag = DAG('ex_dag', start_date=self.default_date)
Dummy2TestOperator(task_id="some_dummy_task_2", dag=dag)
get_dag_function.return_value = dag
exec_date = dates.days_ago(2)
start_date = datetime(2020, 4, 10, 2, 0, 0)
end_date = exec_date + timedelta(seconds=30)
with create_session() as session:
for task in dag.tasks:
ti = TaskInstance(task=task, execution_date=exec_date,
state="success")
ti.start_date = start_date
ti.end_date = end_date
session.add(ti)
with self.app.app_context():
mock_render_template.return_value = make_response("RESPONSE",
200)
url = 'gantt?dag_id={}&execution_date={}'.format(dag.dag_id,
exec_date)
self.client.get(url, follow_redirects=True, data=dict(
username='test',
password='test'
))
self.assertCountEqual(
mock_render_template.call_args[1]['data']['tasks'][0]['extraLinks'],
['airflow', 'github']
)
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]