Test Values From Type

tests.values.test_values_from_type

test_values_from_type.py file for the fejao_config_parser package.

This module contains unit tests for the ValuesFromType class, which is responsible for converting string values to their specified types based on the configuration.

TestValuesFromType

Bases: TestCase

Test suite for the ValuesFromType class.

Source code in tests/values/test_values_from_type.py
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
class TestValuesFromType(unittest.TestCase):
    """Test suite for the ValuesFromType class."""

    def setUp(self):
        """Set up a ValuesFromType instance for testing."""
        self.parser = ValuesFromType(source_name="environment")

    ###
    ### PRIVATE METHODS
    ###

    def test_get_error_msg_is_private(self):
        """Test that __get_error_msg method is private and not directly accessible."""
        with self.assertRaises(AttributeError):
            self.parser.__get_error_msg("string", "value", "error description")

        self.assertTrue(hasattr(self.parser, '_ValuesFromType__get_error_msg'))
        self.assertNotIn('__get_error_msg', dir(self.parser))

    def test_get_from_bool_is_private(self):
        """Test that __get_from_bool method is private and not directly accessible."""
        with self.assertRaises(AttributeError):
            self.parser.__get_from_bool("value")

        self.assertTrue(hasattr(self.parser, '_ValuesFromType__get_from_bool'))
        self.assertNotIn('__get_from_bool', dir(self.parser))

    def test_get_from_int_is_private(self):
        """Test that __get_from_int method is private and not directly accessible."""
        with self.assertRaises(AttributeError):
            self.parser.__get_from_int("value")

        self.assertTrue(hasattr(self.parser, '_ValuesFromType__get_from_int'))
        self.assertNotIn('__get_from_int', dir(self.parser))

    def test_get_from_float_is_private(self):
        """Test that __get_from_float method is private and not directly accessible."""
        with self.assertRaises(AttributeError):
            self.parser.__get_from_float("value")

        self.assertTrue(hasattr(self.parser, '_ValuesFromType__get_from_float'))
        self.assertNotIn('__get_from_float', dir(self.parser))

    def test_get_from_list_is_private(self):
        """Test that __get_from_list method is private and not directly accessible."""
        with self.assertRaises(AttributeError):
            self.parser.__get_from_list("value")

        self.assertTrue(hasattr(self.parser, '_ValuesFromType__get_from_list'))
        self.assertNotIn('__get_from_list', dir(self.parser))

    def test_get_from_dict_is_private(self):
        """Test that __get_from_dict method is private and not directly accessible."""
        with self.assertRaises(AttributeError):
            self.parser.__get_from_dict("value")

        self.assertTrue(hasattr(self.parser, '_ValuesFromType__get_from_dict'))
        self.assertNotIn('__get_from_dict', dir(self.parser))

    ###
    ### TEST VALID VALUE TYPES
    ###

    def test_string_returns_string(self):
        """Test that a string value is returned as-is."""
        self.assertEqual(self.parser.get_value("string", 123), "123")
        self.assertEqual(self.parser.get_value("str", "abc"), "abc")

    def test_bool_true_values(self):
        """Test that various representations of true are correctly parsed as True."""
        for value in ["True", "true", "TRUE", "Yes", "yes", "YES", "On", "on", "ON"]:
            self.assertIs(self.parser.get_value("bool", value), True)

    def test_bool_false_values(self):
        """Test that various representations of false are correctly parsed as False."""
        for value in ["False", "false", "FALSE", "No", "no", "NO", "Off", "off", "OFF"]:
            self.assertIs(self.parser.get_value("bool", value), False)

    def test_bool_value_passed_as_bool(self):
        """Test that boolean values passed as bool are returned as-is."""
        self.assertIs(self.parser.get_value("bool", True), True)
        self.assertIs(self.parser.get_value("bool", False), False)

    def test_int_value_parses_from_string(self):
        """Test that integer values are correctly parsed from strings."""
        self.assertEqual(self.parser.get_value("int", "42"), 42)
        self.assertEqual(self.parser.get_value("integer", "-7"), -7)

    def test_float_value_parses_from_string(self):
        """Test that float values are correctly parsed from strings."""
        self.assertEqual(self.parser.get_value("float", "3.14"), 3.14)
        self.assertEqual(self.parser.get_value("double", "-2.5"), -2.5)

    def test_list_value_returns_list(self):
        """Test that a list value is returned as-is."""
        value = [1, 2, 3]
        self.assertEqual(self.parser.get_value("list", value), value)

    def test_list_value_parses_from_string(self):
        """Test that list values are correctly parsed from strings."""
        self.assertEqual(self.parser.get_value("list", "[1, 2, 3]"), [1, 2, 3])
        # self.assertEqual(self.parser.get_value("list", "['1', '2', '3']"), [1, 2, 3])
        self.assertEqual(self.parser.get_value("list", '[1, 2, 3]'), [1, 2, 3])
        # self.assertEqual(self.parser.get_value("list", '["1", "2", "3"]'), [1, 2, 3])
        self.assertEqual(self.parser.get_value("list", "[a, b]"), ['a', 'b'])
        self.assertEqual(self.parser.get_value("list", "['a', 'b']"), ['a', 'b'])
        self.assertEqual(self.parser.get_value("list", '[a, b]'), ['a', 'b'])
        self.assertEqual(self.parser.get_value("list", '["a", "b"]'), ['a', 'b'])

    def test_list_value_parses_from_string_empty(self):
        """Test that list values are correctly parsed from string of an empty list."""
        self.assertEqual(self.parser.get_value("list", "[ ]"), [])
        self.assertEqual(self.parser.get_value("list", " [ ] "), [])

    def test_dict_value_returns_dict(self):
        """Test that a dict value is returned as-is."""
        value = {"a": 1}
        self.assertEqual(self.parser.get_value("dict", value), value)
        self.assertEqual(self.parser.get_value("dictionary", value), value)

    def test_dict_value_parses_from_string(self):
        """Test that dict values are correctly parsed from strings."""
        self.assertEqual(self.parser.get_value("dict", "{'a': 1}"), {'a': 1})
        self.assertEqual(self.parser.get_value("dictionary", "{'key': 'value'}"), {'key': 'value'})

    def test_unsupported_type_raises(self):
        """Test that an unsupported type raises a ValueError."""
        with self.assertRaises(ValueError) as context:
            self.parser.get_value("unsupported", "value")

        self.assertIn(
            "variable for the type 'unsupported'",
            str(context.exception)
        )

    ###
    ### TEST INVALID VALUE TYPES
    ###

    def test_bool_invalid_value_raises(self):
        """Test that an invalid bool value raises a ValueError."""
        with self.assertRaises(ValueError) as context:
            self.parser.get_value("bool", "notabool")
        self.assertIn("accepted values for boolean types", str(context.exception))

    def test_int_invalid_value_raises(self):
        """Test that an invalid int value raises a ValueError."""
        with self.assertRaises(ValueError) as context:
            self.parser.get_value("int", "3.14")
        self.assertIn("invalid literal for int", str(context.exception))

    def test_float_invalid_value_raises(self):
        """Test that an invalid float value raises a ValueError."""
        with self.assertRaises(ValueError) as context:
            self.parser.get_value("float", "notafloat")
        self.assertIn("could not convert string to float", str(context.exception))

    def test_list_invalid_value_raises(self):
        """Test that an invalid list value raises a SyntaxError."""
        with self.assertRaises(ValueError) as context:
            self.parser.get_value("list", "not a list")
        self.assertIn("Invalid list format", str(context.exception))

    # def test_list_invalid_value_raises(self):
    #     """Test that an invalid list value raises a SyntaxError."""
    #     with self.assertRaises(SyntaxError) as context:
    #         self.parser.get_value("list", "not a list")
    #     self.assertIn("invalid syntax", str(context.exception))

    def test_dict_invalid_value_raises(self):
        """Test that an invalid dict value raises a SyntaxError."""
        with self.assertRaises(SyntaxError) as context:
            self.parser.get_value("dict", "not a dict")
        self.assertIn("invalid syntax", str(context.exception))
