Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enable lint PT009 'use regular assert over self.assert.*' #30521

Merged
merged 3 commits into from
Oct 7, 2024
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ select = [
"E7",
"E9",
"F",
"PT009",
"TRY201",
]
ignore = []
Expand Down
6 changes: 3 additions & 3 deletions tests/integration_tests/async_events/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _test_events_logic(self, mock_cache):
assert rv.status_code == 200
channel_id = app.config["GLOBAL_ASYNC_QUERIES_REDIS_STREAM_PREFIX"] + self.UUID
mock_xrange.assert_called_with(channel_id, "-", "+", 100)
self.assertEqual(response, {"result": []})
assert response == {"result": []}

def _test_events_last_id_logic(self, mock_cache):
with mock.patch.object(mock_cache, "xrange") as mock_xrange:
Expand All @@ -69,7 +69,7 @@ def _test_events_last_id_logic(self, mock_cache):
assert rv.status_code == 200
channel_id = app.config["GLOBAL_ASYNC_QUERIES_REDIS_STREAM_PREFIX"] + self.UUID
mock_xrange.assert_called_with(channel_id, "1607471525180-1", "+", 100)
self.assertEqual(response, {"result": []})
assert response == {"result": []}

def _test_events_results_logic(self, mock_cache):
with mock.patch.object(mock_cache, "xrange") as mock_xrange:
Expand Down Expand Up @@ -115,7 +115,7 @@ def _test_events_results_logic(self, mock_cache):
},
]
}
self.assertEqual(response, expected)
assert response == expected

@mock.patch("uuid.uuid4", return_value=UUID)
def test_events_redis_cache_backend(self, mock_uuid4):
Expand Down
35 changes: 18 additions & 17 deletions tests/integration_tests/base_api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_open_api_spec(self):
self.login(ADMIN_USERNAME)
uri = "api/v1/_openapi"
rv = self.client.get(uri)
self.assertEqual(rv.status_code, 200)
assert rv.status_code == 200
response = json.loads(rv.data.decode("utf-8"))
validate_spec(response)

Expand All @@ -87,20 +87,20 @@ def test_default_missing_declaration_get(self):
self.login(ADMIN_USERNAME)
uri = "api/v1/model1api/"
rv = self.client.get(uri)
self.assertEqual(rv.status_code, 200)
assert rv.status_code == 200
response = json.loads(rv.data.decode("utf-8"))
self.assertEqual(response["list_columns"], ["id"])
assert response["list_columns"] == ["id"]
for result in response["result"]:
self.assertEqual(list(result.keys()), ["id"])
assert list(result.keys()) == ["id"]

# Check get response
dashboard = db.session.query(Dashboard).first()
uri = f"api/v1/model1api/{dashboard.id}"
rv = self.client.get(uri)
self.assertEqual(rv.status_code, 200)
assert rv.status_code == 200
response = json.loads(rv.data.decode("utf-8"))
self.assertEqual(response["show_columns"], ["id"])
self.assertEqual(list(response["result"].keys()), ["id"])
assert response["show_columns"] == ["id"]
assert list(response["result"].keys()) == ["id"]

def test_default_missing_declaration_put_spec(self):
"""
Expand All @@ -113,17 +113,18 @@ def test_default_missing_declaration_put_spec(self):
uri = "api/v1/_openapi"
rv = self.client.get(uri)
# dashboard model accepts all fields are null
self.assertEqual(rv.status_code, 200)
assert rv.status_code == 200
response = json.loads(rv.data.decode("utf-8"))
expected_mutation_spec = {
"properties": {"id": {"type": "integer"}},
"type": "object",
}
self.assertEqual(
response["components"]["schemas"]["Model1Api.post"], expected_mutation_spec
assert (
response["components"]["schemas"]["Model1Api.post"]
== expected_mutation_spec
)
self.assertEqual(
response["components"]["schemas"]["Model1Api.put"], expected_mutation_spec
assert (
response["components"]["schemas"]["Model1Api.put"] == expected_mutation_spec
)

def test_default_missing_declaration_post(self):
Expand All @@ -145,7 +146,7 @@ def test_default_missing_declaration_post(self):
uri = "api/v1/model1api/"
rv = self.client.post(uri, json=dashboard_data)
response = json.loads(rv.data.decode("utf-8"))
self.assertEqual(rv.status_code, 422)
assert rv.status_code == 422
expected_response = {
"message": {
"css": ["Unknown field."],
Expand All @@ -156,7 +157,7 @@ def test_default_missing_declaration_post(self):
"slug": ["Unknown field."],
}
}
self.assertEqual(response, expected_response)
assert response == expected_response

def test_refuse_invalid_format_request(self):
"""
Expand All @@ -169,7 +170,7 @@ def test_refuse_invalid_format_request(self):
rv = self.client.post(
uri, data="a: value\nb: 1\n", content_type="application/yaml"
)
self.assertEqual(rv.status_code, 400)
assert rv.status_code == 400

@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices")
def test_default_missing_declaration_put(self):
Expand All @@ -185,14 +186,14 @@ def test_default_missing_declaration_put(self):
uri = f"api/v1/model1api/{dashboard.id}"
rv = self.client.put(uri, json=dashboard_data)
response = json.loads(rv.data.decode("utf-8"))
self.assertEqual(rv.status_code, 422)
assert rv.status_code == 422
expected_response = {
"message": {
"dashboard_title": ["Unknown field."],
"slug": ["Unknown field."],
}
}
self.assertEqual(response, expected_response)
assert response == expected_response


class ApiOwnersTestCaseMixin:
Expand Down
24 changes: 12 additions & 12 deletions tests/integration_tests/cache_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def test_no_data_cache(self):
)
# restore DATA_CACHE_CONFIG
app.config["DATA_CACHE_CONFIG"] = data_cache_config
self.assertFalse(resp["is_cached"])
self.assertFalse(resp_from_cache["is_cached"])
assert not resp["is_cached"]
assert not resp_from_cache["is_cached"]

@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
def test_slice_data_cache(self):
Expand All @@ -84,20 +84,20 @@ def test_slice_data_cache(self):
resp_from_cache = self.get_json_resp(
json_endpoint, {"form_data": json.dumps(slc.viz.form_data)}
)
self.assertFalse(resp["is_cached"])
self.assertTrue(resp_from_cache["is_cached"])
assert not resp["is_cached"]
assert resp_from_cache["is_cached"]
# should fallback to default cache timeout
self.assertEqual(resp_from_cache["cache_timeout"], 10)
self.assertEqual(resp_from_cache["status"], QueryStatus.SUCCESS)
self.assertEqual(resp["data"], resp_from_cache["data"])
self.assertEqual(resp["query"], resp_from_cache["query"])
assert resp_from_cache["cache_timeout"] == 10
assert resp_from_cache["status"] == QueryStatus.SUCCESS
assert resp["data"] == resp_from_cache["data"]
assert resp["query"] == resp_from_cache["query"]
# should exists in `data_cache`
self.assertEqual(
cache_manager.data_cache.get(resp_from_cache["cache_key"])["query"],
resp_from_cache["query"],
assert (
cache_manager.data_cache.get(resp_from_cache["cache_key"])["query"]
== resp_from_cache["query"]
)
# should not exists in `cache`
self.assertIsNone(cache_manager.cache.get(resp_from_cache["cache_key"]))
assert cache_manager.cache.get(resp_from_cache["cache_key"]) is None

# reset cache config
app.config["DATA_CACHE_CONFIG"] = data_cache_config
Expand Down
Loading
Loading