From bab438a4561770894b4ac897f1e230271e5d63da Mon Sep 17 00:00:00 2001 From: markrz12 Date: Tue, 28 Nov 2023 18:59:33 +0100 Subject: [PATCH] test-1 --- ml/test.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 ml/test.py diff --git a/ml/test.py b/ml/test.py new file mode 100644 index 0000000..e41296a --- /dev/null +++ b/ml/test.py @@ -0,0 +1,57 @@ +import unittest +from unittest.mock import Mock, patch +from element_detection import detect, show_image_with_boxes, BoundBox + +class TestYourModule(unittest.TestCase): + + @patch('element_detection.FACES_MODEL.predict') + @patch('element_detection.PLATES_MODEL.predict') + def test_detect(self, mock_plates_predict, mock_faces_predict): + # Przygotuj dane testowe + image_path = 'blurme/ml/MicrosoftTeams-image.png' + mock_faces_predict.__iter__.return_value = [Mock(cpu=Mock(return_value=Mock(numpy=Mock(boxes=[Mock(xyxy=[1, 2, 3, 4])]))))] + mock_plates_predict.__iter__.return_value = [Mock(cpu=Mock(return_value=Mock(numpy=Mock(boxes=[Mock(xyxy=[5, 6, 7, 8])]))))] + + # Wywołaj funkcję + result = detect(image_path) + + # Sprawdź, czy otrzymałeś oczekiwany wynik + expected_result = [BoundBox(1, 2, 3, 4), BoundBox(5, 6, 7, 8)] + self.assertEqual(result, expected_result) + + @patch('element_detection.ImageDraw.Draw') + @patch('element_detection.ImageFont.truetype') + #@patch('element_detection.Image.save') + @patch('element_detection.Image.open') + def test_show_image_with_boxes(self, mock_image_open, + #mock_image_save, + mock_truetype, mock_draw): + # Przygotuj dane testowe + in_image_path = 'blurme/ml/MicrosoftTeams-image.png' + bounding_boxes = [BoundBox(1, 2, 3, 4), BoundBox(5, 6, 7, 8)] + + # Wywołaj funkcję + show_image_with_boxes(in_image_path, bounding_boxes) + + # Sprawdź, czy odpowiednie metody zostały wywołane z oczekiwanymi argumentami + mock_image_open.assert_called_once_with(in_image_path) + mock_draw.assert_called_once() + # mock_image_save.assert_called_once() + +if __name__ == '__main__': + unittest.main() + +def test_detect_with_stub(self, mock_plates_predict, mock_faces_predict): + # Przygotuj dane testowe + image_path = 'blurme/ml/MicrosoftTeams-image.png' + mock_faces_predict.__iter__.return_value = [Mock(cpu=Mock(return_value=Mock(numpy=Mock(boxes=[Mock(xyxy=[1, 2, 3, 4])]))))] + mock_plates_predict.__iter__.return_value = [Mock(cpu=Mock(return_value=Mock(numpy=Mock(boxes=[Mock(xyxy=[5, 6, 7, 8])]))))] + + # Użyj stuba zamiast rzeczywistego BoundBox + with patch('element_detection.BoundBox', StubBoundBox): + # Wywołaj funkcję + result = detect(image_path) + + # Sprawdź, czy otrzymałeś oczekiwany wynik + expected_result = [StubBoundBox(1, 2, 3, 4), StubBoundBox(5, 6, 7, 8)] + self.assertEqual(result, expected_result) \ No newline at end of file