Skip to content

Commit

Permalink
Create test_anomaly_detection.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 7, 2024
1 parent 7cfca64 commit 3594c58
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions projects/piguardian/test/test_anomaly_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
from unittest.mock import patch, MagicMock
from anomaly_detection import AnomalyDetector

class TestAnomalyDetector(unittest.TestCase):
def setUp(self):
self.anomaly_detector = AnomalyDetector()

def test_init(self):
self.assertIsNotNone(self.anomaly_detector)

@patch('anomaly_detection.stats')
def test_fit(self, mock_stats):
mock_stats.norm.fit.return_value = (1, 2)
data = [1, 2, 3, 4, 5]
self.anomaly_detector.fit(data)
mock_stats.norm.fit.assert_called_once_with(data)

def test_predict(self):
data = [1, 2, 3, 4, 5]
self.anomaly_detector.fit(data)
anomaly_score = self.anomaly_detector.predict(6)
self.assertGreater(anomaly_score, 0.5)

@patch('anomaly_detection.plotly')
def test_visualize(self, mock_plotly):
data = [1, 2, 3, 4, 5]
self.anomaly_detector.fit(data)
self.anomaly_detector.visualize(data)
mock_plotly.plot.assert_called_once()

if __name__ == '__main__':
unittest.main()

0 comments on commit 3594c58

Please sign in to comment.