From d013471728c11cf6fd4847d3dd1bcd32ec4bc4ba Mon Sep 17 00:00:00 2001 From: GZhao Date: Mon, 13 May 2024 11:02:47 +0800 Subject: [PATCH] update unit test case --- tests/test_camera.py | 4 ++-- tests/test_io.py | 23 +++++++++++++---------- tests/test_optics.py | 4 ++-- tests/test_target.py | 17 +++-------------- 4 files changed, 20 insertions(+), 28 deletions(-) diff --git a/tests/test_camera.py b/tests/test_camera.py index 2188edf..1d8804e 100644 --- a/tests/test_camera.py +++ b/tests/test_camera.py @@ -61,8 +61,8 @@ class TestEMCCD(unittest.TestCase): self.assertAlmostEqual(emgain, 1.23, places=2) -if __name__ == '__main__': - unittest.main() +# if __name__ == '__main__': +# unittest.main() # from CpicImgSim.camera import CPIC_VIS_EMCCD # emccd = CPIC_VIS_EMCCD() # emccd.emgain_fun(1023, -30) diff --git a/tests/test_io.py b/tests/test_io.py index 5691cdf..27c5d98 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -46,15 +46,6 @@ camera = CpicVisEmccd() import logging class TestIO(unittest.TestCase): - - @mock.patch("os.makedirs") - def test_set_log(self, patch): - log = set_up_logger(config['log_dir']) - self.assertIsInstance(log, logging.Logger) - - self.assertRaises(FileNotFoundError, set_up_logger, 'new folder') - patch.assert_called_once_with('new folder') - def test_obsid_parser(self): self.assertRaises(ValueError, obsid_parser, '20190101') @@ -88,6 +79,14 @@ class TestIO(unittest.TestCase): @mock.patch("os.makedirs") @mock.patch("astropy.io.fits.writeto") def test_write_fits(self, patch_fits, patch_mkdir): + + log = set_up_logger(config['log_dir']) + self.assertIsInstance(log, logging.Logger) + + self.assertRaises(FileNotFoundError, set_up_logger, 'new folder') + + patch_mkdir.assert_called_once_with('new folder') + images = np.zeros((5, 10, 10)) yaml_str = """ obsid: 42012345678 @@ -115,7 +114,11 @@ target: parameters = yaml.load(yaml_str, Loader=yaml.FullLoader) tmp_folder_path = 'test_folder_for_unit_test' io.save_fits_simple(images, parameters, output_folder=tmp_folder_path) - patch_mkdir.assert_called_once_with(tmp_folder_path) + # patch_mkdir.assert_called_twice() + + dirname = patch_mkdir.call_args[0][0] + self.assertEqual(dirname, tmp_folder_path) + # patch_mkdir.assert_called_once_with(tmp_folder_path) output_name = patch_fits.call_args[0][0] self.assertEqual( output_name[:len(tmp_folder_path)], tmp_folder_path) diff --git a/tests/test_optics.py b/tests/test_optics.py index a297f17..7920186 100644 --- a/tests/test_optics.py +++ b/tests/test_optics.py @@ -54,8 +54,8 @@ class TestOptics(unittest.TestCase): self.assertIsInstance(bandpass, S.spectrum.SpectralElement) - def test_which_focalpalne(self): - self.assertEqual(which_focalplane('f565'), 'vis') + # def test_which_focalpalne(self): + # self.assertEqual(which_focalplane('f565'), 'vis') def test_ideal_focus_image(self): targets = [ diff --git a/tests/test_target.py b/tests/test_target.py index 7451169..303bfdc 100644 --- a/tests/test_target.py +++ b/tests/test_target.py @@ -44,24 +44,19 @@ class TestTarget(unittest.TestCase): self.assertEqual(old_planet.sp_model, 'bcc_planet') - def test_bcc_class(self): + def test_bcc_albedo_spectrum(self): spectrum = AlbedoCat(90, 1, 0) self.assertIsInstance(spectrum, S.spectrum.SpectralElement) - # import matplotlib.pyplot as plt - # plt.plot(spectrum.wave, spectrum.throughput) - # plt.show() - - def test_bcc_func(self): spectrum = bcc_spectrum(0.5, 0.5) self.assertIsInstance(spectrum, S.spectrum.SpectralElement) self.assertEqual(spectrum.waveunits.name, 'angstrom') # import matplotlib.pyplot as plt # plt.plot(spectrum.wave, spectrum.throughput) - # plt.xlabel(spectrum.waveunits) # plt.show() + def test_hybrid_albedo_spectrum(self): planet = hybrid_albedo_spectrum(0.5, 1) self.assertIsInstance(planet, S.spectrum.SpectralElement) @@ -144,7 +139,7 @@ class TestTarget(unittest.TestCase): self.assertRaises(FileExistsError, detect_template_path, 'demo_5_35.yaml') - def test_template_file_load(self): + def test_target_file_load(self): t0 = target_file_load({0:0}) self.assertEqual(t0[0], 0) @@ -228,9 +223,3 @@ class TestTarget(unittest.TestCase): spectrums = spectrum_generator(template_star) self.assertEqual(len(spectrums), 1) - - - - -if __name__ == '__main__': - unittest.main() -- GitLab