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


##########
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:
   Would it be a good idea to also clean after the provider tests? I feel like 
it’s generally a good principle for one thing to clean up one’s own stuffs. We 
could do
   
   1. Clean when entering the `providers` directory.
   2. Clean after each provider is finished.



-- 
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-unsubscr...@airflow.apache.org

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

Reply via email to