You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2022/12/29 10:20:52 UTC

[GitHub] [airflow] Taragolis commented on a diff in pull request #28631: Clear DB before individual module tests

Taragolis commented on code in PR #28631:
URL: https://github.com/apache/airflow/pull/28631#discussion_r1058866477


##########
tests/providers/conftest.py:
##########
@@ -0,0 +1,89 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from __future__ import annotations
+
+from functools import lru_cache
+from pathlib import Path
+
+import pytest
+
+from tests.test_utils import db
+
+_CLEAR_DB_PROVIDERS = set()
+
+
+@lru_cache(maxsize=None)
+def providers_packages():
+    """Get providers packages full qualname."""
+
+    current_dir = Path(__file__).absolute().parent
+    providers = set()
+    for root in current_dir.iterdir():
+        if not root.is_dir():
+            continue
+
+        providers_dirs = set()
+        for sentinel in {"hooks", "operators", "sensors"}:
+            providers_dirs = providers_dirs.union({p.parent for p in root.rglob(sentinel) if p.is_dir()})
+
+        if providers_dirs:
+            for d in providers_dirs:
+                providers.add(".".join(d.relative_to(current_dir).parts))
+        else:
+            providers.add(root.name)
+
+    return providers
+
+
+def get_test_provider_name(m):
+    """Extract provider name from module full qualname."""
+    _, _, name = m.__name__.partition("providers.")
+    for provider in providers_packages():
+        if name.startswith(provider):
+            return provider
+    return None
+
+
+@pytest.fixture(scope="module", autouse=True)
+def _clear_db_between_providers_tests(request):
+    """Clear DB between each separate provider package test runs."""
+    provider_name = get_test_provider_name(request.module)
+    if provider_name and provider_name not in _CLEAR_DB_PROVIDERS:
+        _CLEAR_DB_PROVIDERS.add(provider_name)
+        db.clear_db_runs()
+        db.clear_db_datasets()
+        db.clear_db_dags()
+        db.clear_db_serialized_dags()
+        db.clear_db_sla_miss()
+        db.clear_db_pools()
+        db.clear_db_connections()
+        db.clear_db_variables()
+        db.clear_db_dag_code()
+        db.clear_db_callbacks()
+        db.clear_rendered_ti_fields()
+        db.clear_db_import_errors()
+        db.clear_db_dag_warnings()
+        db.clear_db_xcom()
+        db.clear_db_logs()
+        db.clear_db_jobs()
+        db.clear_db_task_fail()
+        db.clear_db_task_reschedule()
+        db.clear_dag_specific_permissions()
+        db.create_default_connections()
+        db.set_default_pool_slots(128)
+    yield

Review Comment:
   I've better keep `--with-db-init` time to time devs still need to clear DB (something changed in structure).
   
   I have for this run configurations, but someone could find `--with-db-init` more useful rather this
   
   ![image](https://user-images.githubusercontent.com/3998685/209937487-8682130a-d1f9-474d-a5ba-5da8ce40b831.png)
   
   



##########
tests/providers/conftest.py:
##########
@@ -0,0 +1,89 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from __future__ import annotations
+
+from functools import lru_cache
+from pathlib import Path
+
+import pytest
+
+from tests.test_utils import db
+
+_CLEAR_DB_PROVIDERS = set()
+
+
+@lru_cache(maxsize=None)
+def providers_packages():
+    """Get providers packages full qualname."""
+
+    current_dir = Path(__file__).absolute().parent
+    providers = set()
+    for root in current_dir.iterdir():
+        if not root.is_dir():
+            continue
+
+        providers_dirs = set()
+        for sentinel in {"hooks", "operators", "sensors"}:
+            providers_dirs = providers_dirs.union({p.parent for p in root.rglob(sentinel) if p.is_dir()})
+
+        if providers_dirs:
+            for d in providers_dirs:
+                providers.add(".".join(d.relative_to(current_dir).parts))
+        else:
+            providers.add(root.name)
+
+    return providers
+
+
+def get_test_provider_name(m):
+    """Extract provider name from module full qualname."""
+    _, _, name = m.__name__.partition("providers.")
+    for provider in providers_packages():
+        if name.startswith(provider):
+            return provider
+    return None
+
+
+@pytest.fixture(scope="module", autouse=True)
+def _clear_db_between_providers_tests(request):
+    """Clear DB between each separate provider package test runs."""
+    provider_name = get_test_provider_name(request.module)
+    if provider_name and provider_name not in _CLEAR_DB_PROVIDERS:
+        _CLEAR_DB_PROVIDERS.add(provider_name)
+        db.clear_db_runs()
+        db.clear_db_datasets()
+        db.clear_db_dags()
+        db.clear_db_serialized_dags()
+        db.clear_db_sla_miss()
+        db.clear_db_pools()
+        db.clear_db_connections()
+        db.clear_db_variables()
+        db.clear_db_dag_code()
+        db.clear_db_callbacks()
+        db.clear_rendered_ti_fields()
+        db.clear_db_import_errors()
+        db.clear_db_dag_warnings()
+        db.clear_db_xcom()
+        db.clear_db_logs()
+        db.clear_db_jobs()
+        db.clear_db_task_fail()
+        db.clear_db_task_reschedule()
+        db.clear_dag_specific_permissions()
+        db.create_default_connections()
+        db.set_default_pool_slots(128)
+    yield

Review Comment:
   I've better keep `--with-db-init` time to time devs still need to clear DB (something changed in structure).
   
   I have for this run configurations, but someone could find `--with-db-init` more useful rather then this
   
   ![image](https://user-images.githubusercontent.com/3998685/209937487-8682130a-d1f9-474d-a5ba-5da8ce40b831.png)
   
   



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org