Skip to content

Commit

Permalink
Convert to pytest asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
aayush-se committed Sep 10, 2024
1 parent 2e0858c commit 774ffc7
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 62 deletions.
2 changes: 1 addition & 1 deletion tests/automation/autofix/test_autofix_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def test_get_issue_summary(self):
result = instance.get_issue_summary()

self.assertIsNotNone(result)
self.assertIsInstance(result, IssueSummary)
assert isinstance(result, IssueSummary)
self.assertEqual(
result.summary_of_the_issue_based_on_your_step_by_step_reasoning, "summary"
)
Expand Down
4 changes: 2 additions & 2 deletions tests/automation/autofix/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,8 @@ def test_auto_generated_step_ids(self):
self.assertIsNotNone(added_step2.id)

# Check that the IDs are strings (UUIDs)
self.assertIsInstance(added_step1.id, str)
self.assertIsInstance(added_step2.id, str)
assert isinstance(added_step1.id, str)
assert isinstance(added_step2.id, str)

# Check that the IDs are unique
self.assertNotEqual(added_step1.id, added_step2.id)
Expand Down
38 changes: 20 additions & 18 deletions tests/seer/anomaly_detection/detectors/test_anomaly_detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,18 @@ def test_compute_matrix_profile(self, mock_stump):
mp_utils=self.mp_utils,
)

self.assertIsInstance(result, MPTimeSeriesAnomalies)
self.assertIsInstance(result.flags, list)
self.assertEqual(result.scores, [0.1, 6.5, 4.8, 0.2])
self.assertIsInstance(result.scores, list)
self.assertEqual(
result.flags, ["none", "anomaly_higher_confidence", "anomaly_higher_confidence", "none"]
)
self.assertIsInstance(result.matrix_profile, np.ndarray)
self.assertIsInstance(result.window_size, int)
assert isinstance(result, MPTimeSeriesAnomalies)
assert isinstance(result.flags, list)
assert result.scores == [0.1, 6.5, 4.8, 0.2]
assert isinstance(result.scores, list)
assert result.flags == [
"none",
"anomaly_higher_confidence",
"anomaly_higher_confidence",
"none",
]
assert isinstance(result.matrix_profile, np.ndarray)
assert isinstance(result.window_size, int)
mock_stump.assert_called_once()
self.scorer.batch_score.assert_called_once()
self.ws_selector.optimal_window_size.assert_called_once()
Expand Down Expand Up @@ -110,14 +113,13 @@ def test_detect(self, MockMPUtils, MockMPScorer, MockStumpi):

anomalies = self.detector.detect(self.timeseries, self.config, mock_scorer, mock_utils)

self.assertIsInstance(anomalies, MPTimeSeriesAnomalies)
self.assertIsInstance(anomalies.flags, list)
self.assertIsInstance(anomalies.scores, list)
self.assertIsInstance(anomalies.matrix_profile, np.ndarray)
self.assertIsInstance(anomalies.window_size, int)

self.assertEqual(len(anomalies.scores), 3)
self.assertEqual(len(anomalies.flags), 3)
self.assertEqual(len(anomalies.matrix_profile), 3)
assert isinstance(anomalies, MPTimeSeriesAnomalies)
assert isinstance(anomalies.flags, list)
assert isinstance(anomalies.scores, list)
assert isinstance(anomalies.matrix_profile, np.ndarray)
assert isinstance(anomalies.window_size, int)
assert len(anomalies.scores) == 3
assert len(anomalies.flags) == 3
assert len(anomalies.matrix_profile) == 3
mock_scorer.stream_score.assert_called_once()
mock_stream.assert_called_once()
6 changes: 3 additions & 3 deletions tests/seer/anomaly_detection/detectors/test_mp_scorers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_batch_score_synthetic_data(self):
convert_synthetic_ts(
"tests/seer/anomaly_detection/test_data/synthetic_series",
as_ts_datatype=False,
include_range=True,
include_anomaly_range=True,
)
)

Expand All @@ -45,7 +45,7 @@ def test_batch_score_synthetic_data(self):
else "noanomaly"
)

self.assertEqual(result, expected_type)
assert result == expected_type

def test_stream_score(self):

Expand Down Expand Up @@ -73,4 +73,4 @@ def test_stream_score(self):
mp_dist_baseline,
)

self.assertEqual(flag[0], expected_flags[i])
assert flag[0] == expected_flags[i]
7 changes: 3 additions & 4 deletions tests/seer/anomaly_detection/detectors/test_mp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_no_normalizer_with_normalization(self):
mp, pad_to_len=None, mp_config=self.mock_mp_config, normalizer=None
)

self.assertTrue("Need normalizer to normalize MP" in str(context.exception))
assert "Need normalizer to normalize MP" in str(context.exception)

def test_incorrect_padding(self):

Expand All @@ -83,7 +83,6 @@ def test_incorrect_padding(self):
mp, pad_to_len=1, mp_config=self.mock_mp_config, normalizer=None
)

