From 5d51398e866725cc10dbece35cc03f24ee7a7797 Mon Sep 17 00:00:00 2001 From: David Fernandez Date: Tue, 2 Apr 2024 16:13:23 +0100 Subject: [PATCH 1/3] [OPT-1116] More coverage in tests for to_number util function --- sl_util/tests/unit/test_str_utils.py | 67 ++++++++++++++-------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/sl_util/tests/unit/test_str_utils.py b/sl_util/tests/unit/test_str_utils.py index fd8929aa..565577cd 100644 --- a/sl_util/tests/unit/test_str_utils.py +++ b/sl_util/tests/unit/test_str_utils.py @@ -1,5 +1,6 @@ from pytest import mark - +from random import randint +from unittest.mock import patch from sl_util.sl_util.str_utils import deterministic_uuid, to_number @@ -29,41 +30,39 @@ def test_deterministic_uuid_without_source(self, source): # Then we obtain two different values assert uuid1 != uuid2 - @mark.parametrize('source', [0, '0', 'zero']) - def test_number_conversions_to_zero(self, source): - # Given the source - # when passed 0 to function - number1 = to_number(source) - # when passed '0' to function - number2 = to_number(source) - # when passed 'zero' to function - number3 = to_number(source) - # Then we obtain 0 - assert number1 == number2 == number3 == 0 + @mark.parametrize('source', [randint(0, 100), str(randint(0, 100))]) + def test_to_number(self, source): + # GIVEN a random integer - @mark.parametrize('source', [1, '1', 'one']) - def test_number_conversions_to_one(self, source): - # Given the source - # when passed 1 to function - number1 = to_number(source) - # when passed '1' to function - number2 = to_number(source) - # when passed 'one' to function - number3 = to_number(source) - # Then we obtain 1 - assert number1 == number2 == number3 == 1 + # WHEN it is transformed to a number + result = to_number(source) - @mark.parametrize('source', [2, '2', 'two']) - def test_number_conversions_to_two(self, source): - # Given the source - # when passed 2 to function - number1 = to_number(source) - # when passed '2' to function - number2 = to_number(source) - # when passed 'two' to function - number3 = to_number(source) - # Then we obtain 2 - assert number1 == number2 == number3 == 2 + # THEN we obtain the original number + assert result == int(source) + + @patch('sl_util.sl_util.str_utils.w2n.word_to_num', return_value=2) + def test_text_to_number(self, mocked_word_to_otm): + # GIVEN a text number + source = 'two' + + # WHEN it is transformed to a number + result = to_number(source) + + # THEN we obtain the number + assert result == 2 + + def test_unknown_to_number(self): + # GIVEN an unkown + source = 'unknown' + + # AND a default value + default_value = 5 + + # WHEN it is transformed to a number + result = to_number(source, default_value) + + # THEN we obtain the default value + assert result == 5 @mark.parametrize('source', ['sandbox', '']) def test_number_conversions_to_alphanumeric(self, source): From c65082de0a4f54aeeaca0f1de902ae27122bd98a Mon Sep 17 00:00:00 2001 From: David Fernandez Date: Tue, 2 Apr 2024 16:24:53 +0100 Subject: [PATCH 2/3] [OPT-1116] fix coverage --- sl_util/tests/unit/test_str_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sl_util/tests/unit/test_str_utils.py b/sl_util/tests/unit/test_str_utils.py index 565577cd..99585542 100644 --- a/sl_util/tests/unit/test_str_utils.py +++ b/sl_util/tests/unit/test_str_utils.py @@ -1,5 +1,5 @@ from pytest import mark -from random import randint +import random from unittest.mock import patch from sl_util.sl_util.str_utils import deterministic_uuid, to_number @@ -30,7 +30,7 @@ def test_deterministic_uuid_without_source(self, source): # Then we obtain two different values assert uuid1 != uuid2 - @mark.parametrize('source', [randint(0, 100), str(randint(0, 100))]) + @mark.parametrize('source', [random.randint(0, 100), str(random.randint(0, 100))]) def test_to_number(self, source): # GIVEN a random integer From 7304991ce675337dcd8f0673d1123574ef6d89c7 Mon Sep 17 00:00:00 2001 From: David Fernandez Date: Tue, 2 Apr 2024 16:38:14 +0100 Subject: [PATCH 3/3] [OPT-1116] fix coverage (2) --- sl_util/tests/unit/test_str_utils.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sl_util/tests/unit/test_str_utils.py b/sl_util/tests/unit/test_str_utils.py index 99585542..9430afad 100644 --- a/sl_util/tests/unit/test_str_utils.py +++ b/sl_util/tests/unit/test_str_utils.py @@ -1,4 +1,4 @@ -from pytest import mark +from pytest import mark, param import random from unittest.mock import patch from sl_util.sl_util.str_utils import deterministic_uuid, to_number @@ -30,8 +30,11 @@ def test_deterministic_uuid_without_source(self, source): # Then we obtain two different values assert uuid1 != uuid2 - @mark.parametrize('source', [random.randint(0, 100), str(random.randint(0, 100))]) - def test_to_number(self, source): + @mark.parametrize('source', [ + param(random.randint(0, 100)), + param(str(random.randint(0, 100))) + ]) + def test_to_number(self, source: any): # GIVEN a random integer # WHEN it is transformed to a number