Skip to content

Commit

Permalink
Zero warnings in CLI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Taragolis committed Apr 23, 2024
1 parent 665da50 commit 68f0c29
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 41 deletions.
66 changes: 36 additions & 30 deletions tests/cli/commands/test_connection_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from airflow.exceptions import AirflowException
from airflow.models import Connection
from airflow.utils.db import merge_conn
from airflow.utils.session import create_session, provide_session
from airflow.utils.session import create_session
from tests.test_utils.db import clear_db_connections

pytestmark = pytest.mark.db_test
Expand Down Expand Up @@ -372,6 +372,7 @@ def setup_method(self):
"login": "airflow",
"port": 5432,
"schema": "airflow",
"extra": None,
},
id="json-connection",
),
Expand All @@ -393,6 +394,7 @@ def setup_method(self):
"login": "airflow",
"port": 5432,
"schema": "airflow",
"extra": None,
},
id="uri-connection-with-description",
),
Expand All @@ -414,6 +416,7 @@ def setup_method(self):
"login": "airflow",
"port": 5432,
"schema": "airflow",
"extra": None,
},
id="uri-connection-with-description-2",
),
Expand All @@ -424,7 +427,7 @@ def setup_method(self):
"new2",
f"--conn-uri={TEST_URL}",
"--conn-extra",
"{'extra': 'yes'}",
'{"extra": "yes"}',
],
"Successfully added `conn_id`=new2 : postgresql://airflow:airflow@host:5432/airflow",
{
Expand All @@ -436,6 +439,7 @@ def setup_method(self):
"login": "airflow",
"port": 5432,
"schema": "airflow",
"extra": '{"extra": "yes"}',
},
id="uri-connection-with-extra",
),
Expand All @@ -446,7 +450,7 @@ def setup_method(self):
"new3",
f"--conn-uri={TEST_URL}",
"--conn-extra",
"{'extra': 'yes'}",
'{"extra": "yes"}',
"--conn-description",
"new3 description",
],
Expand All @@ -460,6 +464,7 @@ def setup_method(self):
"login": "airflow",
"port": 5432,
"schema": "airflow",
"extra": '{"extra": "yes"}',
},
id="uri-connection-with-extra-and-description",
),
Expand All @@ -486,6 +491,7 @@ def setup_method(self):
"login": "airflow",
"port": 9083,
"schema": "airflow",
"extra": None,
},
id="individual-parts",
),
Expand All @@ -498,7 +504,7 @@ def setup_method(self):
"",
"--conn-type=google_cloud_platform",
"--conn-extra",
"{'extra': 'yes'}",
'{"extra": "yes"}',
"--conn-description=new5 description",
],
"Successfully added `conn_id`=new5 : google_cloud_platform://:@:",
Expand All @@ -511,6 +517,7 @@ def setup_method(self):
"login": None,
"port": None,
"schema": None,
"extra": '{"extra": "yes"}',
},
id="empty-uri-with-conn-type-and-extra",
),
Expand All @@ -526,6 +533,7 @@ def setup_method(self):
"login": None,
"port": None,
"schema": "",
"extra": '{"region_name": "foo-bar-1"}',
},
id="uri-without-authority-and-host-blocks",
),
Expand All @@ -541,33 +549,34 @@ def setup_method(self):
"login": "",
"port": None,
"schema": "",
"extra": '{"region_name": "foo-bar-1"}',
},
id="uri-with-@-instead-authority-and-host-blocks",
),
],
)
@pytest.mark.execution_timeout(120)
def test_cli_connection_add(self, cmd, expected_output, expected_conn):
def test_cli_connection_add(self, cmd, expected_output, expected_conn, session):
with redirect_stdout(StringIO()) as stdout:
connection_command.connections_add(self.parser.parse_args(cmd))

stdout = stdout.getvalue()

assert expected_output in stdout
conn_id = cmd[2]
with create_session() as session:
comparable_attrs = [
"conn_type",
"description",
"host",
"is_encrypted",
"is_extra_encrypted",
"login",
"port",
"schema",
]
current_conn = session.query(Connection).filter(Connection.conn_id == conn_id).first()
assert expected_conn == {attr: getattr(current_conn, attr) for attr in comparable_attrs}
comparable_attrs = [
"conn_type",
"description",
"host",
"is_encrypted",
"is_extra_encrypted",
"login",
"port",
"schema",
"extra",
]
current_conn = session.query(Connection).filter(Connection.conn_id == conn_id).first()
assert expected_conn == {attr: getattr(current_conn, attr) for attr in comparable_attrs}

def test_cli_connections_add_duplicate(self):
conn_id = "to_be_duplicated"
Expand Down Expand Up @@ -653,8 +662,7 @@ class TestCliDeleteConnections:
def setup_method(self):
clear_db_connections(add_default_connections_back=False)

