From dac58400e22caeebdeb40aad8ef277c8b712d532 Mon Sep 17 00:00:00 2001 From: Arthur Loureiro Date: Tue, 5 Dec 2023 19:20:33 +0100 Subject: [PATCH] added unit tests for functions in run_cosmosis --- .gitignore | 1 + tests/test_run_cosmosis_2pt.py | 57 ++++++++++++++++++++++++++++++++++ tests/test_twopt_utils.py | 16 ++++++++++ 3 files changed, 74 insertions(+) diff --git a/.gitignore b/.gitignore index 0213705..586f7c0 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ __pycache__/ *$py.class *.DS_Store *.png +*.ipy # C extensions *.so diff --git a/tests/test_run_cosmosis_2pt.py b/tests/test_run_cosmosis_2pt.py index e69de29..1373b0f 100644 --- a/tests/test_run_cosmosis_2pt.py +++ b/tests/test_run_cosmosis_2pt.py @@ -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) \ No newline at end of file diff --git a/tests/test_twopt_utils.py b/tests/test_twopt_utils.py index d383de2..64babb1 100644 --- a/tests/test_twopt_utils.py +++ b/tests/test_twopt_utils.py @@ -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):