Skip to content

Commit

Permalink
Update 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 75f11d1 commit db88258
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 55 deletions.
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
@@ -1,5 +1,7 @@
import unittest

import numpy as np

from seer.anomaly_detection.detectors.window_size_selectors import SuSSWindowSizeSelector
from tests.seer.anomaly_detection.test_utils import convert_synthetic_ts

Expand All @@ -9,6 +11,23 @@ class TestSuSSWindowSizeSelector(unittest.TestCase):
def setUp(self):
self.selector = SuSSWindowSizeSelector()

def test_optimal_window_size_constant_series(self):
ts = np.array([5.0] * 700)
with self.assertRaises(Exception, msg="Search for optimal window failed."):
self.selector.optimal_window_size(ts)

def test_optimal_window_size_linear_series(self):
ts = np.linspace(1, 100, 100)
window_size = self.selector.optimal_window_size(ts)
assert "Window size for linear series should be greater than the lower bound.", (
window_size > 10
)

def test_optimal_window_size_short_series(self):
ts = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
with self.assertRaises(Exception, msg="Search for optimal window failed."):
self.selector.optimal_window_size(ts)

def test_optimal_window_size(self):

actual_windows = []
Expand All @@ -26,4 +45,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)

0 comments on commit db88258

Please sign in to comment.