holmuk commented on code in PR #65127:
URL: https://github.com/apache/airflow/pull/65127#discussion_r3072563308


##########
airflow-core/src/airflow/settings.py:
##########
@@ -434,6 +434,12 @@ def configure_orm(disable_connection_pool=False, 
pool_class=None):
         # Skip DB initialization in unit tests, if DB tests are skipped
         Session = SkipDBTestsSession
         engine = None
+
+        # Ensure all models are imported so SQLAlchemy can resolve 
string-based relationships in tests.
+        from airflow.models import import_all_models
+
+        import_all_models()

Review Comment:
   Importing `airflow.model` or calling any ORM-related code is not expected in 
non-DB tests. Anyway, I noticed the following:
   - Global `TI` object only used in `test_serialize_deserialize` as an 
argument in one of test suites. 
   - `TI_WITH_START_DAY` and `DAG_RUN` are unused.
   
   I think the better compromise is:
   - Remove `TI`, `TI_WITH_START_DAY` and `DAG_RUN` from the code so no global 
objects are used by tests.
   - In `test_serialize_deserialize` arguments, replace `ti=TI` with 
`ti=make_callback_ti()`, where `make_callback_ti()` creates a datamodel object:
   
   ```python
   from airflow.api_fastapi.execution_api.datamodels import taskinstance as 
ti_datamodel
   
   def make_callback_ti() -> ti_datamodel.TaskInstance:
       return ti_datamodel.TaskInstance(
           id=uuid7(),
           task_id="test-task",
           dag_id="test-dag",
           run_id="fake_run",
           try_number=1,
           dag_version_id=uuid7(),
           map_index=-1,
       )
   ```
   
   This way we keep the code architecturally clean and without explicit ORM 
where ORM is not expected.



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