From acb6427160f9fcaf57e276759874f0c36b6259ff Mon Sep 17 00:00:00 2001 From: 7eXx Date: Sun, 21 Jan 2024 21:05:44 +0100 Subject: [PATCH 1/6] wip: introduce system info that holds cpu, memory, disk --- src/system_info_texx/system_info.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/system_info_texx/system_info.py diff --git a/src/system_info_texx/system_info.py b/src/system_info_texx/system_info.py new file mode 100644 index 0000000..75e0bb4 --- /dev/null +++ b/src/system_info_texx/system_info.py @@ -0,0 +1,20 @@ +from abc import ABC + +from .cpu import Cpu +from .disk import Disk +from .memory import Memory + + +class SystemInfo(ABC): + pass + +class SimpleSystemInfo(SystemInfo): + + def __init__(self): + def __init__(self): + self.cpu = Cpu() + self.memory = Memory() + self.disk = Disk() + + def serialize(self): + return f'{{"cpu": {self.cpu.serialize()}, "memory": {self.memory.serialize()}, "disk": {self.disk.serialize()}}}' \ No newline at end of file From 83aff3974e8928303a4f8929d647d487a144b161 Mon Sep 17 00:00:00 2001 From: "marco.tessari" Date: Mon, 22 Jan 2024 14:15:20 +0100 Subject: [PATCH 2/6] Wip: introduce expanded system info with plain data for all attributes. --- RELEASE.md | 6 ++ src/system_info_texx/memory.py | 2 +- src/system_info_texx/system_info.py | 42 ++++++++-- tests/test_cpu.py | 48 +++++++++++ tests/test_disk.py | 45 +++++++++++ tests/test_memory.py | 42 ++++++++++ tests/test_system_info.py | 118 ++++++++++++++-------------- 7 files changed, 237 insertions(+), 66 deletions(-) create mode 100644 tests/test_cpu.py create mode 100644 tests/test_disk.py create mode 100644 tests/test_memory.py diff --git a/RELEASE.md b/RELEASE.md index e1d0396..7752fb9 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -1,6 +1,12 @@ # Release Procedure This guide aims to help how to release a new version of the package. +## Tests +To execute all test from a folder use: +```shell +python -m unitest discover tests/ +``` + ## Setup Ensure you have general installed ***build*** ```shell diff --git a/src/system_info_texx/memory.py b/src/system_info_texx/memory.py index 44d98b3..65cfff3 100644 --- a/src/system_info_texx/memory.py +++ b/src/system_info_texx/memory.py @@ -16,4 +16,4 @@ def __init__(self) -> None: self.free = Utils.convert_byte_to_megabyte(memory[4]) def serialize(self) -> str: - return json.dumps(self.__dict__) \ No newline at end of file + return json.dumps(self.__dict__) diff --git a/src/system_info_texx/system_info.py b/src/system_info_texx/system_info.py index 75e0bb4..2adcc0b 100644 --- a/src/system_info_texx/system_info.py +++ b/src/system_info_texx/system_info.py @@ -1,4 +1,5 @@ -from abc import ABC +import json +from abc import ABC, abstractmethod from .cpu import Cpu from .disk import Disk @@ -6,15 +7,40 @@ class SystemInfo(ABC): - pass + @abstractmethod + def serialize(self) -> str: + pass + class SimpleSystemInfo(SystemInfo): + def __init__(self): + self.cpu = Cpu() + self.memory = Memory() + self.disk = Disk() + + def serialize(self) -> str: + return f'{{"cpu": {self.cpu.serialize()}, "memory": {self.memory.serialize()}, "disk": {self.disk.serialize()}}}' + +class ExpandedSystemInfo(SystemInfo): def __init__(self): - def __init__(self): - self.cpu = Cpu() - self.memory = Memory() - self.disk = Disk() + cpu = Cpu() + memory = Memory() + disk = Disk() + + self.cpu_temp = cpu.temperature + self.cpu_perc = cpu.percentage + + self.tot_mem = memory.total + self.ava_mem = memory.available + self.per_mem = memory.percentage + self.use_mem = memory.used + self.fre_mem = memory.free + + self.tot_disk = disk.total + self.use_disk = disk.used + self.fre_disk = disk.free + self.per_disk = disk.percentage - def serialize(self): - return f'{{"cpu": {self.cpu.serialize()}, "memory": {self.memory.serialize()}, "disk": {self.disk.serialize()}}}' \ No newline at end of file + def serialize(self) -> str: + return json.dumps(self.__dict__) diff --git a/tests/test_cpu.py b/tests/test_cpu.py new file mode 100644 index 0000000..7dc8a8f --- /dev/null +++ b/tests/test_cpu.py @@ -0,0 +1,48 @@ +import unittest +from unittest.mock import patch, Mock + +from src.system_info_texx.cpu import Cpu + + +class TestCpu(unittest.TestCase): + def setUp(self): + # Create a Cpu instance + self.cpu = Cpu() + + @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 = '50000\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) + + @patch('platform.system', return_value='Windows') + def test_init_windows(self, mock_platform_system): + # Create a Cpu instance on Windows + cpu_windows = Cpu() + + # Check if the temperature is set to 0 on non-Linux platforms + self.assertEqual(cpu_windows.temperature, 0) + + def test_serialize(self): + # Set up values for the Cpu instance + self.cpu.percentage = 75.0 + self.cpu.temperature = 60.0 + self.cpu.min_temp = 0 + self.cpu.max_temp = 100 + self.cpu.unit = "°C" + + # Call the serialize method + result = self.cpu.serialize() + + # Check if the result matches the expected JSON string + expected_result = '{"percentage": 75.0, "temperature": 60.0, "min_temp": 0, "max_temp": 100, "unit": "°C"}' + self.assertEqual(result, expected_result) + diff --git a/tests/test_disk.py b/tests/test_disk.py new file mode 100644 index 0000000..79e38cf --- /dev/null +++ b/tests/test_disk.py @@ -0,0 +1,45 @@ +import unittest +from unittest.mock import Mock, patch +import psutil +import json +from src.system_info_texx.disk import Disk, Utils + + +class TestDisk(unittest.TestCase): + @patch('psutil.disk_usage', return_value=(1024 * 1024 * 1024 * 10, 1024 * 1024 * 1024 * 5, 1024 * 1024 * 1024 * 2, 50.0)) + def test_init(self, mock_disk_usage): + # Create a Disk instance + disk = Disk(unit="GB") + + # Check if the attributes are set correctly + self.assertEqual(disk.unit, "GB") + self.assertEqual(disk.total, 10) + self.assertEqual(disk.used, 5) + self.assertEqual(disk.free, 2) + self.assertEqual(disk.percentage, 50.0) + + def test_init_invalid_unit(self): + # Create a Disk instance with an invalid unit + disk = Disk(unit="InvalidUnit") + + # Check if the unit is set to "B" as a default + self.assertEqual(disk.unit, "B") + + def test_serialize(self): + # Set up values for the Disk instance + disk = Disk(unit="GB") + disk.total = 10 + disk.used = 5 + disk.free = 2 + disk.percentage = 50.0 + + # Call the serialize method + result = disk.serialize() + + # Check if the result matches the expected JSON string + expected_result = '{"unit": "GB", "total": 10, "used": 5, "free": 2, "percentage": 50.0}' + self.assertEqual(result, expected_result) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_memory.py b/tests/test_memory.py new file mode 100644 index 0000000..7f1b043 --- /dev/null +++ b/tests/test_memory.py @@ -0,0 +1,42 @@ +import unittest +from unittest.mock import Mock, patch +import json +import psutil +from src.system_info_texx.memory import Memory, Utils + + +class TestMemory(unittest.TestCase): + @patch('psutil.virtual_memory', return_value=(1024 * 1024 * 1024, 512 * 1024 * 1024, 50.0, 256 * 1024 * 1024, 256 * 1024 * 1024)) + def test_init(self, mock_virtual_memory): + # Create a Memory instance + memory = Memory() + + # Check if the attributes are set correctly + self.assertEqual(memory.unit, "MB") + self.assertEqual(memory.total, 1024) # 1024 MB = 1 GB + self.assertEqual(memory.available, 512) # 512 MB + self.assertEqual(memory.percentage, 50.0) + self.assertEqual(memory.used, 256) # 256 MB + self.assertEqual(memory.free, 256) # 256 MB + + def test_serialize(self): + # Set up values for the Memory instance + memory = Memory() + memory.unit = "MB" + memory.total = 1024 + memory.available = 512 + memory.percentage = 50.0 + memory.used = 256 + memory.free = 256 + + # Call the serialize method + result = memory.serialize() + + # Check if the result matches the expected JSON string + expected_result = '{"unit": "MB", "total": 1024, "available": 512, "percentage": 50.0, "used": 256, "free": 256}' + self.assertEqual(result, expected_result) + + +if __name__ == '__main__': + unittest.main() + diff --git a/tests/test_system_info.py b/tests/test_system_info.py index b49a283..275a91c 100644 --- a/tests/test_system_info.py +++ b/tests/test_system_info.py @@ -1,60 +1,64 @@ -from unittest import TestCase +import unittest +from unittest.mock import Mock, patch +from src.system_info_texx.system_info import SimpleSystemInfo, ExpandedSystemInfo, Cpu, Memory, Disk -from src.system_info_texx.cpu import Cpu -from src.system_info_texx.disk import Disk -from src.system_info_texx.memory import Memory - - -class SystemInfoTest(TestCase): - - disk: Disk - memory: Memory - cpu: Cpu +class TestSimpleSystemInfo(unittest.TestCase): def setUp(self): - self.disk = Disk() - self.memory = Memory() - self.cpu = Cpu() - - def test_disk_info_serialization(self): - serialized = self.disk.serialize() - self.assertTrue("unit" in serialized) - self.assertTrue("total" in serialized) - self.assertTrue("used" in serialized) - self.assertTrue("free" in serialized) - self.assertTrue("percentage" in serialized) - - def test_disk_info_default(self): - self.assertEqual(self.disk.unit, "GB") - - def test_disk_info_mb(self): - self.disk = Disk(unit="MB") - - self.assertEqual(self.disk.unit, "MB") - - def test_disk_info_kb(self): - self.disk = Disk(unit="KB") - - self.assertEqual(self.disk.unit, "KB") - - def test_disk_info_unknown(self): - self.disk = Disk(unit="unknown") - - self.assertEqual(self.disk.unit, "B") - - def test_memory_info(self): - self.assertEqual(self.memory.unit, "MB") - - def test_memory_serialization(self): - serialized = self.memory.serialize() - self.assertTrue("unit" in serialized) - self.assertTrue("total" in serialized) - self.assertTrue("available" in serialized) - self.assertTrue("percentage" in serialized) - self.assertTrue("used" in serialized) - self.assertTrue("free" in serialized) - - def test_cpu_info(self): - print(self.cpu.serialize()) - - self.assertEqual(self.cpu.unit, "°C") + # Create mock objects for Cpu, Memory, and Disk + self.mock_cpu = Mock(spec=Cpu) + self.mock_memory = Mock(spec=Memory) + self.mock_disk = Mock(spec=Disk) + + # Create a SimpleSystemInfo instance with the mock objects + self.system_info = SimpleSystemInfo() + self.system_info.cpu = self.mock_cpu + self.system_info.memory = self.mock_memory + self.system_info.disk = self.mock_disk + + def test_serialize(self): + # Set up the return values for the serialize methods of Cpu, Memory, and Disk + self.mock_cpu.serialize.return_value = '{"cpu_info": "mocked"}' + self.mock_memory.serialize.return_value = '{"memory_info": "mocked"}' + self.mock_disk.serialize.return_value = '{"disk_info": "mocked"}' + + # Call the serialize method of SimpleSystemInfo + result = self.system_info.serialize() + + # Check if the result matches the expected JSON string + expected_result = '{"cpu": {"cpu_info": "mocked"}, "memory": {"memory_info": "mocked"}, "disk": {"disk_info": "mocked"}}' + self.assertEqual(result, expected_result) + + # Check if the serialize methods of Cpu, Memory, and Disk were called + self.mock_cpu.serialize.assert_called_once() + self.mock_memory.serialize.assert_called_once() + self.mock_disk.serialize.assert_called_once() + + +class TestExpandedSystemInfo(unittest.TestCase): + + def test_serialize(self): + # Set up values for the ExpandedSystemInfo instance + expanded_sys_info = ExpandedSystemInfo() + expanded_sys_info.cpu_temp = 42.0 + expanded_sys_info.cpu_perc = 75.0 + expanded_sys_info.tot_mem = 1024 + expanded_sys_info.ava_mem = 512 + expanded_sys_info.per_mem = 50.0 + expanded_sys_info.use_mem = 512 + expanded_sys_info.fre_mem = 512 + expanded_sys_info.tot_disk = 2048 + expanded_sys_info.use_disk = 1024 + expanded_sys_info.fre_disk = 1024 + expanded_sys_info.per_disk = 50.0 + + # Call the serialize method + result = expanded_sys_info.serialize() + + # Check if the result matches the expected JSON string + expected_result = '{"cpu_temp": 42.0, "cpu_perc": 75.0, "tot_mem": 1024, "ava_mem": 512, "per_mem": 50.0, "use_mem": 512, "fre_mem": 512, "tot_disk": 2048, "use_disk": 1024, "fre_disk": 1024, "per_disk": 50.0}' + self.assertEqual(result, expected_result) + + +if __name__ == '__main__': + unittest.main() From cedb03510e6ee234e2ecaa6911c3224b18e2ecbe Mon Sep 17 00:00:00 2001 From: "marco.tessari" Date: Mon, 22 Jan 2024 14:53:35 +0100 Subject: [PATCH 3/6] feat: base sys class to extract dictionary with attributes --- pyproject.toml | 2 +- src/system_info_texx/base_sys_info.py | 11 +++++++++++ src/system_info_texx/cpu.py | 6 ++++-- src/system_info_texx/disk.py | 5 +++-- src/system_info_texx/memory.py | 5 +++-- 5 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 src/system_info_texx/base_sys_info.py diff --git a/pyproject.toml b/pyproject.toml index 3998fef..ece65c8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "system_info_texx" -version = "0.0.5" +version = "0.0.6" authors = [ { name="Marco Tessari", email="marco.tex@live.com" }, ] diff --git a/src/system_info_texx/base_sys_info.py b/src/system_info_texx/base_sys_info.py new file mode 100644 index 0000000..fafd25d --- /dev/null +++ b/src/system_info_texx/base_sys_info.py @@ -0,0 +1,11 @@ +from abc import abstractmethod, ABC + + +class _BaseSysInfo(ABC): + + def get_all_attributes(self): + return self.__dict__ + + @abstractmethod + def serialize(self) -> str: + pass diff --git a/src/system_info_texx/cpu.py b/src/system_info_texx/cpu.py index 479cfaf..31b9a2e 100644 --- a/src/system_info_texx/cpu.py +++ b/src/system_info_texx/cpu.py @@ -4,8 +4,10 @@ import psutil import json +from .base_sys_info import _BaseSysInfo -class Cpu: + +class Cpu(_BaseSysInfo): __sensor_file = '/sys/class/thermal/thermal_zone0/temp' @@ -25,4 +27,4 @@ def __init__(self) -> None: self.unit = "°C" def serialize(self) -> str: - return json.dumps(self.__dict__, ensure_ascii=False) + return json.dumps(self.get_all_attributes(), ensure_ascii=False) diff --git a/src/system_info_texx/disk.py b/src/system_info_texx/disk.py index 5692cd1..d0f2f8d 100644 --- a/src/system_info_texx/disk.py +++ b/src/system_info_texx/disk.py @@ -2,9 +2,10 @@ import json from .utils import Utils +from .base_sys_info import _BaseSysInfo -class Disk: +class Disk(_BaseSysInfo): def __init__(self, unit="GB") -> None: disk_usage = psutil.disk_usage("/") @@ -19,4 +20,4 @@ def __init__(self, unit="GB") -> None: self.percentage = disk_usage[3] def serialize(self) -> str: - return json.dumps(self.__dict__) + return json.dumps(self.get_all_attributes()) diff --git a/src/system_info_texx/memory.py b/src/system_info_texx/memory.py index 65cfff3..67917f9 100644 --- a/src/system_info_texx/memory.py +++ b/src/system_info_texx/memory.py @@ -2,9 +2,10 @@ import psutil from .utils import Utils +from .base_sys_info import _BaseSysInfo -class Memory: +class Memory(_BaseSysInfo): def __init__(self) -> None: memory = psutil.virtual_memory() @@ -16,4 +17,4 @@ def __init__(self) -> None: self.free = Utils.convert_byte_to_megabyte(memory[4]) def serialize(self) -> str: - return json.dumps(self.__dict__) + return json.dumps(self.get_all_attributes()) From 0033e16b2b4e7e6e72ceb5672600b13c7c7b1c05 Mon Sep 17 00:00:00 2001 From: Texx Date: Mon, 22 Jan 2024 22:13:03 +0100 Subject: [PATCH 4/6] wip: add pretty format for system information. --- src/system_info_texx/base_sys_info.py | 4 ++++ src/system_info_texx/cpu.py | 7 ++++++ src/system_info_texx/disk.py | 9 ++++++++ src/system_info_texx/memory.py | 10 ++++++++ src/system_info_texx/system_info.py | 33 +++++++++++++++++++++++++++ tests/test_system_info.py | 10 ++++++++ 6 files changed, 73 insertions(+) diff --git a/src/system_info_texx/base_sys_info.py b/src/system_info_texx/base_sys_info.py index fafd25d..2370fc9 100644 --- a/src/system_info_texx/base_sys_info.py +++ b/src/system_info_texx/base_sys_info.py @@ -9,3 +9,7 @@ def get_all_attributes(self): @abstractmethod def serialize(self) -> str: pass + + @abstractmethod + def format_pretty(self) -> str: + pass \ No newline at end of file diff --git a/src/system_info_texx/cpu.py b/src/system_info_texx/cpu.py index 31b9a2e..4979dc0 100644 --- a/src/system_info_texx/cpu.py +++ b/src/system_info_texx/cpu.py @@ -28,3 +28,10 @@ def __init__(self) -> None: def serialize(self) -> str: return json.dumps(self.get_all_attributes(), ensure_ascii=False) + + def format_pretty(self) -> str: + output = "CPU : \n" + output += "Temperature : " + str(self.temperature) + " " + self.unit + "\n" + output += "Percentage : " + str(self.percentage) + " %\n" + + return output diff --git a/src/system_info_texx/disk.py b/src/system_info_texx/disk.py index d0f2f8d..90fc43f 100644 --- a/src/system_info_texx/disk.py +++ b/src/system_info_texx/disk.py @@ -21,3 +21,12 @@ def __init__(self, unit="GB") -> None: def serialize(self) -> str: return json.dumps(self.get_all_attributes()) + + def format_pretty(self) -> str: + output = "Disk: \n" + output += "Total : " + str(self.total) + " " + self.unit + "\n" + output += "Used : " + str(self.used) + " " + self.unit + "\n" + output += "Free : " + str(self.free) + " " + self.unit + "\n" + output += "Percentage : " + str(self.percentage) + " %" + + return output diff --git a/src/system_info_texx/memory.py b/src/system_info_texx/memory.py index 67917f9..40a09ae 100644 --- a/src/system_info_texx/memory.py +++ b/src/system_info_texx/memory.py @@ -18,3 +18,13 @@ def __init__(self) -> None: def serialize(self) -> str: return json.dumps(self.get_all_attributes()) + + def format_pretty(self) -> str: + output = "Memory: \n" + output += "Total : " + str(self.total) + " " + self.unit + "\n" + output += "Available : " + str(self.available) + " " + self.unit + "\n" + output += "Used : " + str(self.used) + " " + self.unit + "\n" + output += "Free : " + str(self.free) + " " + self.unit + "\n" + output += "Percentage : " + str(self.percentage) + " %\n" + + return output diff --git a/src/system_info_texx/system_info.py b/src/system_info_texx/system_info.py index 2adcc0b..c0699d9 100644 --- a/src/system_info_texx/system_info.py +++ b/src/system_info_texx/system_info.py @@ -11,6 +11,10 @@ class SystemInfo(ABC): def serialize(self) -> str: pass + @abstractmethod + def format_pretty(self) -> str: + pass + class SimpleSystemInfo(SystemInfo): def __init__(self): @@ -21,6 +25,15 @@ def __init__(self): def serialize(self) -> str: return f'{{"cpu": {self.cpu.serialize()}, "memory": {self.memory.serialize()}, "disk": {self.disk.serialize()}}}' + def format_pretty(self) -> str: + output = self.cpu.format_pretty() + output += "----------------------\n" + output += self.memory.format_pretty() + output += "----------------------\n" + output += self.disk.format_pretty() + + return output + class ExpandedSystemInfo(SystemInfo): def __init__(self): @@ -44,3 +57,23 @@ def __init__(self): def serialize(self) -> str: return json.dumps(self.__dict__) + + def format_pretty(self) -> str: + output = "CPU: \n" + output += "Temperature : " + str(self.cpu_temp) + " °C\n" + output += "Percentage : " + str(self.cpu_perc) + " %\n" + output += "----------------------\n" + output += "Memory: \n" + output += "Total : " + str(self.tot_mem) + " MB\n" + output += "Available : " + str(self.ava_mem) + " MB\n" + output += "Used : " + str(self.use_mem) + " MB\n" + output += "Free : " + str(self.fre_mem) + " MB\n" + output += "Percentage : " + str(self.per_mem) + " %\n" + output += "----------------------\n" + output += "Disk: \n" + output += "Total : " + str(self.tot_disk) + " GB\n" + output += "Used : " + str(self.use_disk) + " GB\n" + output += "Free : " + str(self.fre_disk) + " GB\n" + output += "Percentage : " + str(self.per_disk) + " %\n" + + return output diff --git a/tests/test_system_info.py b/tests/test_system_info.py index 275a91c..5b0a2d4 100644 --- a/tests/test_system_info.py +++ b/tests/test_system_info.py @@ -34,6 +34,11 @@ def test_serialize(self): self.mock_memory.serialize.assert_called_once() self.mock_disk.serialize.assert_called_once() + # TODO: make test for pretty format + def test_format_pretty(self): + sys_info = SimpleSystemInfo() + print(sys_info.format_pretty()) + class TestExpandedSystemInfo(unittest.TestCase): @@ -59,6 +64,11 @@ def test_serialize(self): expected_result = '{"cpu_temp": 42.0, "cpu_perc": 75.0, "tot_mem": 1024, "ava_mem": 512, "per_mem": 50.0, "use_mem": 512, "fre_mem": 512, "tot_disk": 2048, "use_disk": 1024, "fre_disk": 1024, "per_disk": 50.0}' self.assertEqual(result, expected_result) + # TODO: make test for pretty format + def test_format_pretty(self): + sys_info = ExpandedSystemInfo() + print(sys_info.format_pretty()) + if __name__ == '__main__': unittest.main() From 1b8fba6c410131769e56e974054137534558e998 Mon Sep 17 00:00:00 2001 From: "marco.tessari" Date: Tue, 23 Jan 2024 19:32:22 +0100 Subject: [PATCH 5/6] test: format pretty for sys info --- src/system_info_texx/system_info.py | 43 +++++++++--------- tests/test_system_info.py | 69 ++++++++++++++++++++--------- 2 files changed, 72 insertions(+), 40 deletions(-) diff --git a/src/system_info_texx/system_info.py b/src/system_info_texx/system_info.py index c0699d9..8c12b03 100644 --- a/src/system_info_texx/system_info.py +++ b/src/system_info_texx/system_info.py @@ -43,37 +43,40 @@ def __init__(self): self.cpu_temp = cpu.temperature self.cpu_perc = cpu.percentage + self.cpu_unit = cpu.unit - self.tot_mem = memory.total - self.ava_mem = memory.available - self.per_mem = memory.percentage - self.use_mem = memory.used - self.fre_mem = memory.free + self.mem_tot = memory.total + self.mem_ava = memory.available + self.mem_use = memory.used + self.mem_fre = memory.free + self.mem_uni = memory.unit + self.mem_per = memory.percentage - self.tot_disk = disk.total - self.use_disk = disk.used - self.fre_disk = disk.free - self.per_disk = disk.percentage + self.disk_tot = disk.total + self.disk_use = disk.used + self.disk_fre = disk.free + self.disk_uni = disk.unit + self.disk_per = disk.percentage def serialize(self) -> str: - return json.dumps(self.__dict__) + return json.dumps(self.__dict__, ensure_ascii=False) def format_pretty(self) -> str: output = "CPU: \n" - output += "Temperature : " + str(self.cpu_temp) + " °C\n" + output += "Temperature : " + str(self.cpu_temp) + " " + self.cpu_unit + "\n" output += "Percentage : " + str(self.cpu_perc) + " %\n" output += "----------------------\n" output += "Memory: \n" - output += "Total : " + str(self.tot_mem) + " MB\n" - output += "Available : " + str(self.ava_mem) + " MB\n" - output += "Used : " + str(self.use_mem) + " MB\n" - output += "Free : " + str(self.fre_mem) + " MB\n" - output += "Percentage : " + str(self.per_mem) + " %\n" + output += "Total : " + str(self.mem_tot) + " " + self.mem_uni + "\n" + output += "Available : " + str(self.mem_ava) + " " + self.mem_uni + "\n" + output += "Used : " + str(self.mem_use) + " " + self.mem_uni + "\n" + output += "Free : " + str(self.mem_fre) + " " + self.mem_uni + "\n" + output += "Percentage : " + str(self.mem_per) + " %\n" output += "----------------------\n" output += "Disk: \n" - output += "Total : " + str(self.tot_disk) + " GB\n" - output += "Used : " + str(self.use_disk) + " GB\n" - output += "Free : " + str(self.fre_disk) + " GB\n" - output += "Percentage : " + str(self.per_disk) + " %\n" + output += "Total : " + str(self.disk_tot) + " " + self.disk_uni + "\n" + output += "Used : " + str(self.disk_use) + " " + self.disk_uni + "\n" + output += "Free : " + str(self.disk_fre) + " " + self.disk_uni + "\n" + output += "Percentage : " + str(self.disk_per) + " %\n" return output diff --git a/tests/test_system_info.py b/tests/test_system_info.py index 5b0a2d4..131a604 100644 --- a/tests/test_system_info.py +++ b/tests/test_system_info.py @@ -36,38 +36,67 @@ def test_serialize(self): # TODO: make test for pretty format def test_format_pretty(self): - sys_info = SimpleSystemInfo() - print(sys_info.format_pretty()) + self.mock_cpu.format_pretty.return_value = "CPU Details\n" + self.mock_memory.format_pretty.return_value = "Memory Details\n" + self.mock_disk.format_pretty.return_value = "Disk Details" + + formatted_result = self.system_info.format_pretty() + # Add your assertion here based on the expected formatted result + expected_result = "CPU Details\n----------------------\nMemory Details\n----------------------\nDisk Details" + self.assertEqual(formatted_result, expected_result) class TestExpandedSystemInfo(unittest.TestCase): - def test_serialize(self): - # Set up values for the ExpandedSystemInfo instance - expanded_sys_info = ExpandedSystemInfo() - expanded_sys_info.cpu_temp = 42.0 - expanded_sys_info.cpu_perc = 75.0 - expanded_sys_info.tot_mem = 1024 - expanded_sys_info.ava_mem = 512 - expanded_sys_info.per_mem = 50.0 - expanded_sys_info.use_mem = 512 - expanded_sys_info.fre_mem = 512 - expanded_sys_info.tot_disk = 2048 - expanded_sys_info.use_disk = 1024 - expanded_sys_info.fre_disk = 1024 - expanded_sys_info.per_disk = 50.0 + def setUp(self): + self.expanded_sys_info = ExpandedSystemInfo() + self.expanded_sys_info.cpu_temp = 42.0 + self.expanded_sys_info.cpu_perc = 75.0 + self.expanded_sys_info.cpu_unit = "°C" + self.expanded_sys_info.mem_tot = 1024 + self.expanded_sys_info.mem_ava = 512 + self.expanded_sys_info.mem_use = 512 + self.expanded_sys_info.mem_fre = 512 + self.expanded_sys_info.mem_uni = "MB" + self.expanded_sys_info.mem_per = 50.0 + self.expanded_sys_info.disk_tot = 2048 + self.expanded_sys_info.disk_use = 1024 + self.expanded_sys_info.disk_fre = 1024 + self.expanded_sys_info.disk_uni = "GB" + self.expanded_sys_info.disk_per = 50.0 + def test_serialize(self): # Call the serialize method - result = expanded_sys_info.serialize() + result = self.expanded_sys_info.serialize() # Check if the result matches the expected JSON string - expected_result = '{"cpu_temp": 42.0, "cpu_perc": 75.0, "tot_mem": 1024, "ava_mem": 512, "per_mem": 50.0, "use_mem": 512, "fre_mem": 512, "tot_disk": 2048, "use_disk": 1024, "fre_disk": 1024, "per_disk": 50.0}' + expected_result = ('{"cpu_temp": 42.0, "cpu_perc": 75.0, "cpu_unit": "°C", ' + '"mem_tot": 1024, "mem_ava": 512, "mem_use": 512, "mem_fre": 512, "mem_uni": "MB", "mem_per": 50.0, ' + '"disk_tot": 2048, "disk_use": 1024, "disk_fre": 1024, "disk_uni": "GB", "disk_per": 50.0}') self.assertEqual(result, expected_result) # TODO: make test for pretty format def test_format_pretty(self): - sys_info = ExpandedSystemInfo() - print(sys_info.format_pretty()) + formatted_result = self.expanded_sys_info.format_pretty() + + # Add your assertion here based on the expected formatted result + expected_result = ("CPU: \n" + "Temperature : 42.0 °C\n" + "Percentage : 75.0 %\n" + "----------------------\n" + "Memory: \n" + "Total : 1024 MB\n" + "Available : 512 MB\n" + "Used : 512 MB\n" + "Free : 512 MB\n" + "Percentage : 50.0 %\n" + "----------------------\n" + "Disk: \n" + "Total : 2048 GB\n" + "Used : 1024 GB\n" + "Free : 1024 GB\n" + "Percentage : 50.0 %\n") + self.assertEqual(formatted_result, expected_result) if __name__ == '__main__': From 744f7bdd3ff2d1d6f1df51aaba49c379a1d4e5f6 Mon Sep 17 00:00:00 2001 From: "marco.tessari" Date: Tue, 23 Jan 2024 20:12:12 +0100 Subject: [PATCH 6/6] chore: long separator for pretty format --- pyproject.toml | 2 +- src/system_info_texx/system_info.py | 8 ++++---- tests/test_system_info.py | 10 +++++++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ece65c8..263aa59 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "system_info_texx" -version = "0.0.6" +version = "0.0.8" authors = [ { name="Marco Tessari", email="marco.tex@live.com" }, ] diff --git a/src/system_info_texx/system_info.py b/src/system_info_texx/system_info.py index 8c12b03..d0c4174 100644 --- a/src/system_info_texx/system_info.py +++ b/src/system_info_texx/system_info.py @@ -27,9 +27,9 @@ def serialize(self) -> str: def format_pretty(self) -> str: output = self.cpu.format_pretty() - output += "----------------------\n" + output += "--------------------------------------------\n" output += self.memory.format_pretty() - output += "----------------------\n" + output += "--------------------------------------------\n" output += self.disk.format_pretty() return output @@ -65,14 +65,14 @@ def format_pretty(self) -> str: output = "CPU: \n" output += "Temperature : " + str(self.cpu_temp) + " " + self.cpu_unit + "\n" output += "Percentage : " + str(self.cpu_perc) + " %\n" - output += "----------------------\n" + output += "--------------------------------------------\n" output += "Memory: \n" output += "Total : " + str(self.mem_tot) + " " + self.mem_uni + "\n" output += "Available : " + str(self.mem_ava) + " " + self.mem_uni + "\n" output += "Used : " + str(self.mem_use) + " " + self.mem_uni + "\n" output += "Free : " + str(self.mem_fre) + " " + self.mem_uni + "\n" output += "Percentage : " + str(self.mem_per) + " %\n" - output += "----------------------\n" + output += "--------------------------------------------\n" output += "Disk: \n" output += "Total : " + str(self.disk_tot) + " " + self.disk_uni + "\n" output += "Used : " + str(self.disk_use) + " " + self.disk_uni + "\n" diff --git a/tests/test_system_info.py b/tests/test_system_info.py index 131a604..a1f7adf 100644 --- a/tests/test_system_info.py +++ b/tests/test_system_info.py @@ -42,7 +42,11 @@ def test_format_pretty(self): formatted_result = self.system_info.format_pretty() # Add your assertion here based on the expected formatted result - expected_result = "CPU Details\n----------------------\nMemory Details\n----------------------\nDisk Details" + expected_result = ("CPU Details\n" + "--------------------------------------------\n" + "Memory Details\n" + "--------------------------------------------\n" + "Disk Details") self.assertEqual(formatted_result, expected_result) @@ -83,14 +87,14 @@ def test_format_pretty(self): expected_result = ("CPU: \n" "Temperature : 42.0 °C\n" "Percentage : 75.0 %\n" - "----------------------\n" + "--------------------------------------------\n" "Memory: \n" "Total : 1024 MB\n" "Available : 512 MB\n" "Used : 512 MB\n" "Free : 512 MB\n" "Percentage : 50.0 %\n" - "----------------------\n" + "--------------------------------------------\n" "Disk: \n" "Total : 2048 GB\n" "Used : 1024 GB\n"