-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
test_core.py
361 lines (265 loc) · 12.2 KB
/
test_core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""Testing :mod:`astropy.cosmology.core`."""
##############################################################################
# IMPORTS
import abc
import inspect
from types import MappingProxyType
import pytest
import numpy as np
import astropy.units as u
from astropy.cosmology import Cosmology, core
from astropy.cosmology.core import _COSMOLOGY_CLASSES, Parameter
from astropy.table import Table, QTable
from .test_connect import ReadWriteTestMixin, ToFromFormatTestMixin
##############################################################################
# TESTS
##############################################################################
class TestParameter:
"""Test `astropy.cosmology.Parameter`."""
def setup_class(self):
class Example1(Cosmology):
param = Parameter(doc="example parameter",
unit=u.m, equivalencies=u.mass_energy())
def __init__(self, param=15):
self._param = param
class Example2(Example1):
def __init__(self, param=15 * u.m):
self._param = param.to_value(u.km)
@Example1.param.getter
def param(self):
return self._param << u.km
self.classes = {"Example1": Example1, "Example2": Example2}
def teardown_class(self):
for cls in self.classes.values():
_COSMOLOGY_CLASSES.pop(cls.__qualname__)
@pytest.fixture(params=["Example1", "Example2"])
def cosmo_cls(self, request):
yield self.classes[request.param]
@pytest.fixture
def cosmo(self, cosmo_cls):
yield cosmo_cls()
@pytest.fixture
def parameter(self, cosmo_cls):
yield cosmo_cls.param
# ==============================================================
def test_has_expected_attributes(self, parameter):
# property
assert hasattr(parameter, "fget") # None or callable
assert parameter.__doc__ == "example parameter"
# custom from init
assert parameter._fmt == ".3g"
assert parameter._unit == u.m
assert hasattr(parameter, "_equivalencies")
# custom from set_name
assert parameter._attr_name == "param"
assert parameter._attr_name_private == "_param"
def test_name(self, parameter):
"""Test :attr:`astropy.cosmology.Parameter.name`."""
assert parameter.name == "param"
def test_unit(self, parameter):
"""Test :attr:`astropy.cosmology.Parameter.unit`."""
assert parameter.unit is parameter._unit
assert parameter.unit == u.m
def test_equivalencies(self, parameter):
"""Test :attr:`astropy.cosmology.Parameter.equivalencies`."""
assert parameter.equivalencies == u.mass_energy()
def test_format_spec(self, parameter):
"""Test :attr:`astropy.cosmology.Parameter.format_spec`."""
# see test_format for more in-depth tests
assert parameter.format_spec is parameter._fmt
assert parameter._fmt == ".3g"
# -------------------------------------------
# descriptor methods
def test_get(self, cosmo_cls):
"""Test :meth:`astropy.cosmology.Parameter.__get__`."""
# from the class
parameter = cosmo_cls.param
assert isinstance(parameter, Parameter)
# from instance
value = cosmo_cls().param
if parameter.fget is None:
assert value == 15
else:
assert value == 15 * u.m
def test_set(self, cosmo):
"""Test :meth:`astropy.cosmology.Parameter.__set__`."""
with pytest.raises(AttributeError, match="can't set attribute"):
cosmo.param = 2
def test_delete(self, cosmo):
"""Test :meth:`astropy.cosmology.Parameter.__delete__`."""
with pytest.raises(AttributeError, match="can't delete attribute"):
del cosmo.param
# -------------------------------------------
# property-style methods
def test_getter_method(self, parameter):
"""Test :meth:`astropy.cosmology.Parameter.getter`."""
newparam = parameter.getter("NOT NONE")
assert newparam.fget == "NOT NONE"
# -------------------------------------------
# misc
def test_repr(self, cosmo_cls):
r = repr(cosmo_cls.param)
assert f"Parameter 'param'" in r
assert str(hex(id(cosmo_cls.param))) in r
# ==============================================================
def test_parameter_doesnt_change_with_generic_class(self):
"""Descriptors are initialized once and not updated on subclasses."""
class ExampleBase:
def __init__(self, param=15):
self._param = param
sig = inspect.signature(__init__)
_init_signature = sig.replace(parameters=list(sig.parameters.values())[1:])
param = Parameter(doc="example parameter")
class Example(ExampleBase): pass
assert Example.param is ExampleBase.param
def test_parameter_doesnt_change_with_cosmology(self, cosmo_cls):
"""Cosmology reinitializes all descriptors when a subclass is defined."""
# define subclass to show param is same
class Example(cosmo_cls): pass
assert Example.param is cosmo_cls.param
# unregister
_COSMOLOGY_CLASSES.pop(Example.__qualname__)
assert Example.__qualname__ not in _COSMOLOGY_CLASSES
# ========================================================================
class ParameterTestMixin:
"""Tests for a :class:`astropy.cosmology.Parameter` on a Cosmology."""
def test_Parameters(self, cosmo_cls):
"""Test `astropy.cosmology.Parameter` attached to Cosmology."""
# first establish has expected attribute
assert hasattr(cosmo_cls, "__parameters__")
# check that each entry is a Parameter
for n in cosmo_cls.__parameters__:
assert hasattr(cosmo_cls, n) # checks on the instance
assert isinstance(getattr(cosmo_cls, n), Parameter)
# the reverse: check that if it is a Parameter, it's listed.
for n in dir(cosmo_cls):
if isinstance(getattr(cosmo_cls, n), Parameter):
assert n in cosmo_cls.__parameters__
def test_Parameter_not_unique(self, cosmo_cls, clean_registry):
"""Cosmology reinitializes Parameter when a class is defined."""
# define subclass to show param is same
class ExampleBase(cosmo_cls):
param = Parameter()
class Example(ExampleBase): pass
assert Example.param is ExampleBase.param
assert Example.__parameters__ == ExampleBase.__parameters__
def test_Parameters_reorder_by_signature(self, cosmo_cls, clean_registry):
"""Test parameters are reordered."""
class Example(cosmo_cls):
param = Parameter()
def __init__(self, param, *, name=None, meta=None):
pass # never actually initialized
# param should be 1st, all other parameters next
Example.__parameters__[0] == "param"
# Check the other parameters are as expected.
assert set(Example.__parameters__[1:]) == set(cosmo_cls.__parameters__)
def test_make_from_Parameter(self, cosmo_cls, clean_registry):
"""Test the parameter creation process."""
class Example(cosmo_cls):
param = Parameter(unit=u.eV, equivalencies=u.mass_energy())
def __init__(self, param, *, name=None, meta=None):
cls = self.__class__
with u.add_enabled_equivalencies(cls.param.equivalencies):
self._param = param << cls.param.unit
assert Example(1).param == 1 * u.eV
assert Example(1 * u.eV).param == 1 * u.eV
assert Example(1 * u.J).param == (1 * u.J).to(u.eV)
assert Example(1 * u.kg).param == (1 * u.kg).to(u.eV, u.mass_energy())
class TestCosmology(ParameterTestMixin, ReadWriteTestMixin, ToFromFormatTestMixin,
metaclass=abc.ABCMeta):
"""Test :class:`astropy.cosmology.Cosmology`."""
def setup_class(self):
"""
Setup for testing.
Cosmology should not be instantiated, so tests are done on a subclass.
"""
class SubCosmology(Cosmology):
H0 = Parameter(unit=u.km / u.s / u.Mpc)
Tcmb0 = Parameter(unit=u.K)
def __init__(self, H0, Tcmb0=0*u.K, name=None, meta=None):
super().__init__(name=name, meta=meta)
self._H0 = H0
self._Tcmb0 = Tcmb0
self.cls = SubCosmology
self.cls_args = (70 * (u.km / u.s / u.Mpc), 2.7 * u.K)
self.cls_kwargs = dict(name=self.__class__.__name__, meta={"a": "b"})
def teardown_class(self):
_COSMOLOGY_CLASSES.pop("TestCosmology.setup_class.<locals>.SubCosmology", None)
@pytest.fixture
def cosmo_cls(self):
return self.cls
@pytest.fixture
def cosmo(self):
"""The cosmology instance with which to test."""
return self.cls(*self.cls_args, **self.cls_kwargs)
# ===============================================================
# Method & Attribute Tests
def _cosmo_test_init_attr(self, cosmo):
"""Helper function for testing ``__init__``."""
assert hasattr(cosmo, "_name")
assert cosmo._name is None or isinstance(cosmo._name, str)
assert hasattr(cosmo, "meta")
assert isinstance(cosmo.meta, dict)
def test_init(self, cosmo_cls):
"""Test initialization."""
cosmo1 = cosmo_cls(70, 2.7)
self._cosmo_test_init_attr(cosmo1)
# but only "name" and "meta" are used
cosmo2 = cosmo_cls(70, name="test", meta={"m": 1})
self._cosmo_test_init_attr(cosmo2)
assert cosmo2.name == "test"
assert cosmo2.meta["m"] == 1
# ------------------------------------------------
def test_is_equivalent(self, cosmo):
"""Test :meth:`astropy.cosmology.Cosmology.is_equivalent`."""
# to self
assert cosmo.is_equivalent(cosmo)
# same class, different instance
newclone = cosmo.clone(name="cloned")
assert cosmo.is_equivalent(newclone)
assert newclone.is_equivalent(cosmo)
# different class
assert not cosmo.is_equivalent(2)
# ------------------------------------------------
@pytest.mark.parametrize("in_meta", [True, False])
@pytest.mark.parametrize("table_cls", [Table, QTable])
def test_astropy_table(self, cosmo, table_cls, in_meta):
"""Test ``astropy.table.Table(cosmology)``."""
tbl = table_cls(cosmo, cosmology_in_meta=in_meta)
assert isinstance(tbl, table_cls)
# the name & all parameters are columns
for n in ("name", *cosmo.__parameters__):
assert n in tbl.colnames
assert all(tbl[n] == getattr(cosmo, n))
# check if Cosmology is in metadata or a column
if in_meta:
assert tbl.meta["cosmology"] == cosmo.__class__.__qualname__
assert "cosmology" not in tbl.colnames
else:
assert "cosmology" not in tbl.meta
assert tbl["cosmology"][0] == cosmo.__class__.__qualname__
# the metadata is transferred
for k, v in cosmo.meta.items():
assert np.all(tbl.meta[k] == v)
class CosmologySubclassTest(TestCosmology):
"""
Test subclasses of :class:`astropy.cosmology.Cosmology`.
This is broken away from ``TestCosmology``, because |Cosmology| is/will be
an ABC and subclasses must override some methods.
"""
@abc.abstractmethod
def setup_class(self):
"""Setup for testing."""
pass
# -----------------------------------------------------------------------------
class FlatCosmologyMixinTest:
"""Test :class:`astropy.cosmology.core.FlatCosmologyMixin`."""
def test_is_equivalent(self, cosmo):
"""Test :meth:`astropy.cosmology.core.FlatCosmologyMixin.is_equivalent`.
normally this would pass up via super(), but ``__equiv__`` is meant
to be overridden, so we skip super().
e.g. FlatFLRWMixinTest -> FlatCosmologyMixinTest -> TestCosmology
vs FlatFLRWMixinTest -> FlatCosmologyMixinTest -> TestFLRW -> TestCosmology
"""
CosmologySubclassTest.test_is_equivalent(self, cosmo)