Skip to content

Commit

Permalink
added unit tests for functions in run_cosmosis
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurmloureiro committed Dec 5, 2023
1 parent fed430f commit dac5840
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ __pycache__/
*$py.class
*.DS_Store
*.png
*.ipy

# C extensions
*.so
Expand Down
57 changes: 57 additions & 0 deletions tests/test_run_cosmosis_2pt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import pytest
from blind_2pt_cosmosis.run_cosmosis_2pt import setup_pipeline, modify_settings, run_pipeline, run_cosmosis_togen_2ptdict

def test_modify_settings():
# Setup
class MockIni:
def __init__(self):
self.__dict__ = {'_sections': {'test': {}, 'output': {}, 'pipeline': {}, 'shear_2pt_eplusb': {}, '2pt_gal': {}, 'fits_nz': {}}}

ini = MockIni()
angles_file = 'angles.fits'
nz_file = 'nz.fits'

# Exercise
result = modify_settings(ini, angles_file, nz_file)

# Verify
assert result._sections['test']['save_dir'] == ''
assert result._sections['output']['save_dir'] == ''
assert result._sections['pipeline']['debug'] == 'F'
assert result._sections['pipeline']['quiet'] == 'T'
assert result._sections['shear_2pt_eplusb']['theta_file'] == angles_file
assert result._sections['2pt_gal']['theta_file'] == angles_file
assert result._sections['fits_nz']['nz_file'] == nz_file

def test_modify_settings_no_files():
# Setup
class MockIni:
def __init__(self):
self.__dict__ = {'_sections': {'test': {}, 'output': {}, 'pipeline': {}, 'shear_2pt_eplusb': {}, '2pt_gal': {}, 'fits_nz': {}}}

ini = MockIni()

# Exercise
result = modify_settings(ini)

# Verify
assert result._sections['test']['save_dir'] == ''
assert result._sections['output']['save_dir'] == ''
assert result._sections['pipeline']['debug'] == 'F'
assert result._sections['pipeline']['quiet'] == 'T'
assert 'theta_file' not in result._sections['shear_2pt_eplusb']
assert 'theta_file' not in result._sections['2pt_gal']
assert 'nz_file' not in result._sections['fits_nz']

def test_modify_settings_missing_section():
# Setup
class MockIni:
def __init__(self):
self.__dict__ = {'_sections': {'test': {}, 'output': {}, 'pipeline': {}, 'shear_2pt_eplusb': {}, '2pt_gal': {}}}

ini = MockIni()
nz_file = 'nz.fits'

# Exercise and Verify
with pytest.raises(ValueError):
modify_settings(ini, None, nz_file)
16 changes: 16 additions & 0 deletions tests/test_twopt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ def test_spectrum_interp_log_ang():
assert interp.interp_type == 'log_ang'
assert np.isclose(interp(2), 0)

def test_get_twoptdict_from_pipeline_data():
# Setup
class MockData:
def has_section(self, section):
return section == 'mock_section'

mock_data = MockData()

# Exercise
result = get_twoptdict_from_pipeline_data(mock_data)
print(result)
# Verify
assert isinstance(result, dict)
# assert 'mock_key' in result
# assert result['mock_key'] == 'mock_value'

# # A mock datablock object for testing
# class MockDatablock:
# def has_section(self, section):
Expand Down

0 comments on commit dac5840

Please sign in to comment.