Skip to content

Commit b53f593

Browse files
committed
fix: resolve all FT lint violations and install pre-commit hook
Install the flagsmith-lint-tests hook in pre-commit config and fix all existing FT003/FT004 violations across the test suite by renaming tests to test_{subject}__{condition}__{expected} and adding Given/When/Then comments.
1 parent 8b46353 commit b53f593

21 files changed

+148
-107
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ repos:
2020
- id: check-toml
2121
- repo: local
2222
hooks:
23+
- id: flagsmith-lint-tests
24+
name: flagsmith-lint-tests
25+
language: system
26+
entry: python -P src/common/lint_tests.py
27+
types: [python]
28+
files: ^tests/.*test_.*\.py$
2329
- id: python-typecheck
2430
name: python-typecheck
2531
language: system

tests/integration/core/snapshots/test_metrics__return_expected.txt renamed to tests/integration/core/snapshots/test_metrics__default_request__returns_expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# HELP pytest_tests_run_total Total number of tests run by pytest.
22
# TYPE pytest_tests_run_total counter
3-
pytest_tests_run_total{test_name="test_metrics__return_expected"} 1.0
3+
pytest_tests_run_total{test_name="test_metrics__default_request__returns_expected"} 1.0

tests/integration/core/test_main.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_main__non_overridden_args__defaults_to_django(
3535
["flagsmith", "healthcheck", "tcp"],
3636
),
3737
)
38-
def test_main__healthcheck_tcp__no_server__runs_expected(
38+
def test_main__healthcheck_tcp_no_server__runs_expected(
3939
argv: list[str],
4040
capsys: pytest.CaptureFixture[str],
4141
) -> None:
@@ -48,7 +48,7 @@ def test_main__healthcheck_tcp__no_server__runs_expected(
4848
assert exc_info.value.code == 1
4949

5050

51-
def test_main__healthcheck_tcp__server__runs_expected(
51+
def test_main__healthcheck_tcp_server__runs_expected(
5252
unused_tcp_port: int,
5353
http_server: HTTPServer,
5454
) -> None:
@@ -63,7 +63,7 @@ def test_main__healthcheck_tcp__server__runs_expected(
6363
assert exc_info.value.code == 0
6464

6565

66-
def test_main__healthcheck_http__no_server__runs_expected() -> None:
66+
def test_main__healthcheck_http_no_server__runs_expected() -> None:
6767
# Given
6868
argv = ["flagsmith", "healthcheck", "http"]
6969

@@ -79,7 +79,7 @@ def test_main__healthcheck_http__no_server__runs_expected() -> None:
7979
(["flagsmith", "healthcheck", "http", "health/readiness"], "/health/readiness"),
8080
),
8181
)
82-
def test_main__healthcheck_http__server__runs_expected(
82+
def test_main__healthcheck_http_server__runs_expected(
8383
unused_tcp_port: int,
8484
http_server: HTTPServer,
8585
argv: list[str],
@@ -94,7 +94,7 @@ def test_main__healthcheck_http__server__runs_expected(
9494
main(argv)
9595

9696

97-
def test_main__healthcheck_http__server_invalid_response__runs_expected(
97+
def test_main__healthcheck_http_server_invalid_response__runs_expected(
9898
unused_tcp_port: int,
9999
http_server: HTTPServer,
100100
) -> None:

tests/integration/core/test_views.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,34 @@
55
from common.test_tools import SnapshotFixture
66

77

8-
def test_liveness_probe__return_expected(
8+
def test_liveness_probe__default_request__returns_expected(
99
client: APIClient,
1010
) -> None:
11-
response = client.get("/health/liveness/")
11+
# Given
12+
url = "/health/liveness/"
13+
14+
# When
15+
response = client.get(url)
16+
17+
# Then
1218
assert response.status_code == 200
1319
assert response.json() == {"status": "ok"}
1420

1521

1622
@pytest.mark.prometheus_multiprocess_mode
17-
def test_metrics__return_expected(
23+
def test_metrics__default_request__returns_expected(
1824
test_metric: prometheus_client.Counter,
1925
client: APIClient,
2026
snapshot: SnapshotFixture,
2127
) -> None:
22-
# Arrange
23-
test_metric.labels(test_name="test_metrics__return_expected").inc()
28+
# Given
29+
test_metric.labels(
30+
test_name="test_metrics__default_request__returns_expected"
31+
).inc()
2432

25-
# Act
33+
# When
2634
response = client.get("/metrics", follow=True)
2735

28-
# Assert
36+
# Then
2937
assert response.status_code == 200
3038
assert response.content.decode() == snapshot()

tests/integration/flagsmith_schemas/test_dynamodb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ def test_document__validate_json__expected_result(
966966
assert document == expected_result
967967

968968

969-
def test_type_adapter__identity__duplicate_features__raises_expected(
969+
def test_type_adapter__identity_duplicate_features__raises_expected(
970970
mocker: MockerFixture,
971971
) -> None:
972972
# Given
@@ -1009,7 +1009,7 @@ def test_type_adapter__identity__duplicate_features__raises_expected(
10091009
)
10101010

10111011

1012-
def test_type_adapter__environment__multivariate_feature_states_percentage_allocation_exceeds_100__raises_expected() -> (
1012+
def test_type_adapter__environment_multivariate_feature_states_percentage_allocation_exceeds_100__raises_expected() -> (
10131013
None
10141014
):
10151015
# Given

tests/unit/common/core/test_logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
@pytest.mark.freeze_time("2023-12-08T06:05:47+00:00")
11-
def test_json_formatter__outputs_expected(
11+
def test_json_formatter__format_log__outputs_expected(
1212
caplog: pytest.LogCaptureFixture,
1313
request: pytest.FixtureRequest,
1414
) -> None:

tests/unit/common/core/test_middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from common.core import middleware as middleware_module
44

55

6-
def test_APIResponseVersionHeaderMiddleware__adds_version_header(
6+
def test_api_response_version_header_middleware__request_processed__adds_version_header(
77
mocker: MockerFixture,
88
) -> None:
99
# Given

tests/unit/common/core/test_utils.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,33 @@ def bad_replica(mocker: MockerFixture) -> MockType:
4949
return replica
5050

5151

52-
def test__is_oss_for_enterprise_returns_false(fs: FakeFilesystem) -> None:
52+
def test_is_oss__enterprise_version_exists__returns_false(fs: FakeFilesystem) -> None:
5353
# Given
5454
fs.create_file("./ENTERPRISE_VERSION")
5555

56-
# Then
56+
# When / Then
5757
assert is_oss() is False
5858

5959

60-
def test__is_oss_for_saas_returns_false(fs: FakeFilesystem) -> None:
60+
def test_is_oss__saas_deployment_exists__returns_false(fs: FakeFilesystem) -> None:
6161
# Given
6262
fs.create_file("./SAAS_DEPLOYMENT")
6363

64-
# Then
64+
# When / Then
6565
assert is_oss() is False
6666

6767

68-
def test__is_oss_for_oss_returns_true(fs: FakeFilesystem) -> None:
68+
def test_is_oss__no_version_files__returns_true(fs: FakeFilesystem) -> None:
69+
# Given / When
70+
result = is_oss()
71+
6972
# Then
70-
assert is_oss() is True
73+
assert result is True
7174

7275

73-
def test_get_version_info(fs: FakeFilesystem) -> None:
76+
def test_get_version_info__all_files_present__returns_expected(
77+
fs: FakeFilesystem,
78+
) -> None:
7479
# Given
7580
expected_manifest_contents = {
7681
".": "2.66.2",
@@ -98,7 +103,7 @@ def test_get_version_info(fs: FakeFilesystem) -> None:
98103
}
99104

100105

101-
def test_get_version_info_with_missing_files(fs: FakeFilesystem) -> None:
106+
def test_get_version_info__missing_files__returns_unknown(fs: FakeFilesystem) -> None:
102107
# Given
103108
fs.create_file("./ENTERPRISE_VERSION")
104109

@@ -217,7 +222,7 @@ def test_get_version__invalid_file_contents__returns_unknown(
217222
({"default", "replica_1", "cross_region_replica_1"}, True),
218223
],
219224
)
220-
def test_is_database_replica_setup__tells_whether_any_replica_is_present(
225+
def test_is_database_replica_setup__various_database_configs__returns_expected(
221226
database_names: list[str],
222227
expected: bool,
223228
mocker: MockerFixture,

tests/unit/common/gunicorn/test_conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from common.gunicorn.conf import child_exit, when_ready
77

88

9-
def test_child_exit__calls_mark_process_dead_with_worker_pid(
9+
def test_child_exit__worker_exits__marks_process_dead_with_worker_pid(
1010
mocker: MockerFixture,
1111
) -> None:
1212
# Given

tests/unit/common/gunicorn/test_logging.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
@pytest.mark.freeze_time("2023-12-08T06:05:47+00:00")
20-
def test_gunicorn_access_log_json_formatter__outputs_expected(
20+
def test_gunicorn_access_log_json_formatter__format_log__outputs_expected(
2121
settings: SettingsWrapper,
2222
) -> None:
2323
# Given
@@ -97,7 +97,7 @@ def test_gunicorn_access_log_json_formatter__outputs_expected(
9797
}
9898

9999

100-
def test_gunicorn_prometheus_gunicorn_logger__expected_metrics(
100+
def test_gunicorn_prometheus_gunicorn_logger__access_logged__expected_metrics(
101101
mocker: MockerFixture,
102102
assert_metric: AssertMetricFixture,
103103
) -> None:
@@ -136,7 +136,7 @@ def test_gunicorn_prometheus_gunicorn_logger__expected_metrics(
136136
)
137137

138138

139-
def test_gunicorn_json_capable_logger__sets_expected_formatters(
139+
def test_gunicorn_json_capable_logger__json_log_format__sets_expected_formatters(
140140
settings: SettingsWrapper,
141141
) -> None:
142142
# Given

0 commit comments

Comments
 (0)