Commits
1 1 | """ |
2 2 | This module contains tests for the classes of imbaseline. Since the main feature of the task is |
3 3 | made up of a combination of imsmooth/sdsmooth/sdbaseline, so tests consist of simple tests for |
4 4 | these tasks and white tests for the unique classes of imbaseline such as image subtraction. |
5 5 | """ |
6 6 | |
7 7 | import os |
8 8 | import re |
9 9 | import shutil |
10 10 | import unittest |
11 + | import uuid |
11 12 | |
12 13 | import functools |
13 14 | import numpy as np |
14 15 | from casatasks import casalog |
15 16 | from casatasks.private.sdutil import tool_manager |
16 17 | from casatasks.private.task_imbaseline \ |
17 18 | import (CasaImageStack, EraseableFolder, Image2MSMethods, Image2MSParams, ImageShape, ImageSubtractionMethods, |
18 19 | ImsmoothMethods, ImsmoothParams, MeasurementSetStack, MS2ImageMethods, SdbaselineMethods, |
19 20 | SdbaselineParams, SdsmoothMethods, SdsmoothParams, UnerasableFolder, |
20 21 | eraseable_folder_register, get_image_shape, imbaseline) |
21 22 | |
22 23 | from casatools import ctsys, image, table |
23 24 | |
24 25 | _tb = table() |
25 26 | ctsys_resolve = ctsys.resolve |
26 27 | DATACOLUMN = 'DATA' |
27 28 | UNEXISTS = 'unexists' |
28 29 | DUMMY_FOLDERS = ('dummy1', 'dummy2', 'dummy3') |
30 + | |
29 31 | casalog.origin('imbaseline') |
30 32 | |
31 33 | |
32 34 | class test_base(unittest.TestCase): |
33 35 | |
34 36 | |
35 37 | def invalid_argument_case(func): |
36 38 | """Decorator for the test case that is intended to fail due to invalid argument.""" |
37 39 | wraps(func) | .
38 40 | def wrapper(self): |
55 57 | with self.assertRaises(exception_type) as ctx: |
56 58 | func(self) |
57 59 | self.fail(msg='The task must throw exception') |
58 60 | the_exception = ctx.exception |
59 61 | message = str(the_exception) |
60 62 | self.assertIsNotNone(re.search(exception_pattern, message), |
61 63 | msg='error message \'%s\' is not expected.' % (message)) |
62 64 | return _wrapper |
63 65 | return wrapper |
64 66 | |
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] |
67 + | |
68 + | def setUpClass(cls): |
69 + | prefix = os.getcwd() + '/' |
70 + | while True: |
71 + | cls.foldername = str(uuid.uuid4()) |
72 + | path = prefix + cls.foldername |
73 + | if not os.path.exists(path): |
74 + | os.mkdir(path) |
75 + | os.chdir(path) |
76 + | break |
77 + | |
78 + | |
79 + | def tearDownClass(cls): |
80 + | os.chdir('..') |
81 + | if os.path.exists(cls.foldername): |
82 + | shutil.rmtree(cls.foldername) |
72 83 | |
73 84 | def tearDown(self): |
74 85 | eraseable_folder_register.clear() |
75 86 | self.assertTrue(len(_tb.showcache()) == 0) |
76 87 | # make sure directory is clean as per verification test requirement |
77 88 | cwd = os.getcwd() |
78 89 | for filename in os.listdir(cwd): |
79 90 | file_path = os.path.join(cwd, filename) |
80 91 | try: |
81 92 | if os.path.isfile(file_path) or os.path.islink(file_path): |
82 93 | os.unlink(file_path) |
83 94 | elif os.path.isdir(file_path): |
84 95 | shutil.rmtree(file_path) |
85 - | else: |
86 - | print(f'unerase: {file_path}') |
87 96 | except Exception as e: |
88 97 | print('Failed to delete %s. Reason: %s' % (file_path, e)) |
89 98 | |
99 + | def _create_dummy_folders(self): |
100 + | def _setup_folder(folder): |
101 + | if os.path.exists(folder): |
102 + | shutil.rmtree(folder) |
103 + | os.mkdir(folder) |
104 + | |
105 + | [_setup_folder(folder) for folder in DUMMY_FOLDERS] |
106 + | |
90 107 | def _copy_test_files(self, basename, filename): |
91 108 | """Copy files for testing into current path.""" |
92 109 | _base = ctsys_resolve(basename) |
93 110 | copy_from = os.path.join(_base, filename) |
94 111 | if not os.path.exists(copy_from) or copy_from == os.path.join(os.getcwd(), filename): |
95 112 | raise RuntimeError(f'Error is occured or existed on a path {copy_from} or {filename}') |
96 113 | |
97 114 | if os.path.exists(filename): |
98 115 | if os.path.isfile(filename) or os.path.islink(filename): |
99 116 | os.unlink(filename) |
125 142 | 1-12. bottom() unexist stuff |
126 143 | 1-13. clear() exist EraseableFolder file |
127 144 | 1-14. clear() exist UneraseableFolder file |
128 145 | 1-15. erase EraseableFolder file |
129 146 | 1-16. erase UneraseableFolder file |
130 147 | 1-17. height() test |
131 148 | 1-18. pop() when height is 1 |
132 149 | """ |
133 150 | |
134 151 | def setUp(self): |
152 + | self._create_dummy_folders() |
135 153 | if os.path.exists(UNEXISTS): |
136 154 | shutil.rmtree(UNEXISTS) |
137 - | super().setUp() |
138 155 | |
139 156 | def test_1_1(self): |
140 157 | stack = CasaImageStack(UnerasableFolder(DUMMY_FOLDERS[0])) |
141 158 | self.assertTrue(stack.height() == 1) |
142 159 | |
143 160 | exception_case(ValueError, f'file {UNEXISTS} is not found') | .
144 161 | def test_1_2(self): |
145 162 | CasaImageStack(UnerasableFolder(UNEXISTS)) |
146 163 | |
147 164 | def test_1_3(self): |
408 425 | 4-3. invalid image |
409 426 | 4-4. set empty stack |
410 427 | 4-5. check Image2MSParams |
411 428 | """ |
412 429 | |
413 430 | datapath = ctsys_resolve('unittest/imbaseline/') |
414 431 | expected = 'expected.im' |
415 432 | datacolumn = DATACOLUMN |
416 433 | |
417 434 | def setUp(self): |
435 + | self._create_dummy_folders() |
418 436 | self._copy_test_files(self.datapath, self.expected) |
419 437 | self.image_shape = get_image_shape(os.path.join(self.datapath, self.expected)) |
420 - | super().setUp() |
421 438 | |
422 439 | def test_4_1(self): |
423 440 | image_stack = CasaImageStack(top=UnerasableFolder(self.expected)) |
424 441 | ms_stack = MeasurementSetStack() |
425 442 | Image2MSMethods.execute(self.datacolumn, self.image_shape, image_stack, ms_stack) |
426 443 | self.assertEqual(ms_stack.height(), 1) |
427 444 | ms_path = ms_stack.peak().path |
428 445 | self.assertTrue(os.path.exists(ms_path)) |
429 446 | self.assertTrue(os.path.exists(os.path.join(ms_path, 'table.dat'))) |
430 447 | self.assertTrue(os.path.exists(os.path.join(ms_path, 'ANTENNA'))) |