def test_trigger_dag(self): url_template = '/api/experimental/dags/{}/dag_runs' run_id = 'my_run' + utcnow().isoformat() response = self.client.post( url_template.format('example_bash_operator'), data=json.dumps({'run_id': run_id}), content_type="application/json", ) self.assert_deprecated(response) assert 200 == response.status_code response_execution_date = parse_datetime( json.loads(response.data.decode('utf-8'))['execution_date']) assert 0 == response_execution_date.microsecond # Check execution_date is correct response = json.loads(response.data.decode('utf-8')) dagbag = DagBag() dag = dagbag.get_dag('example_bash_operator') dag_run = dag.get_dagrun(response_execution_date) dag_run_id = dag_run.run_id assert run_id == dag_run_id assert dag_run_id == response['run_id'] # Test error for nonexistent dag response = self.client.post(url_template.format('does_not_exist_dag'), data=json.dumps({}), content_type="application/json") assert 404 == response.status_code
def test_trigger_dag(self): with conf_vars( {("core", "store_serialized_dags"): self.dag_serialization} ): url_template = '/api/experimental/dags/{}/dag_runs' run_id = 'my_run' + utcnow().isoformat() response = self.client.post( url_template.format('example_bash_operator'), data=json.dumps({'run_id': run_id}), content_type="application/json" ) self.assertEqual(200, response.status_code) response_execution_date = parse_datetime( json.loads(response.data.decode('utf-8'))['execution_date']) self.assertEqual(0, response_execution_date.microsecond) # Check execution_date is correct response = json.loads(response.data.decode('utf-8')) dagbag = DagBag() dag = dagbag.get_dag('example_bash_operator') dag_run = dag.get_dagrun(response_execution_date) dag_run_id = dag_run.run_id self.assertEqual(run_id, dag_run_id) self.assertEqual(dag_run_id, response['run_id']) # Test error for nonexistent dag response = self.client.post( url_template.format('does_not_exist_dag'), data=json.dumps({}), content_type="application/json" ) self.assertEqual(404, response.status_code)
def test_trigger_dag_for_date(self): with conf_vars({ ("core", "store_serialized_dags"): self.dag_serialzation }): url_template = '/api/experimental/dags/{}/dag_runs' dag_id = 'example_bash_operator' execution_date = utcnow() + timedelta(hours=1) datetime_string = execution_date.isoformat() # Test correct execution with execution date response = self.client.post( url_template.format(dag_id), data=json.dumps({'execution_date': datetime_string}), content_type="application/json") self.assertEqual(200, response.status_code) self.assertEqual( datetime_string, json.loads(response.data.decode('utf-8'))['execution_date']) dagbag = DagBag() dag = dagbag.get_dag(dag_id) dag_run = dag.get_dagrun(execution_date) self.assertTrue( dag_run, 'Dag Run not found for execution date {}'.format( execution_date)) # Test correct execution with execution date and microseconds replaced response = self.client.post(url_template.format(dag_id), data=json.dumps({ 'execution_date': datetime_string, 'replace_microseconds': 'true' }), content_type="application/json") self.assertEqual(200, response.status_code) response_execution_date = parse_datetime( json.loads(response.data.decode('utf-8'))['execution_date']) self.assertEqual(0, response_execution_date.microsecond) dagbag = DagBag() dag = dagbag.get_dag(dag_id) dag_run = dag.get_dagrun(response_execution_date) self.assertTrue( dag_run, 'Dag Run not found for execution date {}'.format( execution_date)) # Test error for nonexistent dag response = self.client.post( url_template.format('does_not_exist_dag'), data=json.dumps({'execution_date': datetime_string}), content_type="application/json") self.assertEqual(404, response.status_code) # Test error for bad datetime format response = self.client.post( url_template.format(dag_id), data=json.dumps({'execution_date': 'not_a_datetime'}), content_type="application/json") self.assertEqual(400, response.status_code)
def test_trigger_dag_for_date(self): url_template = '/api/experimental/dags/{}/dag_runs' dag_id = 'example_bash_operator' execution_date = utcnow() + timedelta(hours=1) datetime_string = execution_date.isoformat() # Test correct execution with execution date response = self.client.post( url_template.format(dag_id), data=json.dumps({'execution_date': datetime_string}), content_type="application/json", ) self.assert_deprecated(response) assert 200 == response.status_code assert datetime_string == json.loads( response.data.decode('utf-8'))['execution_date'] dagbag = DagBag() dag = dagbag.get_dag(dag_id) dag_run = dag.get_dagrun(execution_date) assert dag_run, f'Dag Run not found for execution date {execution_date}' # Test correct execution with execution date and microseconds replaced response = self.client.post( url_template.format(dag_id), data=json.dumps({ 'execution_date': datetime_string, 'replace_microseconds': 'true' }), content_type="application/json", ) assert 200 == response.status_code response_execution_date = parse_datetime( json.loads(response.data.decode('utf-8'))['execution_date']) assert 0 == response_execution_date.microsecond dagbag = DagBag() dag = dagbag.get_dag(dag_id) dag_run = dag.get_dagrun(response_execution_date) assert dag_run, f'Dag Run not found for execution date {execution_date}' # Test error for nonexistent dag response = self.client.post( url_template.format('does_not_exist_dag'), data=json.dumps({'execution_date': datetime_string}), content_type="application/json", ) assert 404 == response.status_code # Test error for bad datetime format response = self.client.post( url_template.format(dag_id), data=json.dumps({'execution_date': 'not_a_datetime'}), content_type="application/json", ) assert 400 == response.status_code
def test_trigger_dag(self): url_template = '/api/experimental/dags/{}/dag_runs' run_id = 'my_run' + utcnow().isoformat() response = self.client.post( url_template.format('example_bash_operator'), data=json.dumps({'run_id': run_id}), content_type="application/json") self.assertEqual(200, response.status_code) # Check execution_date is correct response = json.loads(response.data.decode('utf-8')) dagbag = DagBag() dag = dagbag.get_dag('example_bash_operator') dag_run = dag.get_dagrun(parse_datetime(response['execution_date'])) self.assertEqual(run_id, dag_run.run_id) response = self.client.post(url_template.format('does_not_exist_dag'), data=json.dumps({}), content_type="application/json") self.assertEqual(404, response.status_code)