Skip to content

Commit ee31d4c

Browse files
authored
Trim to 3 tests - COPDS-2551
1 parent 644629a commit ee31d4c

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

cads_catalogue_api_service/sanity_check.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
logger = structlog.getLogger(__name__)
1010

11+
SANITY_CHECK_MAX_ENTRIES = 3
12+
1113

1214
class SanityCheckStatus(str, Enum):
1315
available = "available"
@@ -102,7 +104,8 @@ def process(
102104
- If 0 tests succeeded, status is "down"
103105
104106
Args:
105-
sanity_check: List of test results, each containing a "success" boolean
107+
sanity_check: List of test results, each containing a "success" boolean. Only first
108+
SANITY_CHECK_MAX_ENTRIES are considered for status calculation.
106109
107110
Returns
108111
-------
@@ -113,8 +116,11 @@ def process(
113116
if not sanity_check:
114117
return SanityCheckResult(status=SanityCheckStatus.available, timestamp=None)
115118

116-
# Extract timestamp from the last test
117-
timestamp = sanity_check[-1].finished_at
119+
# Just take into account latest X tests (tests are sorted descending by finished_at)
120+
sanity_check = sanity_check[:SANITY_CHECK_MAX_ENTRIES]
121+
122+
# Extract timestamp from the latest test
123+
timestamp = sanity_check[0].finished_at
118124

119125
# Count successful tests
120126
successful_tests = sum(1 for test in sanity_check if test.success)

tests/test_60_sanity_check.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def test_get_outputs() -> None:
1414

1515
# Test case: None input
1616
assert get_outputs(None) is None
17-
1817
# Test case: Empty list
1918
assert get_outputs([]) is None
2019

@@ -34,6 +33,7 @@ def test_get_outputs() -> None:
3433
},
3534
]
3635
result = get_outputs(valid_input)
36+
3737
assert result is not None
3838
assert len(result) == 2
3939
assert result[0].req_id == "test1"
@@ -54,6 +54,7 @@ def test_get_outputs() -> None:
5454
"finished_at": timestamp,
5555
}
5656
]
57+
5758
assert get_outputs(invalid_missing_field) is None
5859

5960
# Test case: Wrong type for success field
@@ -65,6 +66,7 @@ def test_get_outputs() -> None:
6566
"finished_at": timestamp,
6667
}
6768
]
69+
6870
assert get_outputs(invalid_wrong_type) is None
6971

7072
# Test case: Invalid timestamp
@@ -76,11 +78,15 @@ def test_get_outputs() -> None:
7678
"finished_at": timestamp,
7779
}
7880
]
81+
7982
assert get_outputs(invalid_timestamp) is None
8083

8184

8285
def test_process() -> None:
83-
timestamp = datetime.datetime.now(datetime.timezone.utc)
86+
timestamp = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(
87+
seconds=1
88+
)
89+
timestamp_e = datetime.datetime.now(datetime.timezone.utc)
8490

8591
# Test case: None or empty list
8692
assert process(None) == SanityCheckResult(
@@ -92,24 +98,34 @@ def test_process() -> None:
9298
timestamp=None,
9399
)
94100

95-
# Test case: More than 3 tests
96-
four_tests = [
101+
# Test case: more than 3 tests
102+
many_tests = [
97103
SanityCheckOutput(
98-
req_id="test1", success=True, started_at=timestamp, finished_at=timestamp
104+
req_id="test1", success=False, started_at=timestamp, finished_at=timestamp_e
99105
),
100106
SanityCheckOutput(
101-
req_id="test2", success=False, started_at=timestamp, finished_at=timestamp
107+
req_id="test2", success=True, started_at=timestamp, finished_at=timestamp
102108
),
103109
SanityCheckOutput(
104110
req_id="test3", success=False, started_at=timestamp, finished_at=timestamp
105111
),
106112
SanityCheckOutput(
107113
req_id="test4", success=False, started_at=timestamp, finished_at=timestamp
108114
),
115+
SanityCheckOutput(
116+
req_id="test5", success=False, started_at=timestamp, finished_at=timestamp
117+
),
118+
SanityCheckOutput(
119+
req_id="test6", success=False, started_at=timestamp, finished_at=timestamp
120+
),
121+
SanityCheckOutput(
122+
req_id="test7", success=False, started_at=timestamp, finished_at=timestamp
123+
),
109124
]
110-
assert process(four_tests) == SanityCheckResult(
111-
status=SanityCheckStatus.available,
112-
timestamp=timestamp,
125+
126+
assert process(many_tests) == SanityCheckResult(
127+
status=SanityCheckStatus.warning,
128+
timestamp=timestamp_e,
113129
)
114130

115131
# Test case: 1 test, successful
@@ -118,6 +134,7 @@ def test_process() -> None:
118134
req_id="test1", success=True, started_at=timestamp, finished_at=timestamp
119135
)
120136
]
137+
121138
assert process(one_test_success) == SanityCheckResult(
122139
status=SanityCheckStatus.available,
123140
timestamp=timestamp,
@@ -129,6 +146,7 @@ def test_process() -> None:
129146
req_id="test1", success=False, started_at=timestamp, finished_at=timestamp
130147
)
131148
]
149+
132150
assert process(one_test_failed) == SanityCheckResult(
133151
status=SanityCheckStatus.down,
134152
timestamp=timestamp,
@@ -143,6 +161,7 @@ def test_process() -> None:
143161
req_id="test2", success=True, started_at=timestamp, finished_at=timestamp
144162
),
145163
]
164+
146165
assert process(two_tests_all_success) == SanityCheckResult(
147166
status=SanityCheckStatus.available,
148167
timestamp=timestamp,
@@ -157,6 +176,7 @@ def test_process() -> None:
157176
req_id="test2", success=False, started_at=timestamp, finished_at=timestamp
158177
),
159178
]
179+
160180
assert process(two_tests_one_success) == SanityCheckResult(
161181
status=SanityCheckStatus.warning,
162182
timestamp=timestamp,
@@ -171,6 +191,7 @@ def test_process() -> None:
171191
req_id="test2", success=False, started_at=timestamp, finished_at=timestamp
172192
),
173193
]
194+
174195
assert process(two_tests_none_success) == SanityCheckResult(
175196
status=SanityCheckStatus.down,
176197
timestamp=timestamp,
@@ -188,6 +209,7 @@ def test_process() -> None:
188209
req_id="test3", success=True, started_at=timestamp, finished_at=timestamp
189210
),
190211
]
212+
191213
assert process(three_tests_all_success) == SanityCheckResult(
192214
status=SanityCheckStatus.available,
193215
timestamp=timestamp,
@@ -205,6 +227,7 @@ def test_process() -> None:
205227
req_id="test3", success=False, started_at=timestamp, finished_at=timestamp
206228
),
207229
]
230+
208231
assert process(three_tests_two_success) == SanityCheckResult(
209232
status=SanityCheckStatus.available,
210233
timestamp=timestamp,
@@ -222,6 +245,7 @@ def test_process() -> None:
222245
req_id="test3", success=False, started_at=timestamp, finished_at=timestamp
223246
),
224247
]
248+
225249
assert process(three_tests_one_success) == SanityCheckResult(
226250
status=SanityCheckStatus.warning,
227251
timestamp=timestamp,
@@ -239,6 +263,7 @@ def test_process() -> None:
239263
req_id="test3", success=False, started_at=timestamp, finished_at=timestamp
240264
),
241265
]
266+
242267
assert process(three_tests_none_success) == SanityCheckResult(
243268
status=SanityCheckStatus.down,
244269
timestamp=timestamp,

0 commit comments

Comments
 (0)