self.assertTrue(
"Requested length should be greater than or equal to current mp_dist"
in str(context.exception)
assert "Requested length should be greater than or equal to current mp_dist" in str(
context.exception
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def test_optimal_window_size(self):
period = 24 * 4

for window in actual_windows:
self.assertTrue(window - (period * n) <= window <= window + (period * n))
assert window - (period * n) <= window <= window + (period * n)
11 changes: 5 additions & 6 deletions tests/seer/anomaly_detection/models/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ def test_convert_external_ts_to_internal(self):

converted_ts = convert_external_ts_to_internal(external_ts)

self.assertIsInstance(converted_ts, TimeSeries)
self.assertIsInstance(converted_ts.values, np.ndarray)
self.assertIsInstance(converted_ts.timestamps, np.ndarray)

self.assertEqual(len(converted_ts.values), 2)
self.assertEqual(len(converted_ts.timestamps), 2)
assert isinstance(converted_ts, TimeSeries)
assert isinstance(converted_ts.values, np.ndarray)
assert isinstance(converted_ts.timestamps, np.ndarray)
assert len(converted_ts.values) == 2
assert len(converted_ts.timestamps) == 2
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_get_anomaly_algo_data_with_padding(self):

algo_data = self.anomalies.get_anomaly_algo_data(front_pad_to_len)

self.assertEqual(algo_data, expected_data)
assert algo_data == expected_data

def test_get_anomaly_algo_data_without_padding(self):
# Test with front_pad_to_len equal to the length of matrix_profile
Expand All @@ -39,10 +39,8 @@ def test_get_anomaly_algo_data_without_padding(self):
]

algo_data = self.anomalies.get_anomaly_algo_data(front_pad_to_len)

self.assertEqual(algo_data, expected_data)
assert algo_data == expected_data

front_pad_to_len = 1
algo_data = self.anomalies.get_anomaly_algo_data(front_pad_to_len)

self.assertEqual(algo_data, expected_data)
assert algo_data == expected_data
33 changes: 15 additions & 18 deletions tests/seer/anomaly_detection/test_anomaly_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ def test_store_data(self):
request=request, alert_data_accessor=alert_data_accessor
)

# Successful
self.assertEqual(
response,
StoreDataResponse(success=True),
"Store Data Response should be successful",
assert "Store Data Response should be successful", response == StoreDataResponse(
success=True
)

# TODO: Clean up DB(?)
Expand All @@ -71,10 +68,10 @@ def test_detect_anomalies_batch(self):

response = AnomalyDetection().detect_anomalies(request=anomaly_request)

self.assertIsInstance(response, DetectAnomaliesResponse)
self.assertIsInstance(response.timeseries, list)
self.assertEqual(len(response.timeseries), len(ts))
self.assertIsInstance(response.timeseries[0], TimeSeriesPoint)
assert isinstance(response, DetectAnomaliesResponse)
assert isinstance(response.timeseries, list)
assert len(response.timeseries) == len(ts)
assert isinstance(response.timeseries[0], TimeSeriesPoint)

def test_detect_anomalies_online(self):

Expand All @@ -98,12 +95,12 @@ def test_detect_anomalies_online(self):

response = AnomalyDetection().detect_anomalies(request=request)

self.assertIsInstance(response, DetectAnomaliesResponse)
self.assertIsInstance(response.timeseries, list)
self.assertEqual(
len(response.timeseries), len(ts) + 1
assert isinstance(response, DetectAnomaliesResponse)
assert isinstance(response.timeseries, list)
assert (
len(response.timeseries) == len(ts) + 1
) # Adding one more observation to timeseries
self.assertIsInstance(response.timeseries[0], TimeSeriesPoint)
assert isinstance(response.timeseries[0], TimeSeriesPoint)

def test_detect_anomalies_combo(self):

Expand Down Expand Up @@ -133,7 +130,7 @@ def test_detect_anomalies_combo(self):

response = AnomalyDetection().detect_anomalies(request=request)

self.assertIsInstance(response, DetectAnomaliesResponse)
self.assertIsInstance(response.timeseries, list)
self.assertEqual(len(response.timeseries), n)
self.assertIsInstance(response.timeseries[0], TimeSeriesPoint)
assert isinstance(response, DetectAnomaliesResponse)
assert isinstance(response.timeseries, list)
assert len(response.timeseries) == n
assert isinstance(response.timeseries[0], TimeSeriesPoint)
8 changes: 4 additions & 4 deletions tests/test_severity_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_high_severity_error(self):
SeverityRequest(message="TypeError: bad operand type for unary -: 'str'")
).severity

self.assertIsInstance(score, float)
assert isinstance(score, float)
if not can_use_model_stubs():
self.assertGreater(score, 0.5)

Expand All @@ -23,14 +23,14 @@ def test_low_severity_error(self):
SeverityRequest(message="log: user enjoyed their experience")
).severity

self.assertIsInstance(score, float)
assert isinstance(score, float)
if not can_use_model_stubs():
self.assertLess(score, 0.5)

def test_empty_input(self):
score = self.severity_inference.severity_score(SeverityRequest(message="")).severity

self.assertIsInstance(score, float)
assert isinstance(score, float)

def test_output_range(self):
test_messages = ["Test message 1", "Another test message", "Yet another test message"]
Expand All @@ -40,7 +40,7 @@ def test_output_range(self):
SeverityRequest(message=message)
).severity

self.assertIsInstance(score, float)
assert isinstance(score, float)
self.assertGreaterEqual(score, 0.0)
self.assertLessEqual(score, 1.0)

Expand Down

0 comments on commit 774ffc7

Please sign in to comment.