This is an automated email from the ASF dual-hosted git repository.
taragolis pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 5a612dac4a Resolve PT008: Use `return_value=` instead of patching with
`lambda` (#38244)
5a612dac4a is described below
commit 5a612dac4a6ebdf44d73d5d23fed5419e2519eb1
Author: Andrey Anshin <[email protected]>
AuthorDate: Mon Mar 18 22:08:55 2024 +0400
Resolve PT008: Use `return_value=` instead of patching with `lambda`
(#38244)
---
pyproject.toml | 1 -
tests/api_connexion/endpoints/test_dag_run_endpoint.py | 15 +++++++--------
tests/cli/commands/test_task_command.py | 4 ++--
tests/providers/google/cloud/hooks/test_cloud_build.py | 15 +++++++++++----
4 files changed, 20 insertions(+), 15 deletions(-)
diff --git a/pyproject.toml b/pyproject.toml
index 6114cf93d5..b913274769 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1358,7 +1358,6 @@ ignore = [
"PT005", # Fixture returns a value, remove leading underscore
"PT006", # Wrong type of names in @pytest.mark.parametrize
"PT007", # Wrong type of values in @pytest.mark.parametrize
- "PT008", # Use return_value= instead of patching with lambda
"PT011", # pytest.raises() is too broad, set the match parameter
"PT018", # assertion should be broken down into multiple parts
"PT019", # fixture without value is injected as parameter, use
@pytest.mark.usefixtures instead
diff --git a/tests/api_connexion/endpoints/test_dag_run_endpoint.py
b/tests/api_connexion/endpoints/test_dag_run_endpoint.py
index 03b9e3a5b9..ef91ef78eb 100644
--- a/tests/api_connexion/endpoints/test_dag_run_endpoint.py
+++ b/tests/api_connexion/endpoints/test_dag_run_endpoint.py
@@ -1157,6 +1157,7 @@ class TestGetDagRunBatchDateFilters(TestDagRunEndpoint):
class TestPostDagRun(TestDagRunEndpoint):
+ @time_machine.travel(timezone.utcnow(), tick=False)
@pytest.mark.parametrize("logical_date_field_name", ["execution_date",
"logical_date"])
@pytest.mark.parametrize(
"dag_run_id, logical_date, note, data_interval_start,
data_interval_end",
@@ -1188,8 +1189,7 @@ class TestPostDagRun(TestDagRunEndpoint):
):
self._create_dag("TEST_DAG_ID")
- # We'll patch airflow.utils.timezone.utcnow to always return this so we
- # can check the returned dates.
+ # We freeze time for this test, so we could check it into the returned
dates.
fixed_now = timezone.utcnow()
# raise NotImplementedError("TODO: Add tests for data_interval_start
and data_interval_end")
@@ -1205,12 +1205,11 @@ class TestPostDagRun(TestDagRunEndpoint):
request_json["data_interval_end"] = data_interval_end
request_json["note"] = note
- with mock.patch("airflow.utils.timezone.utcnow", lambda: fixed_now):
- response = self.client.post(
- "api/v1/dags/TEST_DAG_ID/dagRuns",
- json=request_json,
- environ_overrides={"REMOTE_USER": "test"},
- )
+ response = self.client.post(
+ "api/v1/dags/TEST_DAG_ID/dagRuns",
+ json=request_json,
+ environ_overrides={"REMOTE_USER": "test"},
+ )
assert response.status_code == 200
diff --git a/tests/cli/commands/test_task_command.py
b/tests/cli/commands/test_task_command.py
index d345075213..056e9cddc2 100644
--- a/tests/cli/commands/test_task_command.py
+++ b/tests/cli/commands/test_task_command.py
@@ -165,12 +165,12 @@ class TestCliTasks:
["tasks", "test", "example_python_operator", "print_the_context",
"2018-01-01"],
)
- with mock.patch("airflow.models.TaskInstance.run", new=lambda *_,
**__: print(password)):
+ with mock.patch("airflow.models.TaskInstance.run", side_effect=lambda
*_, **__: print(password)):
task_command.task_test(args)
assert capsys.readouterr().out.endswith("***\n")
not_password = "!4321drowssapemos"
- with mock.patch("airflow.models.TaskInstance.run", new=lambda *_,
**__: print(not_password)):
+ with mock.patch("airflow.models.TaskInstance.run", side_effect=lambda
*_, **__: print(not_password)):
task_command.task_test(args)
assert capsys.readouterr().out.endswith(f"{not_password}\n")
diff --git a/tests/providers/google/cloud/hooks/test_cloud_build.py
b/tests/providers/google/cloud/hooks/test_cloud_build.py
index 7fb00f279f..69c123a210 100644
--- a/tests/providers/google/cloud/hooks/test_cloud_build.py
+++ b/tests/providers/google/cloud/hooks/test_cloud_build.py
@@ -363,15 +363,18 @@ class TestAsyncHook:
)
@pytest.mark.asyncio
- @mock.patch.object(
- CloudBuildAsyncClient, "__init__", lambda self, credentials,
client_info, client_options: None
- )
@mock.patch(CLOUD_BUILD_PATH.format("CloudBuildAsyncHook.get_credentials"))
@mock.patch(CLOUD_BUILD_PATH.format("CloudBuildAsyncClient.get_build"))
async def
test_async_cloud_build_service_client_creation_should_execute_successfully(
- self, mocked_get_build, mock_get_creds, hook
+ self, mocked_get_build, mock_get_creds, hook, mocker
):
+ fake_credentials = mock.MagicMock(name="FakeCreds")
+ mock_get_creds.return_value = fake_credentials
+ mocked_cloud_build_async_client = mocker.patch.object(
+ CloudBuildAsyncClient, "__init__", return_value=None,
spec=CloudBuildAsyncClient
+ )
mocked_get_build.return_value = Future()
+
await hook.get_cloud_build(project_id=PROJECT_ID, id_=BUILD_ID)
request = GetBuildRequest(
dict(
@@ -379,8 +382,12 @@ class TestAsyncHook:
id=BUILD_ID,
)
)
+
mock_get_creds.assert_called_once()
mocked_get_build.assert_called_once_with(request=request,
retry=DEFAULT, timeout=None, metadata=())
+ mocked_cloud_build_async_client.assert_called_once_with(
+ client_info=mock.ANY, client_options=None,
credentials=fake_credentials
+ )
@pytest.mark.asyncio
async def
test_async_get_clod_build_without_build_id_should_throw_exception(self, hook):