Skip to content

Commit

Permalink
add more tests to improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zoghbi-a committed Jan 9, 2025
1 parent 594dfb2 commit 7cbcbc4
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions astroquery/heasarc/tests/test_heasarc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import shutil
import pytest
from unittest.mock import patch, PropertyMock
from astropy.coordinates import SkyCoord
from astropy.table import Table
import astropy.units as u
Expand Down Expand Up @@ -30,6 +31,55 @@
SIZE_LIST = [2 * u.arcmin, "0d2m0s"]


# used in the tap fixture
class MockTap:
class vColumn:
def __init__(self, name, desc, unit=None):
self.name = name
self.description = desc
self.unit = unit

class vTable:
def __init__(self, desc, cols=[]):
self.description = desc
self.columns = cols

cols = []
for icol in [1, 2, 3]:
cols.append(vColumn(f'col-{icol}', f'desc-{icol}'))

tables = {
'name-1': vTable('description-1 xmm', cols),
'name-2': vTable('description-2 chandra', cols),
'TAPname': None
}


@pytest.fixture
def mock_tap():
with patch('astroquery.heasarc.core.HeasarcClass.tap', new_callable=PropertyMock) as tap:
tap.return_value = MockTap()
yield tap


@pytest.fixture
def mock_default_cols():
with patch('astroquery.heasarc.core.HeasarcClass._get_default_columns') as get_cols:
get_cols.return_value = ['col-3', 'col-2']
yield get_cols


@pytest.fixture
def mock_meta():
with patch('astroquery.heasarc.core.HeasarcClass._meta', new_callable=PropertyMock) as meta:
meta.return_value = Table(dict(
table=['tab1', 'tab2', 'tab1', 'tab1'],
par=['p1', 'p2', 'p3', ''],
value=[1.2, 1.6, 2.0, 3.0]
))
yield meta


@pytest.mark.parametrize("coordinates", OBJ_LIST)
@pytest.mark.parametrize("radius", SIZE_LIST)
def test_query_region_cone(coordinates, radius):
Expand Down Expand Up @@ -143,6 +193,48 @@ def test_no_catalog():
Heasarc.query_region("m31", spatial="cone", columns="*")


def test_tap_def():
assert Heasarc._tap is None
Heasarc.tap
assert Heasarc._tap is not None


def test_meta_def():
class MockResult:
def to_table(self):
return Table({'value': ['1.5', '1.2', '-0.3']})
assert Heasarc._meta_info is None
with patch('astroquery.heasarc.core.HeasarcClass.query_tap') as mock_query_tap:
mock_query_tap.return_value = MockResult()
meta = Heasarc._meta
assert list(meta['value']) == [1.5, 1.2]


def test__get_default_columns(mock_meta):
cols = Heasarc._get_default_columns('tab1')
assert list(cols) == ['p1', 'p3']


def test__get_default_radius(mock_meta):
radius = Heasarc.get_default_radius('tab1')
assert radius.value == 3.0


def test_set_session():
with pytest.raises(ValueError):
Heasarc.set_session('not session object')


def test__list_catalogs(mock_tap):
catalogs = Heasarc.list_catalogs()
assert list(catalogs['name']) == [
lab for lab in MockTap().tables.keys() if 'TAP' not in lab
]
assert list(catalogs['description']) == [
desc.description for lab, desc in MockTap().tables.items() if 'TAP' not in lab
]


def test_list_catalogs_keywords_non_str():
with pytest.raises(ValueError, match="non-str found in keywords elements"):
Heasarc.list_catalogs(keywords=12)
Expand All @@ -153,6 +245,17 @@ def test_list_catalogs_keywords_list_non_str():
Heasarc.list_catalogs(keywords=['x-ray', 12])


def test__list_columns__missing_table(mock_tap):
with pytest.raises(ValueError, match="not available as a public catalog"):
Heasarc.list_columns(catalog_name='missing-table')


def test__list_columns(mock_tap, mock_default_cols):
cols = Heasarc.list_columns(catalog_name='name-1')
assert list(cols['name']) == ['col-2', 'col-3']
assert list(cols['description']) == ['desc-2', 'desc-3']


def test_get_datalink():
with pytest.raises(ValueError, match="query_result is None"):
Heasarc.get_datalinks()
Expand Down

0 comments on commit 7cbcbc4

Please sign in to comment.