Coverage for tests/test_object.py: 16%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import multiprocessing
2import unittest
4import numpy as np
6import astshim as ast
7from astshim.test import ObjectTestCase
10class TestObject(ObjectTestCase):
12 def test_attributes(self):
13 """Test accessing object attributes
14 """
15 nin = 2
16 zoom = 1.3
17 obj = ast.ZoomMap(nin, zoom)
19 self.assertEqual(obj.className, "ZoomMap")
21 self.assertTrue(obj.hasAttribute("ID"))
22 self.assertTrue(obj.hasAttribute("Ident"))
23 self.assertTrue(obj.hasAttribute("UseDefs"))
25 self.assertEqual(obj.id, "")
26 self.assertEqual(obj.ident, "")
27 self.assertEqual(obj.useDefs, True)
29 def test_clear_and_test(self):
30 """Test Object.clear and Object.test"""
31 obj = ast.ZoomMap(2, 1.3)
33 self.assertFalse(obj.test("ID"))
34 obj.id = "initial_id"
35 self.assertEqual(obj.id, "initial_id")
36 self.assertTrue(obj.test("ID"))
37 obj.clear("ID")
38 self.assertEqual(obj.id, "")
39 self.assertFalse(obj.test("ID"))
41 def test_copy_and_same(self):
42 """Test Object.copy and Object.same"""
43 obj = ast.ZoomMap(2, 1.3, "Ident=original")
45 initialNumObj = obj.getNObject() # may be >1 when run using pytest
47 self.checkCopy(obj)
48 cp = obj.copy()
49 # A deep copy does not increment refCount but does incremente nObject
50 self.assertEqual(obj.getRefCount(), 1)
51 self.assertEqual(obj.getNObject(), initialNumObj + 1)
52 # A deep copy is not the `same` as the original:
53 # `same` compares AST pointers, similar to Python `is`
54 self.assertFalse(obj.same(cp))
55 self.assertTrue(obj.same(obj))
57 cp.ident = "copy"
58 self.assertEqual(cp.ident, "copy")
59 self.assertEqual(obj.ident, "original")
61 del cp
62 self.assertEqual(obj.getNObject(), initialNumObj)
63 self.assertEqual(obj.getRefCount(), 1)
65 seriesMap = obj.then(obj)
66 # The seriesMap contains two shallow copies of `obj`, so refCount
67 # is increased by 2 and nObject remains unchanged
68 self.assertEqual(obj.getRefCount(), 3)
69 self.assertEqual(obj.getNObject(), initialNumObj)
70 del seriesMap
71 self.assertEqual(obj.getRefCount(), 1)
72 self.assertEqual(obj.getNObject(), initialNumObj)
74 def test_error_handling(self):
75 """Test handling of AST errors
76 """
77 # To introduce an AST error construct a PolyMap with no inverse mapping
78 # and then try to transform in the inverse direction.
79 coeff_f = np.array([
80 [1.2, 1, 2, 0],
81 [-0.5, 1, 1, 1],
82 [1.0, 2, 0, 1],
83 ])
84 pm = ast.PolyMap(coeff_f, 2, "IterInverse=0")
85 indata = np.array([
86 [1.0, 2.0, 3.0],
87 [0.0, 1.0, 2.0],
88 ])
90 # make sure the error string contains "Error"
91 try:
92 pm.applyInverse(indata)
93 except RuntimeError as e:
94 self.assertEqual(e.args[0].count("Error"), 1)
95 print(e)
97 # cause another error and make sure the first error message was purged
98 try:
99 pm.applyInverse(indata)
100 except RuntimeError as e:
101 self.assertEqual(e.args[0].count("Error"), 1)
103 def test_equality(self):
104 """Test __eq__ and __ne__
105 """
106 frame = ast.Frame(2)
107 zoomMap = ast.ZoomMap(2, 1.5)
108 frameSet1 = ast.FrameSet(frame, zoomMap, frame)
109 frameSet2 = ast.FrameSet(frame, zoomMap, frame)
110 self.assertTrue(frameSet1 == frameSet2)
111 self.assertFalse(frameSet1 != frameSet2)
112 self.assertEqual(frameSet1, frameSet2)
114 # the base attribute of frameSet1 is not set; set the base attribute
115 # of framesSet2 and make sure the frame sets are now not equal
116 self.assertFalse(frameSet1.test("Base"))
117 frameSet2.base = 1
118 self.assertTrue(frameSet2.test("Base"))
119 self.assertFalse(frameSet1 == frameSet2)
120 self.assertTrue(frameSet1 != frameSet2)
121 self.assertNotEqual(frameSet1, frameSet2)
123 # make sure base is unset in the inverse of the inverse of frameSet1,
124 # else the equality test will fail for hard-to-understand reasons
125 self.assertFalse(frameSet1.inverted().inverted().test("Base"))
126 self.assertNotEqual(frameSet1, frameSet1.inverted())
127 self.assertEqual(frameSet1, frameSet1.inverted().inverted())
128 self.assertFalse(frameSet1.inverted().inverted().test("Base"))
130 frame3 = ast.Frame(2)
131 frame3.title = "Frame 3"
132 frameSet3 = ast.FrameSet(frame3)
133 self.assertNotEqual(frameSet1, frameSet3)
135 def test_id(self):
136 """Test that ID is *not* transferred to copies"""
137 obj = ast.ZoomMap(2, 1.3)
139 self.assertEqual(obj.id, "")
140 obj.id = "initial_id"
141 self.assertEqual(obj.id, "initial_id")
142 cp = obj.copy()
143 self.assertEqual(cp.id, "")
145 def test_ident(self):
146 """Test that Ident *is* transferred to copies"""
147 obj = ast.ZoomMap(2, 1.3)
149 self.assertEqual(obj.ident, "")
150 obj.ident = "initial_ident"
151 self.assertEqual(obj.ident, "initial_ident")
152 cp = obj.copy()
153 self.assertEqual(cp.ident, "initial_ident")
155 def test_multiprocessing(self):
156 """Make sure we can return objects from multiprocessing
158 This tests DM-13316: AST errors when using multiprocessing
159 to return astshim objects.
160 """
161 numProcesses = 2
162 naxes = 1
163 params = [naxes]*numProcesses
164 pool = multiprocessing.Pool(processes=numProcesses)
165 results = pool.map(ast.UnitMap, params)
166 self.assertEqual(results, [ast.UnitMap(naxes)]*numProcesses)
168 def test_show(self):
169 # pick an object with no floats so we don't have to worry
170 # about the float representation
171 obj = ast.Frame(2)
172 desShowLines = [
173 " Begin Frame \t# Coordinate system description",
174 "# Title = \"2-d coordinate system\" \t# Title of coordinate system",
175 " Naxes = 2 \t# Number of coordinate axes",
176 "# Lbl1 = \"Axis 1\" \t# Label for axis 1",
177 "# Lbl2 = \"Axis 2\" \t# Label for axis 2",
178 " Ax1 = \t# Axis number 1",
179 " Begin Axis \t# Coordinate axis",
180 " End Axis",
181 " Ax2 = \t# Axis number 2",
182 " Begin Axis \t# Coordinate axis",
183 " End Axis",
184 " End Frame",
185 "",
186 ]
187 desShowLinesNoComments = [
188 " Begin Frame",
189 "# Title = \"2-d coordinate system\"",
190 " Naxes = 2",
191 "# Lbl1 = \"Axis 1\"",
192 "# Lbl2 = \"Axis 2\"",
193 " Ax1 =",
194 " Begin Axis",
195 " End Axis",
196 " Ax2 =",
197 " Begin Axis",
198 " End Axis",
199 " End Frame",
200 "",
201 ]
202 self.assertEqual(obj.show(), "\n".join(desShowLines))
203 self.assertEqual(obj.show(True), "\n".join(desShowLines))
204 self.assertEqual(obj.show(False), "\n".join(desShowLinesNoComments))
207if __name__ == "__main__": 207 ↛ 208line 207 didn't jump to line 208, because the condition on line 207 was never true
208 unittest.main()