Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomli380576 committed Jan 9, 2025
1 parent e312403 commit 86926bd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
13 changes: 5 additions & 8 deletions providers/resource/bin/v4l2_ioctl_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@


def main():

Check warning on line 7 in providers/resource/bin/v4l2_ioctl_resource.py

View check run for this annotation

Codecov / codecov/patch

providers/resource/bin/v4l2_ioctl_resource.py#L7

Added line #L7 was not covered by tests
try:
udev_out = sp.check_output(
"udev_resource.py -f CAPTURE", universal_newlines=True, shell=True
)
lines = udev_out.splitlines()
except Exception as e:
lines = [f"name: broken {str(e)}"]
udev_out = sp.check_output(
"udev_resource.py -f CAPTURE", universal_newlines=True, shell=True
)
lines = udev_out.splitlines()

for line in lines:
if line.startswith("name:"):
for ioctl_names in TEST_NAME_TO_IOCTL_MAP.values():
for name in ioctl_names:
print(line)

Check warning on line 17 in providers/resource/bin/v4l2_ioctl_resource.py

View check run for this annotation

Codecov / codecov/patch

providers/resource/bin/v4l2_ioctl_resource.py#L17

Added line #L17 was not covered by tests
print("ioctl_name:", format(name))
print("ioctl_name: ".format(name))
print() # empty line to mark end of list item

Check warning on line 19 in providers/resource/bin/v4l2_ioctl_resource.py

View check run for this annotation

Codecov / codecov/patch

providers/resource/bin/v4l2_ioctl_resource.py#L19

Added line #L19 was not covered by tests


Expand Down
39 changes: 34 additions & 5 deletions providers/resource/tests/test_v4l2_compliance_resource.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import unittest as ut
from unittest.mock import MagicMock, patch, call
from v4l2_ioctl_resource import main
from v4l2_ioctl_resource import main as main_under_test
from checkbox_support.parsers.v4l2_compliance import TEST_NAME_TO_IOCTL_MAP


class TestV4L2IoctlResource(ut.TestCase):
@patch("v4l2_ioctl_resource.check_output")
@patch("v4l2_ioctl_resource.sp.check_output")
def test_all_ioctl_names_are_generated(self, mock_check_output: MagicMock):
mock_check_output.return_value = "\n".join(
[
"path: /devices/pci0000:00/0000:00:14.0/usb3/3-9/3-9:1.0/video4linux/video0",
"path: /devices/pci0000:00/0000:00:14.0/usb3/"
+ "3-9/3-9:1.0/video4linux/video0",
"name: video0",
"bus: video4linux",
"category: CAPTURE",
Expand All @@ -20,7 +21,35 @@ def test_all_ioctl_names_are_generated(self, mock_check_output: MagicMock):
"vendor: CN0PW36V8LG009BQA0YWA00",
"product_slug: Integrated_Webcam_HD__Integrate",
"vendor_slug: CN0PW36V8LG009BQA0YWA00",
"",
"path: /devices/pci0000:00/0000:00:14.0/usb3/"
+ "3-9/3-9:1.0/video4linux/video1",
"name: video1",
"bus: video4linux",
"category: CAPTURE",
"driver: uvcvideo",
"product_id: 2312312",
"vendor_id: 12213",
"product: Integrated_Webcam_HD: Integrate",
"vendor: CN0PW36V3444438LG009BQA0YWA00",
"product_slug: Integrated_Webcam_HD__Integrate",
"vendor_slug: CN0PW36V8LG009BQA0YWA00",
]
)
main()


with patch("builtins.print") as mock_print:
main_under_test()
expected_calls = []
for name in "video0", "video1":
for ioctl_names in TEST_NAME_TO_IOCTL_MAP.values():
for ioctl_name in ioctl_names:
expected_calls.append(call("name: {}".format(name)))
expected_calls.append(
call("ioctl_name: ".format(ioctl_name))
)
expected_calls.append(call())
mock_print.assert_has_calls(expected_calls)


if __name__ == "__main__":
ut.main()

0 comments on commit 86926bd

Please sign in to comment.