-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.py
More file actions
151 lines (119 loc) · 5.71 KB
/
test_utils.py
File metadata and controls
151 lines (119 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
"""Unit tests for commons/utils.py module."""
import unittest
from pyflowintel.commons.utils import (
validate_file_ext,
validate_nat_num,
validate_list_of,
)
class TestFileValidation(unittest.TestCase):
"""Unit tests for file validation utilities."""
def test_validate_file_ext_valid_json(self):
"""Test that valid JSON extension passes."""
self.assertTrue(validate_file_ext("test.json", ["json"]))
def test_validate_file_ext_valid_yaml(self):
"""Test that valid YAML extensions pass."""
self.assertTrue(validate_file_ext("test.yaml", ["yaml", "yml"]))
self.assertTrue(validate_file_ext("test.yml", ["yaml", "yml"]))
def test_validate_file_ext_invalid(self):
"""Test that invalid extension raises ValueError."""
with self.assertRaises(ValueError) as ctx:
validate_file_ext("test.txt", ["json"])
self.assertIn("must have one of these extensions", str(ctx.exception))
def test_validate_file_ext_no_extension(self):
"""Test that file without extension raises ValueError."""
with self.assertRaises(ValueError) as ctx:
validate_file_ext("testfile", ["json"])
self.assertIn("must have one of these extensions", str(ctx.exception))
class TestValidateNatNum(unittest.TestCase):
"""Unit tests for validate_int function."""
def test_validate_nat_with_integer(self):
"""Test integer values."""
# Should not raise
validate_nat_num(42)
with self.assertRaises(ValueError) as ctx:
validate_nat_num(0)
self.assertIn("integer is expected", str(ctx.exception))
with self.assertRaises(ValueError) as ctx:
validate_nat_num(-1)
self.assertIn("integer is expected", str(ctx.exception))
def test_validate_nat_with_string(self):
"""Test that string raises ValueError."""
with self.assertRaises(ValueError) as ctx:
validate_nat_num("123")
self.assertIn("integer is expected", str(ctx.exception))
def test_validate_nat_with_float(self):
"""Test that float raises ValueError."""
with self.assertRaises(ValueError) as ctx:
validate_nat_num(42.5)
self.assertIn("integer is expected", str(ctx.exception))
def test_validate_nat_with_none(self):
"""Test that None raises ValueError."""
with self.assertRaises(ValueError) as ctx:
validate_nat_num(None)
self.assertIn("integer is expected", str(ctx.exception))
def test_validate_nat_with_boolean(self):
"""Test boolean values (bool is subclass of int in Python)."""
# In Python, bool is a subclass of int, so this passes
validate_nat_num(True)
with self.assertRaises(ValueError) as ctx:
validate_nat_num(False)
self.assertIn("integer is expected", str(ctx.exception))
class TestValidateListOf(unittest.TestCase):
"""Unit tests for validate_list_of function."""
def test_validate_list_of_with_valid_list_no_type(self):
"""Test that list passes validation without type checking."""
validate_list_of([1, 2, 3])
validate_list_of([])
validate_list_of(["a", "b", "c"])
def test_validate_list_of_with_none(self):
"""Test that non-list raises ValueError."""
with self.assertRaises(ValueError) as ctx:
validate_list_of(None)
self.assertIn("List expected", str(ctx.exception))
def test_validate_list_of_with_non_list(self):
"""Test that non-list raises ValueError."""
with self.assertRaises(ValueError) as ctx:
validate_list_of("not a list")
self.assertIn("List expected", str(ctx.exception))
with self.assertRaises(ValueError) as ctx:
validate_list_of(123)
self.assertIn("List expected", str(ctx.exception))
def test_validate_list_of_integers(self):
"""Test validation of list of integers."""
validate_list_of([1, 2, 3], "int")
validate_list_of([0, -5, 100], "int")
def test_validate_list_of_integers_invalid(self):
"""Test that mixed types fail integer validation."""
with self.assertRaises(ValueError) as ctx:
validate_list_of([1, "2", 3], "int")
self.assertIn("All elements must be of type int", str(ctx.exception))
def test_validate_list_of_strings(self):
"""Test validation of list of strings."""
validate_list_of(["a", "b", "c"], "str")
validate_list_of([""], "str")
def test_validate_list_of_floats(self):
"""Test validation of list of floats."""
validate_list_of([1.5, 2.3, 3.7], "float")
def test_validate_list_of_floats_invalid(self):
"""Test that integers don't pass float validation."""
with self.assertRaises(ValueError) as ctx:
validate_list_of([1.5, 2, 3.7], "float")
self.assertIn("All elements must be of type float", str(ctx.exception))
def test_validate_list_of_bools(self):
"""Test validation of list of booleans."""
validate_list_of([True, False, True], "bool")
def test_validate_list_of_lists(self):
"""Test validation of list of lists."""
validate_list_of([[1, 2], [3, 4]], "list")
def test_validate_list_of_unknown_type(self):
"""Test that unknown type raises ValueError."""
with self.assertRaises(ValueError) as ctx:
validate_list_of([1, 2, 3], "dict")
self.assertIn("Unknown validation type", str(ctx.exception))
def test_validate_list_of_empty_list(self):
"""Test that empty list passes all type validations."""
validate_list_of([], "int")
validate_list_of([], "str")
validate_list_of([], "float")
if __name__ == "__main__":
unittest.main()