Skip to content
Merged
Show file tree
Hide file tree
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
46 changes: 24 additions & 22 deletions tests/always/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
import json
import os
import re
import unittest
from collections import namedtuple
from unittest import mock

import pytest
import sqlalchemy
from cryptography.fernet import Fernet
from parameterized import parameterized

from airflow import AirflowException
from airflow.hooks.base import BaseHook
Expand Down Expand Up @@ -61,16 +59,15 @@ def uri_test_name(func, num, param):
return f"{func.__name__}_{num}_{param.args[0].description.replace(' ', '_')}"


class TestConnection(unittest.TestCase):
def setUp(self):
class TestConnection:
def setup_method(self):
crypto._fernet = None
patcher = mock.patch("airflow.models.connection.mask_secret", autospec=True)
self.mask_secret = patcher.start()
self.patcher = mock.patch("airflow.models.connection.mask_secret", autospec=True)
self.mask_secret = self.patcher.start()

self.addCleanup(patcher.stop)

def tearDown(self):
def teardown_method(self):
crypto._fernet = None
self.patcher.stop()

@conf_vars({("core", "fernet_key"): ""})
def test_connection_extra_no_encryption(self):
Expand Down Expand Up @@ -350,7 +347,7 @@ def test_connection_extra_with_encryption_rotate_fernet_key(self):
),
]

@parameterized.expand([(x,) for x in test_from_uri_params], UriTestCaseConfig.uri_test_name)
@pytest.mark.parametrize("test_config", [x for x in test_from_uri_params])
def test_connection_from_uri(self, test_config: UriTestCaseConfig):

connection = Connection(uri=test_config.test_uri)
Expand All @@ -372,7 +369,7 @@ def test_connection_from_uri(self, test_config: UriTestCaseConfig):

self.mask_secret.assert_has_calls(expected_calls)

@parameterized.expand([(x,) for x in test_from_uri_params], UriTestCaseConfig.uri_test_name)
@pytest.mark.parametrize("test_config", [x for x in test_from_uri_params])
def test_connection_get_uri_from_uri(self, test_config: UriTestCaseConfig):
"""
This test verifies that when we create a conn_1 from URI, and we generate a URI from that conn, that
Expand All @@ -393,7 +390,7 @@ def test_connection_get_uri_from_uri(self, test_config: UriTestCaseConfig):
assert connection.schema == new_conn.schema
assert connection.extra_dejson == new_conn.extra_dejson

@parameterized.expand([(x,) for x in test_from_uri_params], UriTestCaseConfig.uri_test_name)
@pytest.mark.parametrize("test_config", [x for x in test_from_uri_params])
def test_connection_get_uri_from_conn(self, test_config: UriTestCaseConfig):
"""
This test verifies that if we create conn_1 from attributes (rather than from URI), and we generate a
Expand Down Expand Up @@ -421,7 +418,8 @@ def test_connection_get_uri_from_conn(self, test_config: UriTestCaseConfig):
else:
assert actual_val == expected_val

@parameterized.expand(
@pytest.mark.parametrize(
"uri,uri_parts",
[
(
"http://:password@host:80/database",
Expand Down Expand Up @@ -486,7 +484,7 @@ def test_connection_get_uri_from_conn(self, test_config: UriTestCaseConfig):
schema="",
),
),
]
],
)
def test_connection_from_with_auth_info(self, uri, uri_parts):
connection = Connection(uri=uri)
Expand All @@ -498,46 +496,50 @@ def test_connection_from_with_auth_info(self, uri, uri_parts):
assert connection.port == uri_parts.port
assert connection.schema == uri_parts.schema

@parameterized.expand(
@pytest.mark.parametrize(
"extra,expected",
[
('{"extra": null}', None),
('{"extra": "hi"}', "hi"),
('{"extra": {"yo": "hi"}}', '{"yo": "hi"}'),
('{"extra": "{\\"yo\\": \\"hi\\"}"}', '{"yo": "hi"}'),
]
],
)
def test_from_json_extra(self, extra, expected):
"""json serialization should support extra stored as object _or_ as string"""
assert Connection.from_json(extra).extra == expected

@parameterized.expand(
@pytest.mark.parametrize(
"val,expected",
[
('{"conn_type": "abc-abc"}', "abc_abc"),
('{"conn_type": "abc_abc"}', "abc_abc"),
('{"conn_type": "postgresql"}', "postgres"),
]
],
)
def test_from_json_conn_type(self, val, expected):
"""two conn_type normalizations are applied: replace - with _ and postgresql with postgres"""
assert Connection.from_json(val).conn_type == expected

@parameterized.expand(
@pytest.mark.parametrize(
"val,expected",
[
('{"port": 1}', 1),
('{"port": "1"}', 1),
('{"port": null}', None),
]
],
)
def test_from_json_port(self, val, expected):
"""two conn_type normalizations are applied: replace - with _ and postgresql with postgres"""
assert Connection.from_json(val).port == expected

@parameterized.expand(
@pytest.mark.parametrize(
"val,expected",
[
('pass :/!@#$%^&*(){}"', 'pass :/!@#$%^&*(){}"'), # these are the same
(None, None),
("", None), # this is a consequence of the password getter
]
],
)
def test_from_json_special_characters(self, val, expected):
"""two conn_type normalizations are applied: replace - with _ and postgresql with postgres"""
Expand Down
12 changes: 5 additions & 7 deletions tests/always/test_project_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import itertools
import mmap
import os
import unittest

import pytest

Expand All @@ -30,7 +29,7 @@
)


class TestProjectStructure(unittest.TestCase):
class TestProjectStructure:
def test_reference_to_providers_from_core(self):
for filename in glob.glob(f"{ROOT_FOLDER}/example_dags/**/*.py", recursive=True):
self.assert_file_not_contains(filename, "providers")
Expand All @@ -47,12 +46,12 @@ def test_deprecated_packages(self):
def assert_file_not_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} not contains pattern - {pattern}")
pytest.fail(f"File {filename} not contains pattern - {pattern}")

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}")
pytest.fail(f"File {filename} contains illegal pattern - {pattern}")

def test_providers_modules_should_have_tests(self):
"""
Expand Down Expand Up @@ -90,8 +89,7 @@ def test_providers_modules_should_have_tests(self):

missing_tests_files = expected_test_files - expected_test_files.intersection(current_test_files)

with self.subTest("Detect missing tests in providers module"):
assert set() == missing_tests_files
assert set() == missing_tests_files, "Detect missing tests in providers module"


def get_imports_from_file(filepath: str):
Expand Down Expand Up @@ -419,7 +417,7 @@ class TestDockerProviderProjectStructure(ExampleCoverageTest):
PROVIDER = "docker"


class TestOperatorsHooks(unittest.TestCase):
class TestOperatorsHooks:
def test_no_illegal_suffixes(self):
illegal_suffixes = ["_operator.py", "_hook.py", "_sensor.py"]
files = itertools.chain(
Expand Down