potiuk commented on PR #41067:
URL: https://github.com/apache/airflow/pull/41067#issuecomment-2263722952

   I pushed a bit better detection mechanism for tested vs. testing code.  
Should cover few more cases I saw as "testing" code
   
   * running dag.create_dagrun() in tests directly
   * running any `utils.db` in tests directly
   
   ```
           if any(filename.endswith("conftest.py") for filename, _, _, _ in 
airflow_frames):
               # This is a fixture call
               return True, None
           if len(airflow_frames) >= 2 and 
airflow_frames[-2].filename.startswith(AIRFLOW_TESTS_PATH):
               # Let's look at what we are calling directly from the test code
               current_filename, current_method_name = 
airflow_frames[-1].filename, airflow_frames[-1].name
               if (current_filename, current_method_name) in (
                   (AIRFLOW_MODELS_BASEOPERATOR_PATH, "run"),
                   (AIRFLOW_MODELS_DAG_PATH, "create_dagrun"),
               ):
                   # This is baseoperator run method that is called directly 
from the test code and this is
                   # usual pattern where we create a session in the test code 
to create dag_runs for tests.
                   # If `run` code will be run inside a real "airflow" code the 
stack trace would be longer
                   # and it would not be directly called from the test code. 
Also if subsequently any of the
                   # run_task() method called later from the task code will 
attempt to execute any DB
                   # method, the stack trace will be longer and we will catch 
it as "illegal" call.
                   return True, None
               if current_filename == AIRFLOW_DB_UTILS_PATH:
                   # This is a util method called directly from the test code
                   return True, None
           for tb in airflow_frames[::-1]:
               if tb.filename.startswith(AIRFLOW_PATH):
                   if tb.filename.startswith(AIRFLOW_TESTS_PATH):
                       # this is a session created directly in the test code
                       return True, None
                   else:
                       return False, tb
   
   
   ```
   


-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to