Repository: apache/airflow

Issue:
airflow task test failing due to mini scheduler implementation not respecting test mode
**Apache Airflow version**: 2.0.0b3

**Environment**: Python3.7-slim running on docker

**What happened**:

Error when running `airflow tasks test <dag> <task> <date>` there is an error with the task rescheduler, which should not happen as we are testing the tasks and not running via scheduler. 

**What you expected to happen**:

No error message should be displayed as the task is success and it is running on test mode

**How to reproduce it**:

just run the `airflow test <dag> <task> <date>` after a vanilla airflow installation using pip install.

**Anything else we need to know**:
this is the log
```
root@add8b3f038cf:/# airflow  tasks test docker_test d 2020-11-24 
[2020-11-24 08:13:00,796] {dagbag.py:440} INFO - Filling up the DagBag from /opt/airflow/dags/test
[2020-11-24 08:13:01,072] {taskinstance.py:827} INFO - Dependencies all met for <TaskInstance: docker_test.d 2020-11-24T00:00:00+00:00 [None]>
[2020-11-24 08:13:01,077] {taskinstance.py:827} INFO - Dependencies all met for <TaskInstance: docker_test.d 2020-11-24T00:00:00+00:00 [None]>
[2020-11-24 08:13:01,077] {taskinstance.py:1018} INFO - 
--------------------------------------------------------------------------------
[2020-11-24 08:13:01,077] {taskinstance.py:1019} INFO - Starting attempt 1 of 4
[2020-11-24 08:13:01,077] {taskinstance.py:1020} INFO - 
--------------------------------------------------------------------------------
[2020-11-24 08:13:01,078] {taskinstance.py:1039} INFO - Executing <Task(PythonOperator): d> on 2020-11-24T00:00:00+00:00
[2020-11-24 08:13:01,109] {taskinstance.py:1232} INFO - Exporting the following env vars:
AIRFLOW_CTX_DAG_EMAIL=adilson@zookal.com
AIRFLOW_CTX_DAG_OWNER=airflow
AIRFLOW_CTX_DAG_ID=docker_test
AIRFLOW_CTX_TASK_ID=d
AIRFLOW_CTX_EXECUTION_DATE=2020-11-24T00:00:00+00:00
d
[2020-11-24 08:13:01,110] {python.py:118} INFO - Done. Returned value was: None
[2020-11-24 08:13:01,115] {taskinstance.py:1143} INFO - Marking task as SUCCESS. dag_id=docker_test, task_id=d, execution_date=20201124T000000, start_date=20201124T081301, end_date=20201124T081301
Traceback (most recent call last):
  File "/usr/local/bin/airflow", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.7/site-packages/airflow/__main__.py", line 40, in main
    args.func(args)
  File "/usr/local/lib/python3.7/site-packages/airflow/cli/cli_parser.py", line 50, in command
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/airflow/utils/cli.py", line 86, in wrapper
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/airflow/cli/commands/task_command.py", line 379, in task_test
    ti.run(ignore_task_deps=True, ignore_ti_state=True, test_mode=True)
  File "/usr/local/lib/python3.7/site-packages/airflow/utils/session.py", line 63, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/airflow/models/taskinstance.py", line 1350, in run
    mark_success=mark_success, test_mode=test_mode, job_id=job_id, pool=pool, session=session
  File "/usr/local/lib/python3.7/site-packages/airflow/utils/session.py", line 59, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/airflow/models/taskinstance.py", line 1152, in _run_raw_task
    self._run_mini_scheduler_on_child_tasks(session)
  File "/usr/local/lib/python3.7/site-packages/airflow/utils/session.py", line 59, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/airflow/models/taskinstance.py", line 1165, in _run_mini_scheduler_on_child_tasks
    execution_date=self.execution_date,
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 3473, in one
    raise orm_exc.NoResultFound("No row was found for one()")
sqlalchemy.orm.exc.NoResultFound: No row was found for one()
```




Discussion:
Thanks for opening your first issue here! Be sure to follow the issue template!

@ashb this issue is the same as https://github.com/apache/airflow/issues/12584 raised by @nathadfield, issues created 1 minute apart.
@AdilsonMendonca Sounds like you've done a better job of explaining it than I. We can close mine. 

Resolving patch:
diff --git a/airflow/models/taskinstance.py b/airflow/models/taskinstance.py
--- a/airflow/models/taskinstance.py
+++ b/airflow/models/taskinstance.py
@@ -1149,7 +1149,8 @@ def _run_raw_task(
 
         session.commit()
 
-        self._run_mini_scheduler_on_child_tasks(session)
+        if not test_mode:
+            self._run_mini_scheduler_on_child_tasks(session)
 
     @provide_session
     @Sentry.enrich_errors


Test patch:
diff --git a/tests/cli/commands/test_task_command.py b/tests/cli/commands/test_task_command.py
--- a/tests/cli/commands/test_task_command.py
+++ b/tests/cli/commands/test_task_command.py
@@ -69,7 +69,8 @@ def test_cli_list_tasks(self):
         args = self.parser.parse_args(['tasks', 'list', 'example_bash_operator', '--tree'])
         task_command.task_list(args)
 
-    def test_test(self):
+    @mock.patch("airflow.models.taskinstance.TaskInstance._run_mini_scheduler_on_child_tasks")
+    def test_test(self, mock_run_mini_scheduler):
         """Test the `airflow test` command"""
         args = self.parser.parse_args(
             ["tasks", "test", "example_python_operator", 'print_the_context', '2018-01-01']
@@ -77,6 +78,8 @@ def test_test(self):
 
         with redirect_stdout(io.StringIO()) as stdout:
             task_command.task_test(args)
+
+        mock_run_mini_scheduler.assert_not_called()
         # Check that prints, and log messages, are shown
         self.assertIn("'example_python_operator__print_the_context__20180101'", stdout.getvalue())