Methods:
setUp
setUp()

Set up a ValuesFromType instance for testing.

Source code in tests/values/test_values_from_type.py
17
18
19
def setUp(self):
    """Set up a ValuesFromType instance for testing."""
    self.parser = ValuesFromType(source_name="environment")
test_bool_false_values
test_bool_false_values()

Test that various representations of false are correctly parsed as False.

Source code in tests/values/test_values_from_type.py
87
88
89
90
def test_bool_false_values(self):
    """Test that various representations of false are correctly parsed as False."""
    for value in ["False", "false", "FALSE", "No", "no", "NO", "Off", "off", "OFF"]:
        self.assertIs(self.parser.get_value("bool", value), False)
test_bool_invalid_value_raises
test_bool_invalid_value_raises()

Test that an invalid bool value raises a ValueError.

Source code in tests/values/test_values_from_type.py
153
154
155
156
157
def test_bool_invalid_value_raises(self):
    """Test that an invalid bool value raises a ValueError."""
    with self.assertRaises(ValueError) as context:
        self.parser.get_value("bool", "notabool")
    self.assertIn("accepted values for boolean types", str(context.exception))
test_bool_true_values
test_bool_true_values()

Test that various representations of true are correctly parsed as True.

Source code in tests/values/test_values_from_type.py
82
83
84
85
def test_bool_true_values(self):
    """Test that various representations of true are correctly parsed as True."""
    for value in ["True", "true", "TRUE", "Yes", "yes", "YES", "On", "on", "ON"]:
        self.assertIs(self.parser.get_value("bool", value), True)
