From 75fde0f4449f732a71b51c3faeed598a32ba8f16 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 7 Apr 2023 14:52:17 +0200 Subject: [PATCH] Added pipe tests --- src/cpl_core/pipes/ip_address_pipe.py | 2 +- unittests/unittests_core/core_test_suite.py | 19 +++++++++++++++--- unittests/unittests_core/pipes/__init__.py | 1 + .../pipes/bool_pipe_test_case.py | 14 +++++++++++++ .../pipes/ip_address_pipe_test_case.py | 20 +++++++++++++++++++ .../pipes/version_pipe_test_case.py | 16 +++++++++++++++ 6 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 unittests/unittests_core/pipes/__init__.py create mode 100644 unittests/unittests_core/pipes/bool_pipe_test_case.py create mode 100644 unittests/unittests_core/pipes/ip_address_pipe_test_case.py create mode 100644 unittests/unittests_core/pipes/version_pipe_test_case.py diff --git a/src/cpl_core/pipes/ip_address_pipe.py b/src/cpl_core/pipes/ip_address_pipe.py index 00dc43b0..71e5c0b4 100644 --- a/src/cpl_core/pipes/ip_address_pipe.py +++ b/src/cpl_core/pipes/ip_address_pipe.py @@ -13,7 +13,7 @@ class IPAddressPipe(PipeABC): for i in range(0, len(value)): byte = value[i] - if byte > 255: + if byte > 255 or byte < 0: raise Exception("Invalid IP") if i == len(value) - 1: diff --git a/unittests/unittests_core/core_test_suite.py b/unittests/unittests_core/core_test_suite.py index a50d9b90..ede67d9e 100644 --- a/unittests/unittests_core/core_test_suite.py +++ b/unittests/unittests_core/core_test_suite.py @@ -1,5 +1,8 @@ import unittest +from unittests_core.pipes.bool_pipe_test_case import BoolPipeTestCase +from unittests_core.pipes.ip_address_pipe_test_case import IPAddressTestCase +from unittests_core.pipes.version_pipe_test_case import VersionPipeTestCase from unittests_core.utils.credential_manager_test_case import CredentialManagerTestCase from unittests_core.utils.json_processor_test_case import JSONProcessorTestCase from unittests_core.utils.string_test_case import StringTestCase @@ -10,9 +13,19 @@ class CoreTestSuite(unittest.TestSuite): unittest.TestSuite.__init__(self) loader = unittest.TestLoader() - self.addTests(loader.loadTestsFromTestCase(CredentialManagerTestCase)) - self.addTests(loader.loadTestsFromTestCase(JSONProcessorTestCase)) - self.addTests(loader.loadTestsFromTestCase(StringTestCase)) + tests = [ + # pipes + BoolPipeTestCase, + IPAddressTestCase, + VersionPipeTestCase, + # utils + CredentialManagerTestCase, + JSONProcessorTestCase, + StringTestCase, + ] + + for test in tests: + self.addTests(loader.loadTestsFromTestCase(test)) def run(self, *args): super().run(*args) diff --git a/unittests/unittests_core/pipes/__init__.py b/unittests/unittests_core/pipes/__init__.py new file mode 100644 index 00000000..425ab6c1 --- /dev/null +++ b/unittests/unittests_core/pipes/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/unittests/unittests_core/pipes/bool_pipe_test_case.py b/unittests/unittests_core/pipes/bool_pipe_test_case.py new file mode 100644 index 00000000..99af412b --- /dev/null +++ b/unittests/unittests_core/pipes/bool_pipe_test_case.py @@ -0,0 +1,14 @@ +import unittest + +from cpl_core.pipes import BoolPipe + + +class BoolPipeTestCase(unittest.TestCase): + def setUp(self): + pass + + def test_transform(self): + pipe = BoolPipe() + + self.assertEqual("True", pipe.transform(True)) + self.assertEqual("False", pipe.transform(False)) diff --git a/unittests/unittests_core/pipes/ip_address_pipe_test_case.py b/unittests/unittests_core/pipes/ip_address_pipe_test_case.py new file mode 100644 index 00000000..218bdb6a --- /dev/null +++ b/unittests/unittests_core/pipes/ip_address_pipe_test_case.py @@ -0,0 +1,20 @@ +import unittest + +from cpl_core.pipes import IPAddressPipe + + +class IPAddressTestCase(unittest.TestCase): + def setUp(self): + pass + + def test_transform(self): + pipe = IPAddressPipe() + + self.assertEqual("192.168.178.1", pipe.transform([192, 168, 178, 1])) + self.assertEqual("255.255.255.255", pipe.transform([255, 255, 255, 255])) + self.assertEqual("0.0.0.0", pipe.transform([0, 0, 0, 0])) + + self.assertRaises(Exception, lambda: pipe.transform([-192, 168, 178, 1])) + self.assertRaises(Exception, lambda: pipe.transform([256, 168, 178, 1])) + self.assertRaises(Exception, lambda: pipe.transform([256, 168, 178])) + self.assertRaises(Exception, lambda: pipe.transform([256, 168, 178, 1, 1])) diff --git a/unittests/unittests_core/pipes/version_pipe_test_case.py b/unittests/unittests_core/pipes/version_pipe_test_case.py new file mode 100644 index 00000000..2df20d76 --- /dev/null +++ b/unittests/unittests_core/pipes/version_pipe_test_case.py @@ -0,0 +1,16 @@ +import unittest + +from cpl_core.pipes.version_pipe import VersionPipe + + +class VersionPipeTestCase(unittest.TestCase): + def setUp(self): + pass + + def test_transform(self): + pipe = VersionPipe() + + self.assertEqual("1.1.1", pipe.transform({"Major": 1, "Minor": 1, "Micro": 1})) + self.assertEqual("0.1.1", pipe.transform({"Major": 0, "Minor": 1, "Micro": 1})) + self.assertEqual("0.0.1", pipe.transform({"Major": 0, "Minor": 0, "Micro": 1})) + self.assertEqual("0.0.0", pipe.transform({"Major": 0, "Minor": 0, "Micro": 0}))