Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
yihong1120 committed Oct 22, 2024
1 parent 2d3ccaf commit a47675b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ def test_augment_image_with_alpha_channel(

self.assertTrue(mock_imwrite.called)

@patch('examples.YOLO_data_augmentation.data_augmentation_albumentations.A.Compose')
@patch(
'examples.YOLO_data_augmentation.data_augmentation_albumentations.'
'A.Compose',
)
def test_resize_small_image(self, mock_compose: MagicMock) -> None:
"""
Test resize_image_and_bboxes method with a small image.
Expand All @@ -179,20 +182,20 @@ def test_resize_small_image(self, mock_compose: MagicMock) -> None:
# Mock the transformation result
mock_transformed = {
'image': np.random.randint(0, 255, (64, 64, 3), dtype=np.uint8),
'bboxes': [[0.5, 0.5, 0.2, 0.2]]
'bboxes': [[0.5, 0.5, 0.2, 0.2]],
}
mock_transform = MagicMock()
mock_transform.return_value = mock_transformed
mock_compose.return_value = mock_transform

resized_image, resized_bboxes = self.augmenter.resize_image_and_bboxes(
mock_image, mock_bboxes, class_labels, image_path
mock_image, mock_bboxes, class_labels, image_path,
)

# Verify that the Compose and BboxParams were called correctly
mock_compose.assert_called()
mock_transform.assert_called_once_with(
image=mock_image, bboxes=mock_bboxes, class_labels=class_labels
image=mock_image, bboxes=mock_bboxes, class_labels=class_labels,
)

# Verify the transformation result
Expand Down
36 changes: 36 additions & 0 deletions tests/src/notifiers/broadcast_notifier_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations

import os
import subprocess
import unittest
from unittest.mock import MagicMock
from unittest.mock import Mock
from unittest.mock import patch

Expand Down Expand Up @@ -87,6 +90,39 @@ def test_main(
# Assert the print function was called with the expected output
mock_print.assert_called_once_with('Broadcast status: True')

@patch('requests.post')
def test_main_as_script(self, mock_post: MagicMock) -> None:
"""
Test running the broadcast_notifier.py script as the main program.
"""
mock_response: MagicMock = MagicMock()
mock_response.status_code = 200
mock_post.return_value = mock_response

# Get the absolute path to the broadcast_notifier.py script
script_path = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
'../../../src/notifiers/broadcast_notifier.py',
),
)

# Run the script using subprocess
result = subprocess.run(
['python', script_path],
capture_output=True, text=True,
)

# Print stdout and stderr for debugging
print('STDOUT:', result.stdout)
print('STDERR:', result.stderr)

# Assert that the script runs without errors
self.assertEqual(
result.returncode, 0,
'Script exited with a non-zero status.',
)


if __name__ == '__main__':
unittest.main()
42 changes: 42 additions & 0 deletions tests/src/notifiers/wechat_notifier_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import os
import subprocess
import unittest
from io import BytesIO
from unittest.mock import MagicMock
Expand Down Expand Up @@ -169,6 +171,46 @@ def test_main(
self.assertIn('errcode', print_args[0])
self.assertIn('errmsg', print_args[0])

@patch('requests.post')
@patch.dict(
os.environ, {
'WECHAT_CORP_ID': 'test_corp_id',
'WECHAT_CORP_SECRET': 'test_corp_secret',
'WECHAT_AGENT_ID': '1000002',
},
)
def test_main_as_script(self, mock_post: MagicMock) -> None:
"""
Test running the wechat_notifier.py script as the main program.
"""
mock_response: MagicMock = MagicMock()
mock_response.status_code = 200
mock_post.return_value = mock_response

# Get the absolute path to the wechat_notifier.py script
script_path = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
'../../../src/notifiers/wechat_notifier.py',
),
)

# Run the script using subprocess
result = subprocess.run(
['python', script_path],
capture_output=True, text=True,
)

# Print stdout and stderr for debugging
print('STDOUT:', result.stdout)
print('STDERR:', result.stderr)

# Assert that the script runs without errors
self.assertEqual(
result.returncode, 0,
'Script exited with a non-zero status.',
)


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

0 comments on commit a47675b

Please sign in to comment.