test_bool_value_passed_as_bool
test_bool_value_passed_as_bool()

Test that boolean values passed as bool are returned as-is.

Source code in tests/values/test_values_from_type.py
92
93
94
95
def test_bool_value_passed_as_bool(self):
    """Test that boolean values passed as bool are returned as-is."""
    self.assertIs(self.parser.get_value("bool", True), True)
    self.assertIs(self.parser.get_value("bool", False), False)
test_dict_invalid_value_raises
test_dict_invalid_value_raises()

Test that an invalid dict value raises a SyntaxError.

Source code in tests/values/test_values_from_type.py
183
184
185
186
187
def test_dict_invalid_value_raises(self):
    """Test that an invalid dict value raises a SyntaxError."""
    with self.assertRaises(SyntaxError) as context:
        self.parser.get_value("dict", "not a dict")
    self.assertIn("invalid syntax", str(context.exception))
test_dict_value_parses_from_string
test_dict_value_parses_from_string()

Test that dict values are correctly parsed from strings.

Source code in tests/values/test_values_from_type.py
134
135
136
137
def test_dict_value_parses_from_string(self):
    """Test that dict values are correctly parsed from strings."""
    self.assertEqual(self.parser.get_value("dict", "{'a': 1}"), {'a': 1})
    self.assertEqual(self.parser.get_value("dictionary", "{'key': 'value'}"), {'key': 'value'})
test_dict_value_returns_dict
test_dict_value_returns_dict()

Test that a dict value is returned as-is.

Source code in tests/values/test_values_from_type.py
128
129
130
131
132
def test_dict_value_returns_dict(self):
    """Test that a dict value is returned as-is."""
    value = {"a": 1}
    self.assertEqual(self.parser.get_value("dict", value), value)
    self.assertEqual(self.parser.get_value("dictionary", value), value)
test_float_invalid_value_raises
test_float_invalid_value_raises()

Test that an invalid float value raises a ValueError.

Source code in tests/values/test_values_from_type.py
165
166
167
168
169
def test_float_invalid_value_raises(self):
    """Test that an invalid float value raises a ValueError."""
    with self.assertRaises(ValueError) as context:
        self.parser.get_value("float", "notafloat")
    self.assertIn("could not convert string to float", str(context.exception))
