Commits
18 18 | ImsmoothMethods, ImsmoothParams, MeasurementSetStack, MS2ImageMethods, SdbaselineMethods, |
19 19 | SdbaselineParams, SdsmoothMethods, SdsmoothParams, UnerasableFolder, |
20 20 | eraseable_folder_register, get_image_shape, imbaseline) |
21 21 | |
22 22 | from casatools import ctsys, image, table |
23 23 | |
24 24 | _tb = table() |
25 25 | ctsys_resolve = ctsys.resolve |
26 26 | DATACOLUMN = 'DATA' |
27 27 | UNEXISTS = 'unexists' |
28 + | DUMMY_FOLDERS = ('dummy1', 'dummy2', 'dummy3') |
28 29 | casalog.origin('imbaseline') |
29 30 | |
30 31 | |
31 32 | class test_base(unittest.TestCase): |
32 33 | |
33 34 | |
34 35 | def invalid_argument_case(func): |
35 36 | """Decorator for the test case that is intended to fail due to invalid argument.""" |
36 37 | wraps(func) | .
37 38 | def wrapper(self): |
54 55 | with self.assertRaises(exception_type) as ctx: |
55 56 | func(self) |
56 57 | self.fail(msg='The task must throw exception') |
57 58 | the_exception = ctx.exception |
58 59 | message = str(the_exception) |
59 60 | self.assertIsNotNone(re.search(exception_pattern, message), |
60 61 | msg='error message \'%s\' is not expected.' % (message)) |
61 62 | return _wrapper |
62 63 | return wrapper |
63 64 | |
65 + | def setUp(self): |
66 + | def _setup_folder(folder): |
67 + | if os.path.exists(folder): |
68 + | shutil.rmtree(folder) |
69 + | os.mkdir(folder) |
70 + | |
71 + | [_setup_folder(folder) for folder in DUMMY_FOLDERS] |
72 + | |
64 73 | def tearDown(self): |
65 74 | eraseable_folder_register.clear() |
66 75 | self.assertTrue(len(_tb.showcache()) == 0) |
67 76 | # make sure directory is clean as per verification test requirement |
68 77 | cwd = os.getcwd() |
69 78 | for filename in os.listdir(cwd): |
70 79 | file_path = os.path.join(cwd, filename) |
71 80 | try: |
72 81 | if os.path.isfile(file_path) or os.path.islink(file_path): |
73 82 | os.unlink(file_path) |
113 122 | 1-9. subpeak() exist stuff |
114 123 | 1-10. subpeak() unexist stuff |
115 124 | 1-11. bottom() exist stuff |
116 125 | 1-12. bottom() unexist stuff |
117 126 | 1-13. clear() exist EraseableFolder file |
118 127 | 1-14. clear() exist UneraseableFolder file |
119 128 | 1-15. erase EraseableFolder file |
120 129 | 1-16. erase UneraseableFolder file |
121 130 | """ |
122 131 | |
123 - | dummy_folder1 = 'dummy1' |
124 - | dummy_folder2 = 'dummy2' |
125 - | |
126 132 | def setUp(self): |
127 - | if os.path.exists(self.dummy_folder1): |
128 - | shutil.rmtree(self.dummy_folder1) |
129 - | os.mkdir(self.dummy_folder1) |
130 - | if os.path.exists(self.dummy_folder2): |
131 - | shutil.rmtree(self.dummy_folder2) |
132 - | os.mkdir(self.dummy_folder2) |
133 133 | if os.path.exists(UNEXISTS): |
134 134 | shutil.rmtree(UNEXISTS) |
135 + | super().setUp() |
135 136 | |
136 137 | def test_1_1(self): |
137 - | stack = CasaImageStack(UnerasableFolder(self.dummy_folder1)) |
138 + | stack = CasaImageStack(UnerasableFolder(DUMMY_FOLDERS[0])) |
138 139 | self.assertTrue(stack.height() == 1) |
139 140 | |
140 141 | exception_case(ValueError, f'file {UNEXISTS} is not found') | .
141 142 | def test_1_2(self): |
142 143 | CasaImageStack(UnerasableFolder(UNEXISTS)) |
143 144 | |
144 145 | def test_1_3(self): |
145 146 | stack = CasaImageStack() |
146 - | stack.push(UnerasableFolder(self.dummy_folder1)) |
147 + | stack.push(UnerasableFolder(DUMMY_FOLDERS[0])) |
147 148 | self.assertTrue(stack.height() == 1) |
148 149 | |
149 150 | exception_case(ValueError, f'file {UNEXISTS} is not found') | .
150 151 | def test_1_4(self): |
151 152 | stack = CasaImageStack() |
152 153 | stack.push(UnerasableFolder(UNEXISTS)) |
153 154 | |
154 155 | def test_1_5(self): |
155 156 | stack = CasaImageStack() |
156 - | stack.push(UnerasableFolder(self.dummy_folder1)) |
157 - | obj = UnerasableFolder(self.dummy_folder2) |
157 + | stack.push(UnerasableFolder(DUMMY_FOLDERS[0])) |
158 + | obj = UnerasableFolder(DUMMY_FOLDERS[1]) |
158 159 | stack.push(obj) |
159 160 | tmp = stack.pop() |
160 161 | self.assertEqual(obj, tmp) |
161 162 | self.assertTrue(stack.height() == 1) |
162 163 | |
163 164 | exception_case(RuntimeError, 'the stack cannot pop') | .
164 165 | def test_1_6(self): |
165 166 | stack = CasaImageStack() |
166 167 | stack.pop() |
167 168 | |
168 169 | def test_1_7(self): |
169 170 | stack = CasaImageStack() |
170 - | obj1 = UnerasableFolder(self.dummy_folder1) |
171 + | obj1 = UnerasableFolder(DUMMY_FOLDERS[0]) |
171 172 | stack.push(obj1) |
172 - | obj2 = UnerasableFolder(self.dummy_folder2) |
173 + | obj2 = UnerasableFolder(DUMMY_FOLDERS[1]) |
173 174 | stack.push(obj2) |
174 - | self.assertEqual(stack.peak(), obj2) |
175 - | self.assertEqual(stack.subpeak(), obj1) |
175 + | obj3 = UnerasableFolder(DUMMY_FOLDERS[2]) |
176 + | stack.push(obj3) |
177 + | self.assertEqual(stack.peak(), obj3) |
178 + | self.assertEqual(stack.subpeak(), obj2) |
176 179 | self.assertEqual(stack.bottom(), obj1) |
177 180 | |
178 181 | exception_case(RuntimeError, 'the stack is empty') | .
179 182 | def test_1_8(self): |
180 183 | stack = CasaImageStack() |
181 184 | stack.peak() |
182 185 | |
183 186 | def test_1_9(self): |
184 - | obj = UnerasableFolder(self.dummy_folder1) |
185 - | stack = CasaImageStack(obj) |
186 - | stack.push(UnerasableFolder(self.dummy_folder2)) |
187 - | self.assertEqual(stack.subpeak(), obj) |
187 + | stack = CasaImageStack() |
188 + | obj1 = UnerasableFolder(DUMMY_FOLDERS[0]) |
189 + | stack.push(obj1) |
190 + | obj2 = UnerasableFolder(DUMMY_FOLDERS[1]) |
191 + | stack.push(obj2) |
192 + | self.assertEqual(stack.subpeak(), obj1) |
193 + | self.assertEqual(stack.bottom(), obj1) |
194 + | obj3 = UnerasableFolder(DUMMY_FOLDERS[2]) |
195 + | stack.push(obj3) |
196 + | self.assertEqual(stack.subpeak(), obj2) |
197 + | self.assertEqual(stack.bottom(), obj1) |
188 198 | |
189 199 | exception_case(RuntimeError, 'the stack has only one stuff') | .
190 200 | def test_1_10(self): |
191 - | stack = CasaImageStack(UnerasableFolder(self.dummy_folder1)) |
201 + | stack = CasaImageStack(UnerasableFolder(DUMMY_FOLDERS[0])) |
192 202 | stack.subpeak() |
193 203 | |
194 204 | def test_1_11(self): |
195 - | obj = UnerasableFolder(self.dummy_folder1) |
205 + | obj = UnerasableFolder(DUMMY_FOLDERS[0]) |
196 206 | stack = CasaImageStack(obj) |
197 207 | self.assertEqual(stack.bottom(), obj) |
198 208 | |
199 209 | exception_case(RuntimeError, 'the stack has not have enough stuff') | .
200 210 | def test_1_12(self): |
201 211 | stack = CasaImageStack() |
202 212 | stack.bottom() |
203 213 | |
204 214 | def test_1_13(self): |
205 - | file = EraseableFolder(self.dummy_folder1) |
215 + | file = EraseableFolder(DUMMY_FOLDERS[0]) |
206 216 | stack = CasaImageStack(file) |
207 217 | stack.clear(False) |
208 - | self.assertTrue(os.path.exists(self.dummy_folder1)) |
218 + | self.assertTrue(os.path.exists(DUMMY_FOLDERS[0])) |
209 219 | self.assertEqual(stack.height(), 0) |
210 220 | |
211 221 | def test_1_14(self): |
212 - | stack = CasaImageStack(UnerasableFolder(self.dummy_folder1)) |
222 + | stack = CasaImageStack(UnerasableFolder(DUMMY_FOLDERS[0])) |
213 223 | stack.clear(False) |
214 - | self.assertTrue(os.path.exists(self.dummy_folder1)) |
224 + | self.assertTrue(os.path.exists(DUMMY_FOLDERS[0])) |
215 225 | self.assertEqual(stack.height(), 0) |
216 226 | |
217 227 | def test_1_15(self): |
218 - | file = EraseableFolder(self.dummy_folder1) |
228 + | file = EraseableFolder(DUMMY_FOLDERS[0]) |
219 229 | file.erase(False) |
220 - | self.assertFalse(os.path.exists(self.dummy_folder1)) |
230 + | self.assertFalse(os.path.exists(DUMMY_FOLDERS[0])) |
221 231 | |
222 232 | def test_1_16(self): |
223 - | file = UnerasableFolder(self.dummy_folder1) |
233 + | file = UnerasableFolder(DUMMY_FOLDERS[0]) |
224 234 | file.erase(False) |
225 - | self.assertTrue(os.path.exists(self.dummy_folder1)) |
235 + | self.assertTrue(os.path.exists(DUMMY_FOLDERS[0])) |
226 236 | |
227 237 | |
228 238 | class ImageShape_test(test_base): |
229 239 | """Test ImageShape. |
230 240 | |
231 241 | 2-1. successful case |
232 242 | 2-2. invalid im_nchan |
233 243 | 2-3. invalid dir_shape |
234 244 | """ |
235 245 | |
368 378 | |
369 379 | 4-1. simple successful case |
370 380 | 4-2. invalid datacolumn |
371 381 | 4-3. invalid image |
372 382 | 4-4. set empty stack |
373 383 | 4-5. check Image2MSParams |
374 384 | """ |
375 385 | |
376 386 | datapath = ctsys_resolve('unittest/imbaseline/') |
377 387 | expected = 'expected.im' |
378 - | dummy_folder1 = 'dummy1' |
379 388 | datacolumn = DATACOLUMN |
380 389 | |
381 390 | def setUp(self): |
382 391 | self._copy_test_files(self.datapath, self.expected) |
383 392 | self.image_shape = get_image_shape(os.path.join(self.datapath, self.expected)) |
384 - | if os.path.exists(self.dummy_folder1): |
385 - | shutil.rmtree(self.dummy_folder1) |
386 - | os.system(f'mkdir {self.dummy_folder1}') |
393 + | super().setUp() |
387 394 | |
388 395 | def test_4_1(self): |
389 396 | image_stack = CasaImageStack(top=UnerasableFolder(self.expected)) |
390 397 | ms_stack = MeasurementSetStack() |
391 398 | Image2MSMethods.execute(self.datacolumn, self.image_shape, image_stack, ms_stack) |
392 399 | self.assertEqual(ms_stack.height(), 1) |
393 400 | ms_path = ms_stack.peak().path |
394 401 | self.assertTrue(os.path.exists(ms_path)) |
395 402 | self.assertTrue(os.path.exists(os.path.join(ms_path, 'table.dat'))) |
396 403 | self.assertTrue(os.path.exists(os.path.join(ms_path, 'ANTENNA'))) |
408 415 | self.assertTrue(os.path.exists(os.path.join(ms_path, 'STATE'))) |
409 416 | |
410 417 | exception_case(RuntimeError, 'column INVALID does not exist') | .
411 418 | def test_4_2(self): |
412 419 | image_stack = CasaImageStack(top=UnerasableFolder(self.expected)) |
413 420 | ms_stack = MeasurementSetStack() |
414 421 | Image2MSMethods.execute('INVALID', self.image_shape, image_stack, ms_stack) |
415 422 | |
416 423 | exception_case(RuntimeError, 'Unable to open image dummy1.') | .
417 424 | def test_4_3(self): |
418 - | image_stack = CasaImageStack(top=UnerasableFolder(self.dummy_folder1)) |
425 + | image_stack = CasaImageStack(top=UnerasableFolder(DUMMY_FOLDERS[0])) |
419 426 | ms_stack = MeasurementSetStack() |
420 427 | Image2MSMethods.execute(self.datacolumn, self.image_shape, image_stack, ms_stack) |
421 428 | |
422 429 | exception_case(RuntimeError, 'the stack is empty') | .
423 430 | def test_4_4(self): |
424 431 | image_stack = CasaImageStack() |
425 432 | ms_stack = MeasurementSetStack() |
426 433 | Image2MSMethods.execute(self.datacolumn, self.image_shape, image_stack, ms_stack) |
427 434 | |
428 435 | def test_4_5(self): |
447 454 | Tests of sdsmooth rely on ones of test_sdsmooth basically, so we have minimal tests in imbaseline. |
448 455 | |
449 456 | 5-1. simple successful case |
450 457 | 5-2. invalid ms stack |
451 458 | 5-3. invalid image stack |
452 459 | 5-4. check SdsmoothParams |
453 460 | """ |
454 461 | datapath = ctsys_resolve('unittest/imbaseline/') |
455 462 | expected_im = 'expected.im' |
456 463 | expected_ms = 'expected.ms' |
457 - | dummy_folder1 = 'dummy1' |
458 464 | datacolumn = DATACOLUMN |
459 465 | spkenel = 'gaussian' |
460 466 | kwidth = 5 |
461 467 | |
462 468 | def setUp(self): |
463 469 | self._copy_test_files(self.datapath, self.expected_im) |
464 470 | self._copy_test_files(self.datapath, self.expected_ms) |
465 471 | self.image_shape = get_image_shape(os.path.join(self.datapath, self.expected_im)) |
466 472 | |
467 473 | def test_5_1(self): |