From a53a0f5fdf90591c1c4688fb6e8b1638e5c64869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Bregu=C5=82a?= Date: Mon, 3 Feb 2020 00:02:50 +0100 Subject: [PATCH 1/3] [AIRFLOW-6720] Detect missing tests for providers package --- tests/test_project_structure.py | 82 +++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/tests/test_project_structure.py b/tests/test_project_structure.py index 627a3c08d0970..d2efcbddbdfdc 100644 --- a/tests/test_project_structure.py +++ b/tests/test_project_structure.py @@ -24,6 +24,41 @@ os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir) ) +MISSING_TEST_FILES = { + 'tests/providers/amazon/aws/hooks/test_athena.py', + 'tests/providers/amazon/aws/hooks/test_s3.py', + 'tests/providers/apache/cassandra/sensors/test_record.py', + 'tests/providers/apache/cassandra/sensors/test_table.py', + 'tests/providers/apache/hdfs/sensors/test_web_hdfs.py', + 'tests/providers/apache/hive/operators/test_vertica_to_hive.py', + 'tests/providers/apache/hive/sensors/test_hive_partition.py', + 'tests/providers/apache/hive/sensors/test_metastore_partition.py', + 'tests/providers/apache/pig/operators/test_pig.py', + 'tests/providers/apache/spark/hooks/test_spark_jdbc_script.py', + 'tests/providers/cncf/kubernetes/operators/test_kubernetes_pod.py', + 'tests/providers/google/cloud/operators/test_datastore.py', + 'tests/providers/google/cloud/operators/test_gcs_to_bigquery.py', + 'tests/providers/google/cloud/operators/test_sql_to_gcs.py', + 'tests/providers/google/cloud/sensors/test_bigquery.py', + 'tests/providers/google/cloud/utils/test_field_sanitizer.py', + 'tests/providers/google/cloud/utils/test_field_validator.py', + 'tests/providers/google/cloud/utils/test_mlengine_operator_utils.py', + 'tests/providers/google/cloud/utils/test_mlengine_prediction_summary.py', + 'tests/providers/jenkins/hooks/test_jenkins.py', + 'tests/providers/microsoft/azure/sensors/test_azure_cosmos.py', + 'tests/providers/microsoft/mssql/hooks/test_mssql.py', + 'tests/providers/microsoft/mssql/operators/test_mssql.py', + 'tests/providers/oracle/operators/test_oracle.py', + 'tests/providers/presto/operators/test_presto_check.py', + 'tests/providers/qubole/hooks/test_qubole.py', + 'tests/providers/samba/hooks/test_samba.py', + 'tests/providers/snowflake/hooks/test_snowflake.py', + 'tests/providers/snowflake/operators/test_s3_to_snowflake.py', + 'tests/providers/snowflake/operators/test_snowflake.py', + 'tests/providers/sqlite/operators/test_sqlite.py', + 'tests/providers/vertica/hooks/test_vertica.py' +} + class TestProjectStructure(unittest.TestCase): def test_reference_to_providers_from_core(self): @@ -49,3 +84,50 @@ def assert_file_contains(self, filename: str, pattern: str): with open(filename, 'rb', 0) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as content: if content.find(bytes(pattern, 'utf-8')) == -1: self.fail(f"File {filename} contains illegal pattern - {pattern}") + + def test_providers_modules_should_have_tests(self): + # TODO: Should we extend this test to cover other directories? + expected_test_files = glob.glob(f"{ROOT_FOLDER}/airflow/providers/**/*.py", recursive=True) + # Make path relative + expected_test_files = (os.path.relpath(f, ROOT_FOLDER) for f in expected_test_files) + # Exclude example_dags + expected_test_files = (f for f in expected_test_files if "/example_dags/" not in f) + # Exclude __init__.py + expected_test_files = (f for f in expected_test_files if not f.endswith("__init__.py")) + # Change airflow/ to tests/ + expected_test_files = ( + f'tests/{f.partition("/")[2]}' + for f in expected_test_files if not f.endswith("__init__.py") + ) + # Add test_ prefix to filename + expected_test_files = ( + f'{f.rpartition("/")[0]}/test_{f.rpartition("/")[2]}' + for f in expected_test_files if not f.endswith("__init__.py") + ) + + current_test_files = glob.glob(f"{ROOT_FOLDER}/tests/providers/**/*.py", recursive=True) + # Make path relative + current_test_files = (os.path.relpath(f, ROOT_FOLDER) for f in current_test_files) + # Exclude __init__.py + current_test_files = (f for f in current_test_files if not f.endswith("__init__.py")) + + expected_test_files = set(expected_test_files) + current_test_files = set(current_test_files) + + missing_tests_files = expected_test_files - expected_test_files.intersection(current_test_files) + self.assertEqual(missing_tests_files, MISSING_TEST_FILES) + + def test_keep_missing_test_files_update(self): + new_test_files = [] + for test_file in MISSING_TEST_FILES: + if os.path.isfile(f"{ROOT_FOLDER}/{test_file}"): + new_test_files.append(test_file) + new_files_text = '\n'.join(new_test_files) + if new_test_files: + self.fail( + "It looks like you added a missing test files:\n" + f"{new_files_text}" + "\n" + "Thank you very much.\n" + "Can you remove it from the list of missing tests, please?" + ) From 53845c6d623e7d7c7513f4dd1b28e771292b652b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Bregu=C5=82a?= Date: Mon, 3 Feb 2020 08:58:13 +0100 Subject: [PATCH 2/3] fixup! [AIRFLOW-6720] Detect missing tests for providers package --- tests/test_project_structure.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_project_structure.py b/tests/test_project_structure.py index d2efcbddbdfdc..9a40e601b5b7b 100644 --- a/tests/test_project_structure.py +++ b/tests/test_project_structure.py @@ -86,6 +86,9 @@ def assert_file_contains(self, filename: str, pattern: str): self.fail(f"File {filename} contains illegal pattern - {pattern}") def test_providers_modules_should_have_tests(self): + """ + Assert every module in /airflow/providers has a corresponding test_ file in tests/airflow/providers. + """ # TODO: Should we extend this test to cover other directories? expected_test_files = glob.glob(f"{ROOT_FOLDER}/airflow/providers/**/*.py", recursive=True) # Make path relative @@ -115,15 +118,15 @@ def test_providers_modules_should_have_tests(self): current_test_files = set(current_test_files) missing_tests_files = expected_test_files - expected_test_files.intersection(current_test_files) - self.assertEqual(missing_tests_files, MISSING_TEST_FILES) + self.assertEqual(set(), missing_tests_files - MISSING_TEST_FILES) def test_keep_missing_test_files_update(self): new_test_files = [] for test_file in MISSING_TEST_FILES: if os.path.isfile(f"{ROOT_FOLDER}/{test_file}"): new_test_files.append(test_file) - new_files_text = '\n'.join(new_test_files) if new_test_files: + new_files_text = '\n'.join(new_test_files) self.fail( "It looks like you added a missing test files:\n" f"{new_files_text}" From 04cc228242fb95608c21f1f7b65447027f7b0b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Bregu=C5=82a?= Date: Mon, 3 Feb 2020 09:29:00 +0100 Subject: [PATCH 3/3] fixup! fixup! [AIRFLOW-6720] Detect missing tests for providers package --- tests/test_project_structure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_project_structure.py b/tests/test_project_structure.py index 9a40e601b5b7b..5907cf57c09f9 100644 --- a/tests/test_project_structure.py +++ b/tests/test_project_structure.py @@ -128,7 +128,7 @@ def test_keep_missing_test_files_update(self): if new_test_files: new_files_text = '\n'.join(new_test_files) self.fail( - "It looks like you added a missing test files:\n" + "You've added a test file currently listed as missing:\n" f"{new_files_text}" "\n" "Thank you very much.\n"