@provide_session
def test_cli_delete_connections(self, session=None):
def test_cli_delete_connections(self, session):
merge_conn(
Connection(
conn_id="new1",
Expand Down Expand Up @@ -729,7 +737,7 @@ def test_cli_connections_import_should_load_connections(self, mock_exists, mock_
"password": "password",
"port": 5432,
"schema": "airflow",
"extra": "test",
"extra": '{"foo": "bar"}',
},
"new1": {
"conn_type": "mysql",
Expand All @@ -739,7 +747,7 @@ def test_cli_connections_import_should_load_connections(self, mock_exists, mock_
"password": "password",
"port": 3306,
"schema": "airflow",
"extra": "test",
"extra": '{"spam": "egg"}',
},
}

Expand Down Expand Up @@ -772,11 +780,10 @@ def test_cli_connections_import_should_load_connections(self, mock_exists, mock_
}
assert expected_connections == current_conns_as_dicts

@provide_session
@mock.patch("airflow.secrets.local_filesystem._parse_secret_file")
@mock.patch("os.path.exists")
def test_cli_connections_import_should_not_overwrite_existing_connections(
self, mock_exists, mock_parse_secret_file, session=None
self, mock_exists, mock_parse_secret_file, session
):
mock_exists.return_value = True

Expand Down Expand Up @@ -804,7 +811,7 @@ def test_cli_connections_import_should_not_overwrite_existing_connections(
"password": "password",
"port": 5432,
"schema": "airflow",
"extra": "test",
"extra": '{"foo": "bar"}',
},
"new3": {
"conn_type": "mysql",
Expand All @@ -814,7 +821,7 @@ def test_cli_connections_import_should_not_overwrite_existing_connections(
"password": "new password",
"port": 3306,
"schema": "airflow",
"extra": "test",
"extra": '{"spam": "egg"}',
},
}

Expand Down Expand Up @@ -852,11 +859,10 @@ def test_cli_connections_import_should_not_overwrite_existing_connections(
# The existing connection's description should not have changed
assert current_conns_as_dicts["new3"]["description"] == "original description"

@provide_session
@mock.patch("airflow.secrets.local_filesystem._parse_secret_file")
@mock.patch("os.path.exists")
def test_cli_connections_import_should_overwrite_existing_connections(
self, mock_exists, mock_parse_secret_file, session=None
self, mock_exists, mock_parse_secret_file, session
):
mock_exists.return_value = True

Expand Down Expand Up @@ -884,7 +890,7 @@ def test_cli_connections_import_should_overwrite_existing_connections(
"password": "password",
"port": 5432,
"schema": "airflow",
"extra": "test",
"extra": '{"foo": "bar"}',
},
"new3": {
"conn_type": "mysql",
Expand All @@ -894,7 +900,7 @@ def test_cli_connections_import_should_overwrite_existing_connections(
"password": "new password",
"port": 3306,
"schema": "airflow",
"extra": "test",
"extra": '{"spam": "egg"}',
},
}

Expand Down
2 changes: 1 addition & 1 deletion tests/cli/commands/test_task_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ def test_task_states_for_dag_run(self):
run_type=DagRunType.MANUAL,
external_trigger=True,
)
ti2 = TaskInstance(task2, dagrun.execution_date)
ti2 = TaskInstance(task2, run_id=dagrun.run_id)
ti2.set_state(State.SUCCESS)
ti_start = ti2.start_date
ti_end = ti2.end_date
Expand Down
10 changes: 0 additions & 10 deletions tests/deprecations_ignore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,7 @@


# CLI
- tests/cli/commands/test_connection_command.py::TestCliAddConnections::test_cli_connection_add
- tests/cli/commands/test_connection_command.py::TestCliImportConnections::test_cli_connections_import_should_load_connections
- tests/cli/commands/test_connection_command.py::TestCliImportConnections::test_cli_connections_import_should_not_overwrite_existing_connections
- tests/cli/commands/test_connection_command.py::TestCliImportConnections::test_cli_connections_import_should_overwrite_existing_connections
- tests/cli/commands/test_kubernetes_command.py::TestGenerateDagYamlCommand::test_generate_dag_yaml
- tests/cli/commands/test_task_command.py::TestCliTasks::test_parentdag_downstream_clear
- tests/cli/commands/test_task_command.py::TestCliTasks::test_subdag_clear
- tests/cli/commands/test_task_command.py::TestCliTasks::test_task_states_for_dag_run
- tests/cli/commands/test_task_command.py::TestCliTasks::test_test
- tests/cli/commands/test_task_command.py::TestCliTasks::test_test_no_execution_date
- tests/cli/commands/test_task_command.py::TestCliTasks::test_test_with_existing_dag_run


# Core
Expand Down

0 comments on commit 68f0c29

Please sign in to comment.