|
| 1 | +"""UNIT TESTS FOR CFIS SUBPACKAGE. |
| 2 | +
|
| 3 | +This module contains unit tests for the cfis subpackage. |
| 4 | +
|
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +from numpy import testing as npt |
| 11 | +from astropy import units |
| 12 | + |
| 13 | +from unittest import TestCase |
| 14 | + |
| 15 | +from cs_util import cfis |
| 16 | + |
| 17 | + |
| 18 | +class CfisTestCase(TestCase): |
| 19 | + """Test case for the ``cfis`` module.""" |
| 20 | + |
| 21 | + def setUp(self): |
| 22 | + """Set test parameter values.""" |
| 23 | + |
| 24 | + self._size_tile = 0.5 * units.deg |
| 25 | + |
| 26 | + self._tile_number_ok = ['270.283', '188-308'] |
| 27 | + self._nix = ['270', '188'] |
| 28 | + self._niy = ['283', '308'] |
| 29 | + self._dec = [51.5, 64] |
| 30 | + self._ra = [216.86237, 214.43017] |
| 31 | + self._unit = units.deg |
| 32 | + |
| 33 | + self._nix_nok = ['23x', '1234'] |
| 34 | + self._tile_number_nok = '12x.456' |
| 35 | + |
| 36 | + def tearDown(self): |
| 37 | + """Unset test parameter values.""" |
| 38 | + self._tile_number_ok = None |
| 39 | + self._nix = None |
| 40 | + self._niy = None |
| 41 | + self._dec = None |
| 42 | + self._ra = None |
| 43 | + self._unit = None |
| 44 | + self._tile_number_nok = None |
| 45 | + |
| 46 | + def test_Cfis(self): |
| 47 | + """Test ``cs_util.Cfis`` class.""" |
| 48 | + self.assertTrue(self._size_tile == cfis.Cfis().size['tile']) |
| 49 | + |
| 50 | + def test_get_tile_number(self): |
| 51 | + """Test ``cs_util.get_tile_number`` method.""" |
| 52 | + |
| 53 | + # Test return values for valid input tile numbers |
| 54 | + for idx, tile_number_ok in enumerate(self._tile_number_ok): |
| 55 | + nix, niy = cfis.get_tile_number(tile_number_ok) |
| 56 | + self.assertTrue( |
| 57 | + (nix == self._nix[idx]) and (niy == self._niy[idx]), |
| 58 | + msg=f'{nix}!={self._nix[idx]} or {niy}!={self._niy[idx]}', |
| 59 | + ) |
| 60 | + |
| 61 | + self.assertRaises( |
| 62 | + ValueError, |
| 63 | + cfis.get_tile_number, |
| 64 | + self._tile_number_nok |
| 65 | + ) |
| 66 | + |
| 67 | + def test_get_tile_coord_from_nixy(self): |
| 68 | + """Test ``cs_util.get_tile_coord_from_nixy`` method.""" |
| 69 | + |
| 70 | + # Call with scalar arguments |
| 71 | + for idx in range(len(self._nix)): |
| 72 | + ra, dec = cfis.get_tile_coord_from_nixy( |
| 73 | + self._nix[idx], |
| 74 | + self._niy[idx], |
| 75 | + ) |
| 76 | + |
| 77 | + # Test values |
| 78 | + npt.assert_almost_equal( |
| 79 | + ra.value, |
| 80 | + self._ra[idx], |
| 81 | + err_msg=f'{ra}!={self._ra[idx]}', |
| 82 | + decimal=5, |
| 83 | + ) |
| 84 | + npt.assert_almost_equal( |
| 85 | + dec.value, |
| 86 | + self._dec[idx], |
| 87 | + err_msg=f'{dec}!={self._dec[idx]}', |
| 88 | + ) |
| 89 | + |
| 90 | + # Test units |
| 91 | + self.assertTrue(ra.unit == self._unit) |
| 92 | + self.assertTrue(dec.unit == self._unit) |
| 93 | + |
| 94 | + # Call with list arguments |
| 95 | + ra, dec = cfis.get_tile_coord_from_nixy(self._nix, self._niy) |
| 96 | + for idx in range(len(self._nix)): |
| 97 | + |
| 98 | + # Test values |
| 99 | + npt.assert_almost_equal( |
| 100 | + ra[idx].value, |
| 101 | + self._ra[idx], |
| 102 | + err_msg=f'{ra[idx]}!={self._ra[idx]}', |
| 103 | + decimal=5, |
| 104 | + ) |
| 105 | + npt.assert_almost_equal( |
| 106 | + dec[idx].value, |
| 107 | + self._dec[idx], |
| 108 | + err_msg=f'{dec[idx]}!={self._dec[idx]}', |
| 109 | + ) |
| 110 | + # Test units |
| 111 | + self.assertTrue(ra[idx].unit == self._unit) |
| 112 | + self.assertTrue(dec[idx].unit == self._unit) |
| 113 | + |
| 114 | + # Test exception for invalid input |
| 115 | + self.assertRaises( |
| 116 | + ValueError, |
| 117 | + cfis.get_tile_coord_from_nixy, |
| 118 | + self._nix_nok, |
| 119 | + self._niy, |
| 120 | + ) |
| 121 | + self.assertRaises( |
| 122 | + ValueError, |
| 123 | + cfis.get_tile_coord_from_nixy, |
| 124 | + self._niy, |
| 125 | + self._nix_nok, |
| 126 | + ) |
| 127 | + self.assertRaises( |
| 128 | + ValueError, |
| 129 | + cfis.get_tile_coord_from_nixy, |
| 130 | + self._nix_nok[0], |
| 131 | + self._niy[0], |
| 132 | + ) |
0 commit comments