Skip to content

Commit

Permalink
move functions to helpers instead of constants; add unit tests and im…
Browse files Browse the repository at this point in the history
…prove function
  • Loading branch information
0x7878 committed Nov 15, 2024
1 parent 6a43f11 commit 1ca51d2
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 39 deletions.
28 changes: 9 additions & 19 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
},
"python.formatting.provider": "autopep8",
"autopep8.args": [
"--max-line-length",
"120"
],
"editor.formatOnSave": true,
"python.testing.unittestArgs": [
"-v",
"-s",
".",
"-p",
"test_*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true,
}
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
},
"autopep8.args": ["--max-line-length", "120"],
"editor.formatOnSave": true,
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true,
"python.testing.unittestArgs": ["-v", "-s", "./tests", "-p", "test_*"]
}
20 changes: 1 addition & 19 deletions constants.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
'''Global constants'''

# region formatting helping functions (used in constant)


def _kwh(_p, value: float) -> str:
return f"{round(value, 2)}KWh"


def _a(_p, value: float) -> str:
return f"{round(value, 1)}A"


def _w(_p, value: float) -> str:
return f"{round(value, 1)}W"


def _v(_p, value: float) -> str:
return f"{round(value, 1)}V"
# endregion

from helpers import _kwh, _a, _w, _v

DTUVARIANT_AHOY = "ahoy"
DTUVARIANT_OPENDTU = "opendtu"
Expand Down
18 changes: 18 additions & 0 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@
import logging


# region formatting helping functions (used in constant)
def _kwh(_p, value: float) -> str:
return f"{value:.2f}KWh"


def _a(_p, value: float) -> str:
return f"{value:.1f}A"


def _w(_p, value: float) -> str:
return f"{value:.1f}W"


def _v(_p, value: float) -> str:
return f"{value:.1f}V"
# endregion


def get_config_value(config, name, inverter_or_template, inverter_or_tpl_number, defaultvalue=None):
'''check if config value exist in current inverter/template's section, otherwise throw error'''
if name in config[f"{inverter_or_template}{inverter_or_tpl_number}"]:
Expand Down
42 changes: 41 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
convert_to_expected_type,
get_ahoy_field_by_name,
is_true,
timeit
timeit,
_kwh,
_a,
_w,
_v,
)
sys.modules['vedbus'] = MagicMock()
sys.modules['dbus'] = MagicMock()
Expand Down Expand Up @@ -228,6 +232,42 @@ def test_part_get_values_for_inverts(self):
self.assertEqual(voltage, 225.66)
self.assertEqual(current, None)

def test_kwh(self):
''' Test the _kwh() function. '''
self.assertEqual(_kwh(None, 123.456), "123.46KWh")
self.assertEqual(_kwh(None, 1.234), "1.23KWh")
self.assertEqual(_kwh(None, -1.234), "-1.23KWh")
self.assertEqual(_kwh(None, 0), "0.00KWh")
self.assertEqual(_kwh(None, 0.1234), "0.12KWh")
self.assertEqual(_kwh(None, 1.5678), "1.57KWh")

def test_a(self):
''' Test the _a() function. '''
self.assertEqual(_a(None, 0), "0.0A")
self.assertEqual(_a(None, 0.45), "0.5A")
self.assertEqual(_a(None, 0.459), "0.5A")
self.assertEqual(_a(None, 1.2345), "1.2A")
self.assertEqual(_a(None, 1.5678), "1.6A")
self.assertEqual(_a(None, -1.5678), "-1.6A")

def test_w(self):
''' Test the _w() function. '''
self.assertEqual(_w(None, 0), "0.0W")
self.assertEqual(_w(None, 0.45), "0.5W")
self.assertEqual(_w(None, 0.459), "0.5W")
self.assertEqual(_w(None, 1.2345), "1.2W")
self.assertEqual(_w(None, 1.5678), "1.6W")
self.assertEqual(_w(None, -1.5678), "-1.6W")

def test_v(self):
''' Test the _v() function. '''
self.assertEqual(_v(None, 0), "0.0V")
self.assertEqual(_v(None, 0.45), "0.5V")
self.assertEqual(_v(None, 0.459), "0.5V")
self.assertEqual(_v(None, 1.2345), "1.2V")
self.assertEqual(_v(None, 1.5678), "1.6V")
self.assertEqual(_v(None, -1.5678), "-1.6V")


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

0 comments on commit 1ca51d2

Please sign in to comment.