Skip to content

Commit 50d9f41

Browse files
author
martinkilbinger
committed
more tests of args
1 parent 52f38c6 commit 50d9f41

2 files changed

Lines changed: 53 additions & 5 deletions

File tree

cs_util/tests/test_args.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import pytest
1313

1414
from unittest import TestCase
15+
from unittest.mock import patch
1516

1617
from cs_util import args
1718

@@ -51,13 +52,17 @@ def setUp(self):
5152
"p_Bool": "bool option, set to True if given",
5253
"p_str": "string option, default={}",
5354
}
55+
self._string0 = None
56+
self._string1 = "abc"
57+
self._string3 = "a b c"
5458

5559
def tearDown(self):
5660
"""Unset test parameter values."""
5761
self._params_def = None
5862
self._types = None
59-
63+
self._short_options = None
6064
self._help_strings = None
65+
self._string1 = None
6166

6267
def test_parse_options(self):
6368
"""Test `cs_util.args.parse_options` method."""
@@ -79,4 +84,47 @@ def test_parse_options(self):
7984
npt.assert_equal(self._options["p_Bool"], False)
8085
npt.assert_equal(self._options["p_str_def"], "second_string")
8186

82-
# Test exceptions TBD
87+
# Test default args array
88+
test_argv = [None, "-i", "2"]
89+
with patch("sys.argv", test_argv):
90+
self._options = args.parse_options(
91+
self._params_def,
92+
self._short_options,
93+
self._types,
94+
self._help_strings,
95+
args=None,
96+
)
97+
npt.assert_equal(self._options["p_int"], 2)
98+
99+
def test_my_string_split(self):
100+
"""Test `cs_util.args.my_string_split` method."""
101+
102+
# Test string=None
103+
results = args.my_string_split(self._string0)
104+
self.assertIsNone(results)
105+
106+
# Test string without separator
107+
results = args.my_string_split(self._string1)
108+
npt.assert_equal(len(results), 1)
109+
npt.assert_equal(results[0], self._string1)
110+
111+
# Test string with separators
112+
results = args.my_string_split(self._string3)
113+
npt.assert_equal(len(results), 3)
114+
for idx in (0, 1, 2):
115+
npt.assert_equal(results[idx], self._string3[idx * 2])
116+
117+
# Test different separator
118+
results = args.my_string_split(self._string3, sep="_")
119+
npt.assert_equal(len(results), 1)
120+
npt.assert_equal(results[0], self._string3)
121+
122+
# Test mismatching number of substrings
123+
# Exception for stop=True
124+
with self.assertRaises(ValueError):
125+
args.my_string_split(self._string3, num=2, stop=True)
126+
# Return value when stop is False
127+
results = args.my_string_split(self._string3, num=2, stop=False)
128+
npt.assert_equal(len(results), 3)
129+
for idx in (0, 1, 2):
130+
npt.assert_equal(results[idx], self._string3[idx * 2])

cs_util/tests/test_cosmo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def setUp(self):
2727
self._z_lens = 0.5
2828
self._z_source_arr = [0.4, 0.6, 0.8, 0.9]
2929
self._nz_source_arr = [0.5, 0.6, 2.2, 1.6]
30-
self._cosmo = ccl.core.CosmologyVanillaLCDM()
30+
self._cosmo = ccl.CosmologyVanillaLCDM()
3131
self._sigma_crit_value = 3920.1478
3232
# Value verified with package dsigma as 3919.700
3333

@@ -87,7 +87,7 @@ def test_sigma_crit(self):
8787
npt.assert_almost_equal(
8888
sigma_crit.value,
8989
self._sigma_crit_value,
90-
decimal=4,
90+
decimal=3,
9191
)
9292
# Test return unit
9393
npt.assert_equal(sigma_crit.unit, self._sigma_crit_unit)
@@ -148,7 +148,7 @@ def test_sigma_crit_eff(self):
148148
npt.assert_almost_equal(
149149
sigma_crit_eff.value,
150150
self._sigma_crit_value_eff,
151-
decimal=4,
151+
decimal=3,
152152
)
153153

154154
# Test return unit

0 commit comments

Comments
 (0)