test_float_value_parses_from_string
test_float_value_parses_from_string()

Test that float values are correctly parsed from strings.

Source code in tests/values/test_values_from_type.py
102
103
104
105
def test_float_value_parses_from_string(self):
    """Test that float values are correctly parsed from strings."""
    self.assertEqual(self.parser.get_value("float", "3.14"), 3.14)
    self.assertEqual(self.parser.get_value("double", "-2.5"), -2.5)
test_get_error_msg_is_private
test_get_error_msg_is_private()

Test that __get_error_msg method is private and not directly accessible.

Source code in tests/values/test_values_from_type.py
25
26
27
28
29
30
31
def test_get_error_msg_is_private(self):
    """Test that __get_error_msg method is private and not directly accessible."""
    with self.assertRaises(AttributeError):
        self.parser.__get_error_msg("string", "value", "error description")

    self.assertTrue(hasattr(self.parser, '_ValuesFromType__get_error_msg'))
    self.assertNotIn('__get_error_msg', dir(self.parser))
test_get_from_bool_is_private
test_get_from_bool_is_private()

Test that __get_from_bool method is private and not directly accessible.

Source code in tests/values/test_values_from_type.py
33
34
35
36
37
38
39
def test_get_from_bool_is_private(self):
    """Test that __get_from_bool method is private and not directly accessible."""
    with self.assertRaises(AttributeError):
        self.parser.__get_from_bool("value")

    self.assertTrue(hasattr(self.parser, '_ValuesFromType__get_from_bool'))
    self.assertNotIn('__get_from_bool', dir(self.parser))
test_get_from_dict_is_private
test_get_from_dict_is_private()

Test that __get_from_dict method is private and not directly accessible.

Source code in tests/values/test_values_from_type.py
65
66
67
68
69
70
71
def test_get_from_dict_is_private(self):
    """Test that __get_from_dict method is private and not directly accessible."""
    with self.assertRaises(AttributeError):
        self.parser.__get_from_dict("value")

    self.assertTrue(hasattr(self.parser, '_ValuesFromType__get_from_dict'))
    self.assertNotIn('__get_from_dict', dir(self.parser))
test_get_from_float_is_private
test_get_from_float_is_private()

Test that __get_from_float method is private and not directly accessible.

Source code in tests/values/test_values_from_type.py
49
50
51
52
53
54
55
def test_get_from_float_is_private(self):
    """Test that __get_from_float method is private and not directly accessible."""
    with self.assertRaises(AttributeError):
        self.parser.__get_from_float("value")

    self.assertTrue(hasattr(self.parser, '_ValuesFromType__get_from_float'))
    self.assertNotIn('__get_from_float', dir(self.parser))
test_get_from_int_is_private
test_get_from_int_is_private()

Test that __get_from_int method is private and not directly accessible.

Source code in tests/values/test_values_from_type.py
41
42
43
44
45
46
47
def test_get_from_int_is_private(self):
    """Test that __get_from_int method is private and not directly accessible."""
    with self.assertRaises(AttributeError):
        self.parser.__get_from_int("value")

    self.assertTrue(hasattr(self.parser, '_ValuesFromType__get_from_int'))
    self.assertNotIn('__get_from_int', dir(self.parser))
test_get_from_list_is_private
test_get_from_list_is_private()

Test that __get_from_list method is private and not directly accessible.

Source code in tests/values/test_values_from_type.py
57
58
59
60
61
62
63
def test_get_from_list_is_private(self):
    """Test that __get_from_list method is private and not directly accessible."""
    with self.assertRaises(AttributeError):
        self.parser.__get_from_list("value")

    self.assertTrue(hasattr(self.parser, '_ValuesFromType__get_from_list'))
    self.assertNotIn('__get_from_list', dir(self.parser))
test_int_invalid_value_raises
test_int_invalid_value_raises()

Test that an invalid int value raises a ValueError.

