-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_models.py
240 lines (205 loc) · 9.57 KB
/
test_models.py
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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#
# Copyright 2023 Haiku, Inc. All rights reserved.
# Distributed under the terms of the MIT License.
#
# Authors:
# Niels Sascha Reedijk, niels.reedijk@gmail.com
#
import copy
import dataclasses
import os
import unittest
from typing import Any
from formatchecker.models import FormatSegment, Segment, ReformatType, File, strip_empty_values_from_input_dict
class SegmentTest(unittest.TestCase):
def test_input(self):
# Test invalid start (start < 1)
self.assertRaises(ValueError, Segment, 0,None)
# Test invalid end (end < 1)
self.assertRaises(ValueError, Segment, 1,0)
# Test invalid range (end < start)
self.assertRaises(ValueError, Segment, 50,45)
# Test valid input
s = Segment(1, None)
self.assertEqual(s.start, 1)
self.assertEqual(s.end, None)
s = Segment(15, 30)
self.assertEqual(s.start, 15)
self.assertEqual(s.end, 30)
def test_format_range(self):
# Valid range
s = Segment(1,5)
self.assertEqual(s.format_range(), "1:5")
# Invalid range
s = Segment(1, None)
self.assertRaises(ValueError, s.format_range)
def test_equality(self):
self.assertEqual(Segment(100,200), Segment(100,200))
self.assertNotEqual(Segment(100, 200), Segment(100, None))
class FormatSegmentTest(unittest.TestCase):
# Test contents
_content = ["line1\n", "line2\n"]
def test_reformat_type(self):
# Valid insert segment
f = FormatSegment(15, None, self._content)
self.assertEqual(f.start, 15)
self.assertIsNone(f.end)
self.assertEqual(f.formatted_content, self._content)
self.assertEqual(f.reformat_type, ReformatType.INSERTION)
# Invalid insert segment
self.assertRaises(ValueError, FormatSegment, 15, None, [])
# Valid modification segment
f = FormatSegment(15, 20, self._content)
self.assertEqual(f.start, 15)
self.assertEqual(f.end, 20)
self.assertEqual(f.formatted_content, self._content)
self.assertEqual(f.reformat_type, ReformatType.MODIFICATION)
# Valid deletion segment
f = FormatSegment(15,20, [])
self.assertEqual(f.start, 15)
self.assertEqual(f.end, 20)
self.assertEqual(f.formatted_content, [])
self.assertEqual(f.reformat_type, ReformatType.DELETION)
def test_equality(self):
contents = ["line1"]
self.assertEqual(FormatSegment(100,200, self._content), FormatSegment(100,200, self._content))
self.assertNotEqual(FormatSegment(100, 200, self._content), FormatSegment(100, 200, []))
class FileTest(unittest.TestCase):
# Static data
_patch_segments = [Segment(25, 25), Segment(37,49), Segment(51, 52),
Segment(61, 61), Segment(83, 83), Segment(85, 85)]
# Helpers
@classmethod
def get_patch_segments(cls, f: File) -> list[Segment]:
return f.patch_segments
@classmethod
def get_format_segments(cls, f: File) -> list[Segment]:
return f.format_segments
@classmethod
def setUpClass(cls):
data_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
with open(os.path.join(data_path, "test_models_file_base")) as f:
cls._base_contents = f.readlines()
with open(os.path.join(data_path, "test_models_file_patch")) as f:
cls._patch_contents = f.readlines()
with open(os.path.join(data_path, "test_models_file_formatted")) as f:
cls._formatted_contents = f.readlines()
# set up the format segments
cls._format_segments = [
FormatSegment(25, 25, cls._formatted_contents[24:25]),
FormatSegment(37, 49, cls._formatted_contents[36:48]),
FormatSegment(51, 52, cls._formatted_contents[49:52]),
FormatSegment(88, 89, []),
FormatSegment(95, None, cls._formatted_contents[93:95])
]
def test_initialization(self):
# Helper functions to check that properties raise exceptions
# Create object with no content
f = File("filename")
self.assertIsNone(f.base_contents)
self.assertIsNone(f.patch_contents)
self.assertIsNone(f.formatted_contents)
self.assertRaises(RuntimeError, self.get_patch_segments, f)
self.assertRaises(RuntimeError, self.get_format_segments, f)
# Create object with only base contents
f = File("filename", self._base_contents)
self.assertEqual(f.base_contents, self._base_contents)
self.assertIsNone(f.patch_contents)
self.assertIsNone(f.formatted_contents)
self.assertRaises(RuntimeError, self.get_patch_segments, f)
self.assertRaises(RuntimeError, self.get_format_segments, f)
# Create object with only patched contents
f = File("filename", patch=self._patch_contents)
self.assertIsNone(f.base_contents)
self.assertEqual(f.patch_contents, self._patch_contents)
self.assertIsNone(f.formatted_contents)
self.assertRaises(RuntimeError, self.get_patch_segments, f)
self.assertRaises(RuntimeError, self.get_format_segments, f)
# Create object with base and patch contents
f = File("filename", self._base_contents, self._patch_contents)
self.assertEqual(f.base_contents, self._base_contents)
self.assertEqual(f.patch_contents, self._patch_contents)
self.assertIsNone(f.formatted_contents)
self.assertEqual(f.patch_segments, self._patch_segments)
self.assertRaises(RuntimeError, self.get_format_segments, f)
def test_content_reset(self):
"""Validate that a File object can be set/reset various times, and that triggers a recalculation of segments"""
f = File("filename", self._base_contents, self._patch_contents)
self.assertEqual(f.base_contents, self._base_contents)
self.assertEqual(f.patch_contents, self._patch_contents)
self.assertIsNone(f.formatted_contents)
self.assertGreater(len(f.patch_segments), 0)
self.assertRaises(RuntimeError, self.get_format_segments, f)
# Add formatted segments
f.formatted_contents = self._formatted_contents
self.assertEqual(f.base_contents, self._base_contents)
self.assertEqual(f.patch_contents, self._patch_contents)
self.assertEqual(f.formatted_contents, self._formatted_contents)
self.assertGreater(len(f.patch_segments), 0)
self.assertGreater(len(f.format_segments), 0)
# Remove patched contents (should reset all segments)
f.patch_contents = None
self.assertEqual(f.base_contents, self._base_contents)
self.assertEqual(f.patch_contents, None)
self.assertEqual(f.formatted_contents, self._formatted_contents)
self.assertRaises(RuntimeError, self.get_patch_segments, f)
self.assertRaises(RuntimeError, self.get_format_segments, f)
# Validate that setting empty (patch) content is also valid. If patch contents is empty, it is a deletion only
# so that means that there will be 0 patch segments.
f.patch_contents = []
self.assertEqual(f.base_contents, self._base_contents)
self.assertEqual(f.patch_contents, [])
self.assertEqual(f.formatted_contents, self._formatted_contents)
self.assertEqual(len(f.patch_segments), 0)
self.assertGreater(len(f.format_segments), 0)
# Validate that removing formatted_contents will remove format_segments
f.formatted_contents = None
self.assertEqual(f.base_contents, self._base_contents)
self.assertEqual(f.patch_contents, [])
self.assertIsNone(f.formatted_contents)
self.assertEqual(len(f.patch_segments), 0)
self.assertRaises(RuntimeError, self.get_format_segments, f)
def test_formatted_segments(self):
"""This test determines if the File class correctly determines the formatted segments"""
f = File("filename", self._base_contents, self._patch_contents)
f.formatted_contents = self._formatted_contents
self.assertEqual(f.format_segments, self._format_segments)
class DictStripTest(unittest.TestCase):
@dataclasses.dataclass
class SimpleData:
field_with_value: int = 1
field_without_value: None = None
@dataclasses.dataclass
class ComplexData:
simple_field: int = 1
complex_list: list[Any|None] = dataclasses.field(default_factory=lambda: copy.copy(
[
"value1",
DictStripTest.SimpleData()
]
))
complex_dict: dict[str, Any | None] = dataclasses.field(default_factory=lambda: copy.copy(
{
"value1": "string",
"value2": DictStripTest.SimpleData(),
"value3": None
}
))
def test_strip(self):
self.assertEqual(strip_empty_values_from_input_dict(DictStripTest.SimpleData()), {"field_with_value": 1})
self.assertEqual(strip_empty_values_from_input_dict(DictStripTest.ComplexData()),
{
"simple_field": 1,
"complex_list": [
"value1",
{
"field_with_value": 1
}
],
"complex_dict": {
"value1": "string",
"value2": {
"field_with_value": 1
}
}
})