Skip to content

Commit 9caa176

Browse files
authored
Merge pull request #1862 from mfonism/fix-861
Prefer pytest-style tests (issue #861)
2 parents 0d57774 + 2bc3e44 commit 9caa176

24 files changed

Lines changed: 1428 additions & 1577 deletions

‎CHANGES.rst‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ esa/xmm_newton
3636
- new method ``get_epic_images`` is added to extract EPIC images from
3737
tarballs. [#1759]
3838

39+
esasky
40+
^^^^^^
41+
42+
- Converted unittest styled tests to pytest. [#1862]
43+
3944

4045
Gemini
4146
^^^^^^
@@ -72,6 +77,11 @@ UKIDSS
7277

7378
- Updated to ``UKIDSSDR11PLUS`` as the default version [#1767]
7479

80+
utils/tap
81+
^^^^^^
82+
83+
- Converted unittest styled tests to pytest. [#1862]
84+
7585
alma
7686
^^^^
7787

‎astroquery/esasky/tests/test_esasky.py‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import pytest
55

66
import os
7-
import unittest
87

98
from ...utils.testing_tools import MockResponse
109
from ...esasky import ESASky
@@ -40,19 +39,21 @@ def esasky_request(request):
4039
return mp
4140

4241

43-
class TestEsaSkyLocal(unittest.TestCase):
44-
def test_esasky_query_region_maps_invalid_position(self):
45-
with self.assertRaises(ValueError):
46-
ESASky.query_region_maps(51, "5 arcmin")
42+
def test_esasky_query_region_maps_invalid_position():
43+
with pytest.raises(ValueError):
44+
ESASky.query_region_maps(51, "5 arcmin")
4745

48-
def test_esasky_query_region_maps_invalid_radius(self):
49-
with self.assertRaises(ValueError):
50-
ESASky.query_region_maps("M51", 5)
5146

52-
def test_esasky_query_region_maps_invalid_mission(self):
53-
with self.assertRaises(ValueError):
54-
ESASky.query_region_maps("M51", "5 arcmin", missions=True)
47+
def test_esasky_query_region_maps_invalid_radius():
48+
with pytest.raises(ValueError):
49+
ESASky.query_region_maps("M51", 5)
5550

56-
def test_list_catalogs(self):
57-
result = ESASky.list_catalogs()
58-
assert (len(result) == 13)
51+
52+
def test_esasky_query_region_maps_invalid_mission():
53+
with pytest.raises(ValueError):
54+
ESASky.query_region_maps("M51", "5 arcmin", missions=True)
55+
56+
57+
def test_list_catalogs():
58+
result = ESASky.list_catalogs()
59+
assert len(result) == 13

‎astroquery/utils/commons.py‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ def radius_to_unit(radius, unit='degree'):
131131
if isinstance(unit, six.string_types):
132132
if hasattr(rad, unit):
133133
return getattr(rad, unit)
134-
elif hasattr(rad, unit + 's'):
135-
return getattr(rad, unit + 's')
134+
elif hasattr(rad, f"{unit}s"):
135+
return getattr(rad, f"{unit}s")
136136

137137
return rad.to(unit).value
138138

@@ -447,10 +447,9 @@ def get_stringio(self):
447447

448448
def __repr__(self):
449449
if hasattr(self, '_fits'):
450-
return "Downloaded FITS file: " + self._fits.__repr__()
450+
return f"Downloaded FITS file: {self._fits!r}"
451451
else:
452-
return ("Downloaded object from URL {} with ID {}"
453-
.format(self._target, id(self._readable_object)))
452+
return f"Downloaded object from URL {self._target} with ID {id(self._readable_object)}"
454453

455454

456455
def get_readable_fileobj(*args, **kwargs):

‎astroquery/utils/decorators.py‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def deprecate_function(func, message=message, since=since,
2727
message = ('Function {} has been deprecated since {}.'
2828
.format(func.__name__, since))
2929
if alternative is not None:
30-
message += '\n Use {} instead.'.format(alternative)
30+
message += f'\n Use {alternative} instead.'
3131

3232
@functools.wraps(func)
3333
def deprecated_func(*args, **kwargs):
@@ -41,7 +41,7 @@ def deprecate_class(cls, message=message, since=since,
4141
message = ('Class {} has been deprecated since {}.'
4242
.format(cls.__name__, since))
4343
if alternative is not None:
44-
message += '\n Use {} instead.'.format(alternative)
44+
message += f'\n Use {alternative} instead.'
4545

4646
cls.__init__ = deprecate_function(cls.__init__, message=message)
4747

@@ -144,11 +144,9 @@ def wrapper(*args, **kwargs):
144144
'and will be removed in a future version. '
145145
.format(old_name[i], since[i]))
146146
if new_name[i] is not None:
147-
message += ('Use argument "{}" instead.'
148-
.format(new_name[i]))
147+
message += f'Use argument "{new_name[i]}" instead.'
149148
elif alternative:
150-
message += ('\n Use {} instead.'
151-
.format(alternative))
149+
message += f'\n Use {alternative} instead.'
152150
warnings.warn(message, warning_type, stacklevel=2)
153151

154152
# Check if the newkeyword was given as well.

‎astroquery/utils/download_file_list.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
__doctest_skip__ = ["download_list_of_fitsfiles"]
1414

1515
whitespace_re = re.compile(r"\s")
16-
valid_chars = "-_.()%s%s" % (string.ascii_letters, string.digits)
16+
valid_chars = f"-_.(){string.ascii_letters}{string.digits}"
1717

1818

1919
def validify_filename(filestr):
@@ -96,7 +96,7 @@ def download_list_of_fitsfiles(linklist, output_directory=None,
9696
coordstr = output_coord_format.format(lon, lat)
9797
except TypeError:
9898
coordstr = output_coord_format % (lon, lat)
99-
nametxt += "_" + coordstr
99+
nametxt += f"_{coordstr}"
100100

101101
if include_input_filename:
102102
filename_root = os.path.split(link)[1]
@@ -105,13 +105,13 @@ def download_list_of_fitsfiles(linklist, output_directory=None,
105105

106106
savename = output_prefix if output_prefix else ""
107107
savename += nametxt
108-
savename += "_" + filename_root
108+
savename += f"_{filename_root}"
109109

110110
# Set final directory and file names
111-
final_file = output_directory + savename
111+
final_file = f"{output_directory}{savename}"
112112

113113
if verbose:
114-
print("Saving file %s" % final_file)
114+
print(f"Saving file {final_file}")
115115

116116
try:
117117
fitsfile.writeto(final_file, clobber=overwrite)

‎astroquery/utils/schema.py‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ def __init__(self, *args, **kw):
3232
self._error = kw.get('error')
3333

3434
def __repr__(self):
35-
return '%s(%s)' % (self.__class__.__name__,
36-
', '.join(repr(a) for a in self._args))
35+
return f"{self.__class__.__name__}({', '.join(repr(a) for a in self._args)})"
3736

3837
def validate(self, data):
3938
for s in [Schema(s, error=self._error) for s in self._args]:
@@ -50,7 +49,7 @@ def validate(self, data):
5049
return s.validate(data)
5150
except SchemaError as _x:
5251
x = _x
53-
raise SchemaError(['%r did not validate %r' % (self, data)] + x.autos,
52+
raise SchemaError([f'{self!r} did not validate {data!r}'] + x.autos,
5453
[self._error] + x.errors)
5554

5655

@@ -62,7 +61,7 @@ def __init__(self, callable_, error=None):
6261
self._error = error
6362

6463
def __repr__(self):
65-
return '%s(%r)' % (self.__class__.__name__, self._callable)
64+
return f'{self.__class__.__name__}({self._callable!r})'
6665

6766
def validate(self, data):
6867
try:
@@ -71,7 +70,7 @@ def validate(self, data):
7170
raise SchemaError([None] + x.autos, [self._error] + x.errors)
7271
except BaseException as x:
7372
f = self._callable.__name__
74-
raise SchemaError('%s(%r) raised %r' % (f, data, x), self._error)
73+
raise SchemaError(f'{f}({data!r}) raised {x!r}', self._error)
7574

7675

7776
def priority(s):
@@ -103,7 +102,7 @@ def __init__(self, schema, error=None):
103102
self._error = error
104103

105104
def __repr__(self):
106-
return '%s(%r)' % (self.__class__.__name__, self._schema)
105+
return f'{self.__class__.__name__}({self._schema!r})'
107106

108107
def validate(self, data):
109108
s = self._schema
@@ -141,30 +140,29 @@ def validate(self, data):
141140
new[nkey] = nvalue
142141
elif skey is not None:
143142
if x is not None:
144-
raise SchemaError(['key %r is required' % key] +
143+
raise SchemaError([f'key {key!r} is required'] +
145144
x.autos, [e] + x.errors)
146145
else:
147-
raise SchemaError('key %r is required' % skey, e)
146+
raise SchemaError(f'key {skey!r} is required', e)
148147
coverage = set(k for k in coverage if type(k) is not Optional)
149148
required = set(k for k in s if type(k) is not Optional)
150149
if coverage != required:
151-
raise SchemaError('missed keys %r' % (required - coverage), e)
150+
raise SchemaError(f'missed keys {(required - coverage)!r}', e)
152151
if len(new) != len(data):
153-
raise SchemaError('wrong keys %r in %r' % (new, data), e)
152+
raise SchemaError(f'wrong keys {new!r} in {data!r}', e)
154153
return new
155154
if hasattr(s, 'validate'):
156155
try:
157156
return s.validate(data)
158157
except SchemaError as x:
159158
raise SchemaError([None] + x.autos, [e] + x.errors)
160159
except BaseException as x:
161-
raise SchemaError('%r.validate(%r) raised %r' % (s, data, x),
162-
self._error)
160+
raise SchemaError(f'{s!r}.validate({data!r}) raised {x!r}', self._error)
163161
if type(s) is type:
164162
if isinstance(data, s):
165163
return data
166164
else:
167-
raise SchemaError('%r should be instance of %r' % (data, s), e)
165+
raise SchemaError(f'{data!r} should be instance of {s!r}', e)
168166
if callable(s):
169167
f = s.__name__
170168
try:
@@ -173,13 +171,12 @@ def validate(self, data):
173171
except SchemaError as x:
174172
raise SchemaError([None] + x.autos, [e] + x.errors)
175173
except BaseException as x:
176-
raise SchemaError('%s(%r) raised %r' % (f, data, x),
177-
self._error)
178-
raise SchemaError('%s(%r) should evaluate to True' % (f, data), e)
174+
raise SchemaError(f'{f}({data!r}) raised {x!r}', self._error)
175+
raise SchemaError(f'{f}({data!r}) should evaluate to True', e)
179176
if s == data:
180177
return data
181178
else:
182-
raise SchemaError('%r does not match %r' % (s, data), e)
179+
raise SchemaError(f'{s!r} does not match {data!r}', e)
183180

184181

185182
class Optional(Schema):

0 commit comments

Comments
 (0)