11from unittest import IsolatedAsyncioTestCase
2- from unittest .mock import MagicMock , patch
2+ from unittest .mock import MagicMock , mock_open , patch
33import time
4+ from django .test import TestCase
5+ from apps .webcam .tasks import update_cam_status_from_sql_db
46
57from apps .consumer .processor import (
8+ check_backup_exists ,
69 on_reconnect ,
710 on_close ,
811 on_channel_close ,
12+ save_original_image_to_pvc ,
13+ save_watermarked_image_to_drivebc_pvc ,
14+ save_watermarked_image_to_pvc ,
915)
1016
1117
@@ -66,4 +72,112 @@ async def test_on_channel_close_without_exception(self, mock_logger):
6672
6773 mock_logger .warning .assert_called_once_with (
6874 "RabbitMQ channel closed: None"
69- )
75+ )
76+
77+
78+ class TestUpdateCamStatusFromSqlDb (TestCase ):
79+
80+ @patch ("apps.webcam.tasks.CameraSource" )
81+ def test_update_cam_status_from_sql_db_exception (
82+ self ,
83+ mock_camera_source ,
84+ ):
85+ mock_camera_source .objects .using .side_effect = Exception ("Database error" )
86+
87+ result = update_cam_status_from_sql_db (1 )
88+
89+ self .assertEqual (result , {})
90+
91+ class TestOtherFunctions (TestCase ):
92+
93+ @patch ("apps.consumer.processor.logging.exception" )
94+ @patch ("builtins.open" , side_effect = OSError ("Disk full" ))
95+ def test_save_original_image_to_pvc_exception (
96+ self ,
97+ mock_open ,
98+ mock_logging ,
99+ ):
100+ save_original_image_to_pvc ("123" , b"image" )
101+
102+ mock_logging .assert_called_once ()
103+
104+ @patch ("apps.consumer.processor.logging.exception" )
105+ @patch ("builtins.open" , side_effect = OSError ("Disk full" ))
106+ def test_save_watermarked_image_to_pvc_exception (
107+ self ,
108+ mock_open ,
109+ mock_logging ,
110+ ):
111+ save_watermarked_image_to_pvc (
112+ "123" ,
113+ b"image" ,
114+ "20250101000000" ,
115+ True ,
116+ )
117+
118+ mock_logging .assert_called_once ()
119+
120+ @patch ("apps.consumer.processor.os.path.exists" )
121+ def test_check_backup_exists_true (self , mock_exists ):
122+ mock_exists .return_value = True
123+
124+ self .assertTrue (check_backup_exists ("123" ))
125+
126+ @patch ("apps.consumer.processor.logging.exception" )
127+ @patch ("builtins.open" , side_effect = OSError ("Disk full" ))
128+ def test_save_watermarked_image_to_drivebc_pvc_exception (
129+ self ,
130+ mock_open ,
131+ mock_logging ,
132+ ):
133+ save_watermarked_image_to_drivebc_pvc (
134+ "123" ,
135+ b"image" ,
136+ True ,
137+ )
138+
139+ mock_logging .assert_called_once ()
140+
141+ @patch ("apps.consumer.processor.check_backup_exists" )
142+ @patch ("apps.consumer.processor.shutil.move" )
143+ @patch ("apps.consumer.processor.os.path.exists" )
144+ @patch ("builtins.open" , new_callable = mock_open )
145+ def test_backup_already_exists (
146+ self ,
147+ mock_file ,
148+ mock_exists ,
149+ mock_move ,
150+ mock_backup ,
151+ ):
152+ mock_exists .return_value = True
153+ mock_backup .return_value = True
154+
155+ save_watermarked_image_to_drivebc_pvc (
156+ "123" ,
157+ b"image" ,
158+ False ,
159+ )
160+
161+ mock_move .assert_not_called ()
162+
163+ @patch ("apps.consumer.processor.check_backup_exists" )
164+ @patch ("apps.consumer.processor.shutil.move" )
165+ @patch ("apps.consumer.processor.os.path.exists" )
166+ @patch ("builtins.open" , new_callable = mock_open )
167+ def test_backup_created (
168+ self ,
169+ mock_file ,
170+ mock_exists ,
171+ mock_move ,
172+ mock_backup ,
173+ ):
174+ mock_exists .return_value = True
175+ mock_backup .return_value = False
176+
177+ save_watermarked_image_to_drivebc_pvc (
178+ "123" ,
179+ b"image" ,
180+ False ,
181+ )
182+
183+ mock_move .assert_called_once ()
0 commit comments