Source code in tests/values/test_values_from_type.py
159
160
161
162
163
def test_int_invalid_value_raises(self):
    """Test that an invalid int value raises a ValueError."""
    with self.assertRaises(ValueError) as context:
        self.parser.get_value("int", "3.14")
    self.assertIn("invalid literal for int", str(context.exception))
test_int_value_parses_from_string
test_int_value_parses_from_string()

Test that integer values are correctly parsed from strings.

Source code in tests/values/test_values_from_type.py
 97
 98
 99
100
def test_int_value_parses_from_string(self):
    """Test that integer values are correctly parsed from strings."""
    self.assertEqual(self.parser.get_value("int", "42"), 42)
    self.assertEqual(self.parser.get_value("integer", "-7"), -7)
test_list_invalid_value_raises
test_list_invalid_value_raises()

Test that an invalid list value raises a SyntaxError.

Source code in tests/values/test_values_from_type.py
171
172
173
174
175
def test_list_invalid_value_raises(self):
    """Test that an invalid list value raises a SyntaxError."""
    with self.assertRaises(ValueError) as context:
        self.parser.get_value("list", "not a list")
    self.assertIn("Invalid list format", str(context.exception))
test_list_value_parses_from_string
test_list_value_parses_from_string()

Test that list values are correctly parsed from strings.

Source code in tests/values/test_values_from_type.py
112
113
114
115
116
117
118
119
120
121
def test_list_value_parses_from_string(self):
    """Test that list values are correctly parsed from strings."""
    self.assertEqual(self.parser.get_value("list", "[1, 2, 3]"), [1, 2, 3])
    # self.assertEqual(self.parser.get_value("list", "['1', '2', '3']"), [1, 2, 3])
    self.assertEqual(self.parser.get_value("list", '[1, 2, 3]'), [1, 2, 3])
    # self.assertEqual(self.parser.get_value("list", '["1", "2", "3"]'), [1, 2, 3])
    self.assertEqual(self.parser.get_value("list", "[a, b]"), ['a', 'b'])
    self.assertEqual(self.parser.get_value("list", "['a', 'b']"), ['a', 'b'])
    self.assertEqual(self.parser.get_value("list", '[a, b]'), ['a', 'b'])
    self.assertEqual(self.parser.get_value("list", '["a", "b"]'), ['a', 'b'])
test_list_value_parses_from_string_empty
test_list_value_parses_from_string_empty()

Test that list values are correctly parsed from string of an empty list.

Source code in tests/values/test_values_from_type.py
123
124
125
126
def test_list_value_parses_from_string_empty(self):
    """Test that list values are correctly parsed from string of an empty list."""
    self.assertEqual(self.parser.get_value("list", "[ ]"), [])
    self.assertEqual(self.parser.get_value("list", " [ ] "), [])
test_list_value_returns_list
test_list_value_returns_list()

Test that a list value is returned as-is.

Source code in tests/values/test_values_from_type.py
107
108
109
110
def test_list_value_returns_list(self):
    """Test that a list value is returned as-is."""
    value = [1, 2, 3]
    self.assertEqual(self.parser.get_value("list", value), value)
test_string_returns_string
test_string_returns_string()

Test that a string value is returned as-is.

Source code in tests/values/test_values_from_type.py
77
78
79
80
def test_string_returns_string(self):
    """Test that a string value is returned as-is."""
    self.assertEqual(self.parser.get_value("string", 123), "123")
    self.assertEqual(self.parser.get_value("str", "abc"), "abc")
test_unsupported_type_raises
test_unsupported_type_raises()

Test that an unsupported type raises a ValueError.

Source code in tests/values/test_values_from_type.py
139
140
141
142
143
144
145
146
147
def test_unsupported_type_raises(self):
    """Test that an unsupported type raises a ValueError."""
    with self.assertRaises(ValueError) as context:
        self.parser.get_value("unsupported", "value")

    self.assertIn(
        "variable for the type 'unsupported'",
        str(context.exception)
    )