11import unittest
22
3- from astropy .cosmology import WMAP9 , Planck15
3+ from astropy .cosmology import WMAP9
44from bilby .gw import cosmology
55import lal
6+ import pytest
7+
8+
9+ @pytest .fixture (autouse = True )
10+ def reset_cosmology ():
11+ # Reset cosmology before each test
12+ cosmology .DEFAULT_COSMOLOGY = None
13+ cosmology .COSMOLOGY = [None , str (None )]
14+ try :
15+ yield
16+ finally :
17+ # Reset cosmology after each test
18+ cosmology .DEFAULT_COSMOLOGY = None
19+ cosmology .COSMOLOGY = [None , str (None )]
620
721
822class TestSetCosmology (unittest .TestCase ):
@@ -12,12 +26,11 @@ def setUp(self):
1226 def test_setting_cosmology_with_string (self ):
1327 cosmology .set_cosmology ("WMAP9" )
1428 self .assertEqual (cosmology .COSMOLOGY [1 ], "WMAP9" )
15- cosmology . set_cosmology ( "Planck15 " )
29+ self . assertEqual ( cosmology . DEFAULT_COSMOLOGY . name , "WMAP9 " )
1630
1731 def test_setting_cosmology_with_astropy_object (self ):
1832 cosmology .set_cosmology (WMAP9 )
1933 self .assertEqual (cosmology .COSMOLOGY [1 ], "WMAP9" )
20- cosmology .set_cosmology (Planck15 )
2134
2235 def test_setting_cosmology_with_default (self ):
2336 cosmology .set_cosmology ()
@@ -38,6 +51,15 @@ def test_setting_cosmology_with_w_cdm_dict(self):
3851 cosmology .set_cosmology (cosmo_dict )
3952 self .assertEqual (cosmology .COSMOLOGY [1 ][:4 ], "wCDM" )
4053
54+ def test_repeated_setting_cosmology (self ):
55+ cosmology .set_cosmology ("WMAP9" )
56+ self .assertEqual (cosmology .COSMOLOGY [1 ], "WMAP9" )
57+ self .assertEqual (cosmology .DEFAULT_COSMOLOGY .name , "WMAP9" )
58+ cosmology .set_cosmology ("Planck18" )
59+ self .assertEqual (cosmology .COSMOLOGY [1 ], "Planck18" )
60+ self .assertEqual (cosmology .DEFAULT_COSMOLOGY .name , "Planck18" )
61+ cosmology .set_cosmology ("Planck15" )
62+
4163
4264class TestGetCosmology (unittest .TestCase ):
4365 def setUp (self ):
@@ -49,6 +71,10 @@ def test_getting_cosmology_with_string(self):
4971 def test_getting_cosmology_with_default (self ):
5072 self .assertEqual (cosmology .get_cosmology ().name , "Planck15" )
5173
74+ def test_getting_cosmology_non_standard_default (self ):
75+ cosmology .set_cosmology ("WMAP9" )
76+ self .assertEqual (cosmology .get_cosmology ().name , "WMAP9" )
77+
5278
5379class TestPlanck15LALCosmology (unittest .TestCase ):
5480
0 commit comments