Skip to content

Commit

Permalink
Added tests to image quality test
Browse files Browse the repository at this point in the history
Included some automated tests to the image quality test to increase
the coverage
  • Loading branch information
fernando79513 committed Oct 16, 2023
1 parent 70abcca commit 0c23946
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
Binary file added providers/base/data/images/image_quality_bad.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added providers/base/data/images/image_quality_good.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
134 changes: 134 additions & 0 deletions providers/base/tests/test_camera_quality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#!/usr/bin/env python3
#
# This file is part of Checkbox.
#
# Copyright 2023 Canonical Ltd.
# Written by:
# Fernando Bravo <[email protected]>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
#

import os
import unittest
from unittest.mock import patch

import cv2

from bin.camera_quality_test import brisque

default_dir = os.path.join(os.path.dirname(__file__), '../data')
data_dir = os.getenv('PLAINBOX_PROVIDER_DATA', default=default_dir)


class CameraQualityTests(unittest.TestCase):
"""This class provides test cases for the camera_quality_test module."""

# Setup the patch for all the tests
def setUp(self):
self.patcher = patch('cv2.VideoCapture')
self.mock_capture = self.patcher.start()

def tearDown(self):
self.patcher.stop()

def test_device_not_opened(self):
"""
The test should fail if the camera device is not opened.
"""

# Set the mock
self.mock_capture.return_value.isOpened.return_value = False

assert brisque() == 1

def test_grab_not_available(self):
"""
The test should fail if the camera device can't grab an image.
"""

# Set the mock
self.mock_capture.return_value.isOpened.return_value = True
self.mock_capture.return_value.grab.return_value = False

assert brisque() == 1

def test_image_not_read(self):
"""
The test should fail if the camera device can't read the image.
"""

# Set the mock
self.mock_capture.return_value.isOpened.return_value = True
self.mock_capture.return_value.grab.return_value = True
self.mock_capture.return_value.read.return_value = (False, None)

assert brisque() == 1

def test_save_image(self):
"""
Check if the test can save the image.
"""

# Set the mock
img_path = os.path.join(data_dir, "images/image_quality_good.jpg")
img = cv2.imread(img_path)
self.mock_capture.return_value.isOpened.return_value = True
self.mock_capture.return_value.grab.return_value = True
self.mock_capture.return_value.read.return_value = (True, img)

assert brisque(save=True) == 0

def test_good_image(self):
"""
Check if the test passes with a valid image.
"""

# Set the mock
img_path = os.path.join(data_dir, "images/image_quality_good.jpg")
img = cv2.imread(img_path)
self.mock_capture.return_value.isOpened.return_value = True
self.mock_capture.return_value.grab.return_value = True
self.mock_capture.return_value.read.return_value = (True, img)

assert brisque() == 0

def test_bad_image(self):
"""
Check if the test does not pass with a bad image.
"""

# Set the mock
img_path = os.path.join(data_dir, "images/image_quality_bad.jpg")
img = cv2.imread(img_path)
self.mock_capture.return_value.isOpened.return_value = True
self.mock_capture.return_value.grab.return_value = True
self.mock_capture.return_value.read.return_value = (True, img)

assert brisque() == 1

@patch('checkbox_support.vendor.brisque.brisque.BRISQUE.score')
def test_invalid_image(self, mock_score):
"""
Check if the test passes with a valid image.
"""

# Set the mock
img_path = os.path.join(data_dir, "images/image_quality_good.jpg")
img = cv2.imread(img_path)
self.mock_capture.return_value.isOpened.return_value = True
self.mock_capture.return_value.grab.return_value = True
self.mock_capture.return_value.read.return_value = (True, img)
mock_score.return_value = float('nan')

assert brisque() == 1

0 comments on commit 0c23946

Please sign in to comment.