Skip to content

Commit

Permalink
Add unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
binli committed Apr 3, 2024
1 parent ca5eef4 commit 2cb1442
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
12 changes: 7 additions & 5 deletions providers/base/bin/check_power_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#
# This file is part of Checkbox.
#
# Copyright 2023 Canonical Ltd.
# Copyright 2024 Canonical Ltd.
# Authors: Bin Li <bin.li@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
Expand All @@ -28,10 +29,11 @@ def main():
sysfs_root = Path("/sys/firmware/acpi/")
profile_path = sysfs_root / "platform_profile"

profile = get_sysfs_content(profile_path).split()
print(profile[0])
# uncomment the following line to change another mode
# os.system(f"powerprofilesctl set power-saver")
profile = get_sysfs_content(profile_path)
print(profile)
# uncomment the following lines to set another mode for testing
# from switch_power_mode import set_power_profile
# set_power_profile("power-saver")


if __name__ == "__main__":
Expand Down
58 changes: 58 additions & 0 deletions providers/base/tests/test_check_power_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3
# Copyright 2024 Canonical Ltd.
# Written by:
# Bin Li <bin.li@canonical.com>
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.

# pylint: disable=import-error

""" A unittest module for the check_power_mode module. """
import unittest
from unittest.mock import patch
import io

from check_power_mode import main


class TestCheckPowerMode(unittest.TestCase):
"""Tests for the check_power_mode module."""

@patch("sys.stdout", new_callable=io.StringIO)
@patch("check_power_mode.get_sysfs_content")
def test_main_success(self, mock_get_sysfs_content, mock_stdout):
"""
Tests successful execution of the main function.
"""
mock_get_sysfs_content.return_value = "balanced"

# Call the main function
main()

self.assertEqual(mock_stdout.getvalue(), "balanced\n")

@patch("sys.stdout", new_callable=io.StringIO)
@patch("check_power_mode.get_sysfs_content")
def test_main_failure(self, mock_get_sysfs_content, mock_stdout):
"""
Tests failed execution of the main function.
"""
mock_get_sysfs_content.return_value = ""

main()

self.assertNotEqual(mock_stdout.getvalue(), "balanced\n")


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

0 comments on commit 2cb1442

Please sign in to comment.