-
Notifications
You must be signed in to change notification settings - Fork 187
/
test_core.py
93 lines (68 loc) · 2.55 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
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import absolute_import, division, print_function, unicode_literals
from collections import OrderedDict
import numpy as np
from numpy.testing import assert_allclose
from astropy.tests.helper import pytest, assert_quantity_allclose
from astropy.table import Table, Column
from astropy.units import Quantity
from ..core import SourceCatalog
def make_test_catalog():
table = Table()
table['Source_Name'] = ['a', 'bb', 'ccc']
table['RA'] = Column([42.2, 43.3, 44.4])
table['DEC'] = Column([1, 2, 3], unit='deg')
catalog = SourceCatalog(table)
return catalog
class TestSourceCatalog:
def setup(self):
self.cat = make_test_catalog()
def test_table(self):
assert_allclose(self.cat.table['RA'][1], 43.3)
def test_row_index(self):
idx = self.cat.row_index(name='bb')
assert idx == 1
with pytest.raises(KeyError):
self.cat.row_index(name='invalid')
def test_source_name(self):
name = self.cat.source_name(index=1)
assert name == 'bb'
with pytest.raises(IndexError):
self.cat.source_name(index=99)
# This seems to raise IndexError or ValueError with
# different Astropy versions, so we just check for
# any exception here
with pytest.raises(Exception):
self.cat.source_name('invalid')
def test_getitem(self):
source = self.cat['a']
assert source.data['Source_Name'] == 'a'
source = self.cat[0]
assert source.data['Source_Name'] == 'a'
source = self.cat[np.int(0)]
assert source.data['Source_Name'] == 'a'
with pytest.raises(KeyError):
self.cat['invalid']
with pytest.raises(IndexError):
self.cat[99]
with pytest.raises(ValueError):
self.cat[int]
class TestSourceCatalogObject:
def setup(self):
self.cat = make_test_catalog()
self.source = self.cat['bb']
def test_name(self):
assert self.source.name == 'bb'
def test_index(self):
assert self.source.index == 1
def test_data(self):
d = self.source.data
print(d)
assert isinstance(d, OrderedDict)
assert isinstance(d['RA'], float)
assert_allclose(d['RA'], 43.3)
assert isinstance(d['DEC'], Quantity)
assert_quantity_allclose(d['DEC'], Quantity(2, 'deg'))
def test_pprint(self):
# TODO: capture output and assert that it contains some substring
self.source.pprint()