Skip to content

Commit 534b8d4

Browse files
authored
Merge pull request #15 from mwcraig/fix-api-test-issues
Fix api test issues
2 parents 59b8cb6 + b01d937 commit 534b8d4

File tree

1 file changed

+52
-41
lines changed

1 file changed

+52
-41
lines changed

src/astro_image_display_api/widget_api_test.py

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@ class variable does the trick.
4949
"""
5050
self.image = self.image_widget_class(image_width=250, image_height=100)
5151

52-
def _check_marker_table_return_properties(self, table):
52+
def _assert_empty_marker_table(self, table):
5353
assert isinstance(table, Table)
5454
assert len(table) == 0
5555
assert sorted(table.colnames) == sorted(['x', 'y', 'coord', 'marker name'])
5656

57+
def _get_marker_names_as_set(self):
58+
marks = self.image.get_markers(marker_name="all")["marker name"]
59+
if hasattr(marks, 'mask') and all(marks.mask):
60+
marker_names = set()
61+
else:
62+
marker_names = set(marks)
63+
return marker_names
64+
5765
def test_default_marker_names(self):
5866
# Check only that default names are set to a non-empty string
5967
assert self.image.DEFAULT_MARKER_NAME
@@ -70,9 +78,12 @@ def test_width_height(self):
7078
assert self.image.image_width == width
7179
assert self.image.image_height == height
7280

73-
def test_load_fits(self, data):
81+
def test_load_fits(self, data, tmp_path):
7482
hdu = fits.PrimaryHDU(data=data)
75-
self.image.load_fits(hdu)
83+
image_path = tmp_path / 'test.fits'
84+
hdu.header["BUNIT"] = "adu"
85+
hdu.writeto(image_path)
86+
self.image.load_fits(image_path)
7687

7788
def test_load_nddata(self, data):
7889
nddata = NDData(data)
@@ -96,11 +107,11 @@ def test_offset_by(self, data, wcs):
96107
self.image.offset_by(10 * u.arcmin, 10 * u.arcmin)
97108

98109
# A mix of pixel and sky should produce an error
99-
with pytest.raises(ValueError, match='but dy is of type'):
110+
with pytest.raises(u.UnitConversionError, match='are not convertible'):
100111
self.image.offset_by(10 * u.arcmin, 10)
101112

102113
# A mix of inconsistent units should produce an error
103-
with pytest.raises(u.UnitConversionError):
114+
with pytest.raises(u.UnitConversionError, match='are not convertible'):
104115
self.image.offset_by(1 * u.arcsec, 1 * u.AA)
105116

106117
def test_zoom_level(self, data):
@@ -116,7 +127,7 @@ def test_zoom(self):
116127

117128
def test_marking_operations(self):
118129
marks = self.image.get_markers(marker_name="all")
119-
self._check_marker_table_return_properties(marks)
130+
self._assert_empty_marker_table(marks)
120131
assert not self.image.is_marking
121132

122133
# Ensure you cannot set it like this.
@@ -131,6 +142,7 @@ def test_marking_operations(self):
131142

132143
# Set the marker style
133144
marker_style = {'color': 'yellow', 'radius': 10, 'type': 'cross'}
145+
self.image.marker = marker_style
134146
m_str = str(self.image.marker)
135147
for key in marker_style.keys():
136148
assert key in m_str
@@ -157,41 +169,42 @@ def test_marking_operations(self):
157169
warnings.simplefilter("error")
158170
t = self.image.get_markers(marker_name='markymark')
159171

160-
self._check_marker_table_return_properties(t)
172+
self._assert_empty_marker_table(t)
161173

162174
self.image.click_drag = True
163175
self.image.start_marking()
164176
assert not self.image.click_drag
165177

166-
# Simulate a mouse click to add default marker name to the list.
167-
try:
168-
self.image._mouse_click_cb(self.image.viewer, None, 50, 50)
169-
assert self.image.get_marker_names() == [self.image._interactive_marker_set_name, 'markymark']
170-
except AttributeError:
171-
pass
178+
# Add a marker to the interactive marking table
179+
self.image.add_markers(
180+
Table(data=[[50], [50]], names=['x', 'y'], dtype=('float', 'float')),
181+
marker_name=self.image.DEFAULT_INTERACTIVE_MARKER_NAME,
182+
)
183+
assert self._get_marker_names_as_set() == set([self.image.DEFAULT_INTERACTIVE_MARKER_NAME])
172184

173185
# Clear markers to not pollute other tests.
174186
self.image.stop_marking(clear_markers=True)
175187

176188
assert self.image.is_marking is False
177-
self._check_marker_table_return_properties(self.image.get_markers(marker_name="all"))
189+
self._assert_empty_marker_table(self.image.get_markers(marker_name="all"))
178190

179191
# Hate this, should add to public API
180-
marknames = self.image._marktags
192+
marknames = self._get_marker_names_as_set()
181193
assert len(marknames) == 0
182194

183195
# Make sure that click_drag is restored as expected
184196
assert self.image.click_drag
185197

186198
def test_add_markers(self):
199+
original_marker_name = self.image.DEFAULT_MARKER_NAME
187200
data = np.arange(10).reshape(5, 2)
188201
orig_tab = Table(data=data, names=['x', 'y'], dtype=('float', 'float'))
189202
tab = Table(data=data, names=['x', 'y'], dtype=('float', 'float'))
190203
self.image.add_markers(tab, x_colname='x', y_colname='y',
191204
skycoord_colname='coord', marker_name='test1')
192205

193206
# Make sure setting didn't change the default name
194-
assert self.image._default_mark_tag_name == 'default-marker-name'
207+
assert self.image.DEFAULT_MARKER_NAME == original_marker_name
195208

196209
# Regression test for GitHub Issue 45:
197210
# Adding markers should not modify the input data table.
@@ -201,7 +214,7 @@ def test_add_markers(self):
201214
self.image.add_markers(tab, x_colname='x', y_colname='y',
202215
skycoord_colname='coord', marker_name='test2')
203216

204-
marknames = self.image._marktags
217+
marknames = self._get_marker_names_as_set()
205218
assert marknames == set(['test1', 'test2'])
206219
# assert self.image.get_marker_names() == ['test1', 'test2']
207220

@@ -223,7 +236,7 @@ def test_add_markers(self):
223236
assert (t2['y'] == expected['y']).all()
224237

225238
self.image.remove_markers(marker_name='test1')
226-
marknames = self.image._marktags
239+
marknames = self._get_marker_names_as_set()
227240
assert marknames == set(['test2'])
228241
# assert self.image.get_marker_names() == ['test2']
229242

@@ -238,24 +251,22 @@ def test_add_markers(self):
238251
skycoord_colname='coord')
239252
# Don't care about the order of the marker names so use set instead of
240253
# list.
241-
marknames = self.image._marktags
242-
assert (set(marknames) == set(['test2', self.image._default_mark_tag_name]))
243-
# assert (set(self.image.get_marker_names()) ==
244-
# set(['test2', self.image._default_mark_tag_name]))
254+
marknames = self._get_marker_names_as_set()
255+
assert (set(marknames) == set(['test2', self.image.DEFAULT_MARKER_NAME]))
245256

246257
# Clear markers to not pollute other tests.
247258
self.image.reset_markers()
248-
marknames = self.image._marktags
259+
marknames = self._get_marker_names_as_set()
249260
assert len(marknames) == 0
250-
self._check_marker_table_return_properties(self.image.get_markers(marker_name="all"))
261+
self._assert_empty_marker_table(self.image.get_markers(marker_name="all"))
251262
# Check that no markers remain after clearing
252-
tab = self.image.get_markers(marker_name=self.image._default_mark_tag_name)
253-
self._check_marker_table_return_properties(tab)
263+
tab = self.image.get_markers(marker_name=self.image.DEFAULT_MARKER_NAME)
264+
self._assert_empty_marker_table(tab)
254265

255266
# Check that retrieving a marker set that doesn't exist returns
256267
# an empty table with the right columns
257268
tab = self.image.get_markers(marker_name='test1')
258-
self._check_marker_table_return_properties(tab)
269+
self._assert_empty_marker_table(tab)
259270

260271
def test_get_markers_accepts_list_of_names(self):
261272
# Check that the get_markers method accepts a list of marker names
@@ -285,7 +296,7 @@ def test_remove_markers_name_all(self):
285296
self.image.add_markers(tab, marker_name='test2')
286297

287298
self.image.remove_markers(marker_name='all')
288-
self._check_marker_table_return_properties(self.image.get_markers(marker_name='all'))
299+
self._assert_empty_marker_table(self.image.get_markers(marker_name='all'))
289300

290301
def test_remove_marker_accepts_list(self):
291302
data = np.arange(10).reshape(5, 2)
@@ -295,7 +306,7 @@ def test_remove_marker_accepts_list(self):
295306

296307
self.image.remove_markers(marker_name=['test1', 'test2'])
297308
marks = self.image.get_markers(marker_name='all')
298-
self._check_marker_table_return_properties(marks)
309+
self._assert_empty_marker_table(marks)
299310

300311
def test_adding_markers_as_world(self, data, wcs):
301312
ndd = NDData(data=data, wcs=wcs)
@@ -304,14 +315,13 @@ def test_adding_markers_as_world(self, data, wcs):
304315
# Add markers using world coordinates
305316
pixels = np.linspace(0, 100, num=10).reshape(5, 2)
306317
marks_pix = Table(data=pixels, names=['x', 'y'], dtype=('float', 'float'))
307-
marks_world = wcs.pixel_to_world(marks_pix['x'], marks_pix['y'])
308-
marks_coords = SkyCoord(marks_world, unit='degree')
318+
marks_coords = wcs.pixel_to_world(marks_pix['x'], marks_pix['y'])
309319
mark_coord_table = Table(data=[marks_coords], names=['coord'])
310320
self.image.add_markers(mark_coord_table, use_skycoord=True)
311321
result = self.image.get_markers()
312322
# Check the x, y positions as long as we are testing things...
313323
# The first test had one entry that was zero, so any check
314-
# based on rtol will will. Added a small atol to make sure
324+
# based on rtol will not work. Added a small atol to make sure
315325
# the test passes.
316326
np.testing.assert_allclose(result['x'], marks_pix['x'], atol=1e-9)
317327
np.testing.assert_allclose(result['y'], marks_pix['y'])
@@ -326,7 +336,7 @@ def test_stretch(self):
326336

327337
original_stretch = self.image.stretch
328338

329-
with pytest.raises(ValueError, match='must be one of'):
339+
with pytest.raises(ValueError, match='[mM]ust be one of'):
330340
self.image.stretch = 'not a valid value'
331341

332342
# A bad value should leave the stretch unchanged
@@ -337,7 +347,7 @@ def test_stretch(self):
337347
assert self.image.stretch is not original_stretch
338348

339349
def test_cuts(self, data):
340-
with pytest.raises(ValueError, match='must be one of'):
350+
with pytest.raises(ValueError, match='[mM]ust be one of'):
341351
self.image.cuts = 'not a valid value'
342352

343353
with pytest.raises(ValueError, match='must have length 2'):
@@ -353,6 +363,7 @@ def test_cuts(self, data):
353363
self.image.cuts = (10, 100)
354364
assert self.image.cuts == (10, 100)
355365

366+
@pytest.mark.xfail(reason="Not clear whether colormap is part of the API")
356367
def test_colormap(self):
357368
cmap_desired = 'gray'
358369
cmap_list = self.image.colormap_options
@@ -380,10 +391,10 @@ def test_click_drag(self):
380391

381392
# If is_marking is true then trying to enable click_drag should fail
382393
self.image.click_drag = False
383-
self.image._is_marking = True
384-
with pytest.raises(ValueError, match=r'([Ii]nteractive marking)|(while in marking mode)'):
394+
self.image.start_marking()
395+
with pytest.raises(ValueError, match=r'([Ii]nteractive marking)|(while in marking mode)|(while marking is active)'):
385396
self.image.click_drag = True
386-
self.image._is_marking = False
397+
self.image.stop_marking()
387398

388399
def test_click_center(self):
389400
# Set this to ensure that click_center turns it off
@@ -397,12 +408,12 @@ def test_click_center(self):
397408
self.image.click_center = True
398409
assert not self.image.click_drag
399410

400-
# If is_marking is true then trying to enable click_center should fail
401-
self.image._is_marking = True
402411
self.image.click_center = False
403-
with pytest.raises(ValueError, match=r'([Ii]nteractive marking)|(while in marking mode)'):
412+
# If is_marking is true then trying to enable click_center should fail
413+
self.image.start_marking()
414+
with pytest.raises(ValueError, match=r'([Ii]nteractive marking)|(while marking is active)'):
404415
self.image.click_center = True
405-
self.image._is_marking = False
416+
self.image.stop_marking()
406417

407418
def test_scroll_pan(self):
408419
# Make sure scroll_pan is actually settable

0 commit comments

Comments
 (0)