Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions tests/test_project_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -49,3 +84,53 @@ 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):
"""
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
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(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)
if new_test_files:
new_files_text = '\n'.join(new_test_files)
self.fail(
"You've added a test file currently listed as missing:\n"
f"{new_files_text}"
"\n"
"Thank you very much.\n"
"Can you remove it from the list of missing tests, please?"
)