From c577eee8665b6b0e80cfd746765e3b3f038090c6 Mon Sep 17 00:00:00 2001 From: "marco.tessari" Date: Mon, 26 Feb 2024 16:08:09 +0100 Subject: [PATCH] fix: now cpu temp has 2 decimals --- pyproject.toml | 2 +- src/raspi_home_texx/sys_info/cpu.py | 2 +- tests/test_cpu.py | 18 ++++++++++++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index b23fdd1..ff8823e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "raspi_home_texx" -version = "0.2.0" +version = "0.2.1" authors = [ { name="Marco Tessari", email="marco.tex@live.com" }, ] diff --git a/src/raspi_home_texx/sys_info/cpu.py b/src/raspi_home_texx/sys_info/cpu.py index 4979dc0..93c6b74 100644 --- a/src/raspi_home_texx/sys_info/cpu.py +++ b/src/raspi_home_texx/sys_info/cpu.py @@ -18,7 +18,7 @@ def __init__(self) -> None: if platform.system() == "Linux": try: with io.open(self.__sensor_file, 'r') as f: - self.temperature = float(f.read().strip()) / 1000 + self.temperature = round(float(f.read().strip()) / 1000, 2) except FileNotFoundError as err: pass diff --git a/tests/test_cpu.py b/tests/test_cpu.py index 7e1bd26..8708bec 100644 --- a/tests/test_cpu.py +++ b/tests/test_cpu.py @@ -15,13 +15,27 @@ def test_init_linux(self, mock_io_open, mock_platform_system): # Set up mock file content and open function for Linux platform mock_io_open.return_value.__enter__ = lambda s: s mock_io_open.return_value.__exit__ = Mock() - mock_io_open.return_value.read.return_value = '50000\n' # Assuming a temperature value of 50.000°C + mock_io_open.return_value.read.return_value = '41234\n' # Assuming a temperature value of 50.000°C # Create a Cpu instance on Linux cpu_linux = Cpu() # Check if the temperature is correctly set - self.assertEqual(cpu_linux.temperature, 50.0) + self.assertEqual(cpu_linux.temperature, 41.23) + + @patch('platform.system', return_value='Linux') + @patch('io.open', create=True) + def test_init_linux(self, mock_io_open, mock_platform_system): + # Set up mock file content and open function for Linux platform + mock_io_open.return_value.__enter__ = lambda s: s + mock_io_open.return_value.__exit__ = Mock() + mock_io_open.return_value.read.return_value = '41237\n' # Assuming a temperature value of 50.000°C + + # Create a Cpu instance on Linux + cpu_linux = Cpu() + + # Check if the temperature is correctly set + self.assertEqual(cpu_linux.temperature, 41.24) @patch('platform.system', return_value='Windows') def test_init_windows(self, mock_platform_system):