-
Notifications
You must be signed in to change notification settings - Fork 187
/
test_core.py
107 lines (77 loc) · 2.86 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
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import pytest
import numpy as np
from numpy.testing import assert_allclose
from astropy.table import Column, Table
from astropy.units import Quantity
from gammapy.catalog import SourceCatalog
from gammapy.utils.testing import assert_quantity_allclose
class SomeSourceCatalog(SourceCatalog):
"""Minimal test source catalog class for unit tests."""
name = "test123"
tag = "test123"
description = "Test source catalog"
def make_test_catalog():
table = Table()
table["Source_Name"] = ["a", "bb", "ccc"]
table["RA"] = Column([42.2, 43.3, 44.4], unit="deg")
table["DEC"] = Column([1, 2, 3], unit="deg")
return SomeSourceCatalog(table)
class TestSourceCatalog:
def setup(self):
self.cat = make_test_catalog()
def test_str(self):
assert "description" in str(self.cat)
assert "name" in str(self.cat)
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)
with pytest.raises(IndexError):
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.int32(0)]
assert source.data["Source_Name"] == "a"
with pytest.raises(KeyError):
self.cat["invalid"]
with pytest.raises(IndexError):
self.cat[99]
with pytest.raises(TypeError):
self.cat[1.2]
def test_positions(self):
positions = self.cat.positions
assert len(positions) == 3
def test_selection(self):
new = self.cat[self.cat.table["Source_Name"] != "a"]
assert len(new.table) == 2
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_row_index(self):
assert self.source.row_index == 1
def test_data(self):
d = self.source.data
assert isinstance(d, dict)
assert isinstance(d["RA"], Quantity)
assert_quantity_allclose(d["RA"], Quantity(43.3, "deg"))
assert isinstance(d["DEC"], Quantity)
assert_quantity_allclose(d["DEC"], Quantity(2, "deg"))
def test_position(self):
position = self.source.position
assert_allclose(position.ra.deg, 43.3)
assert_allclose(position.dec.deg, 2)