test_main.py 3.22 KB
Newer Older
Chen Yili's avatar
Chen Yili committed
1
import unittest
GZhao's avatar
GZhao committed
2
3
import shutil
import os
GZhao's avatar
GZhao committed
4
5
6
# from CpicImgSim import observation_simulation, quick_run
from csst_cpic_sim.main import main, quick_run_v2, observation_simulation_from_config
from csst_cpic_sim.config import config
GZhao's avatar
GZhao committed
7

GZhao's avatar
GZhao committed
8
9
10
11
test_dir = os.path.dirname(__file__)
cases = os.path.join(test_dir, 'testcases')
output = os.path.join(test_dir, 'test_output')
config['output'] = output
GZhao's avatar
GZhao committed
12
# from unittest.mock import patch
Chen Yili's avatar
Chen Yili committed
13

GZhao's avatar
GZhao committed
14
a = []
GZhao's avatar
GZhao committed
15

GZhao's avatar
GZhao committed
16
17

def clear_output():
GZhao's avatar
GZhao committed
18
    if os.path.exists(output):
19
        shutil.rmtree(output)
GZhao's avatar
GZhao committed
20
21
    os.mkdir(output)

GZhao's avatar
GZhao committed
22

Chen Yili's avatar
Chen Yili committed
23
class TestMain(unittest.TestCase):
24
25
    # def test_help(self):
    #     main(argv = None)
GZhao's avatar
GZhao committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

    def test_quick_run_cmdline(self):
        clear_output()
        main([
            'quickrun',
            'demo_0_20',
            '0', '700', '1',
            '-r', '30',
            '-s', '19',
            '-ec',
            '-o', output
        ])
        file = os.listdir(output)
        self.assertEqual(len(file), 1)
        self.assertEqual(file[0][:9], 'demo_0_20')

    def test_quick_run_func(self):
        clear_output()
        quick_run_v2(
            '', 'f661', 0, 20, 1, skybg=None, camera_effect=False, output=output
Chen Yili's avatar
Chen Yili committed
46
        )
GZhao's avatar
GZhao committed
47
48
49
50
51
52
53
54
55
56
57
58
        file = os.listdir(output)
        self.assertEqual(len(file), 1)
        self.assertEqual(file[0][:5], 'blank')

    def test_full_run_func(self):
        clear_output()
        with open(cases+'/run_config.yaml', 'r') as fid:
            text = fid.read()

        text = text.replace('<output>', output)
        with open(output+'/run_config_new.yaml', 'w') as fid:
            fid.write(text)
Chen Yili's avatar
Chen Yili committed
59

GZhao's avatar
GZhao committed
60
61
62
        observation_simulation_from_config(
            os.path.join(cases, '05_sci.yaml'),
            os.path.join(output, 'run_config_new.yaml')
Chen Yili's avatar
Chen Yili committed
63
64
        )

GZhao's avatar
GZhao committed
65
66
        file = os.listdir(output)
        self.assertEqual(len(file), 4)
GZhao's avatar
GZhao committed
67

GZhao's avatar
GZhao committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
        clear_output()
        observation_simulation_from_config(
            os.path.join(cases, '05_sci.yaml'),
            None
        )
        file = os.listdir(output)
        self.assertEqual(len(file), 1)
        self.assertEqual(file[0][:5], 'SCI')

    def test_full_run_cmdline(self):
        clear_output()
        main(['run', os.path.join(cases, '05_sci.yaml')])
        file = os.listdir(output)
        self.assertEqual(len(file), 1)
        self.assertEqual(file[0][:5], 'SCI')

    # def test_main(self):
    #     target_example = {
    #         'name': 'test',
    #         'cstar': {
    #             'magnitude': 0,
    #             'ra': '120d',
    #             'dec': '40d',
    #             'distance': 10,
    #             'sptype': 'F0III',
    #         },
    #     }

    #     quick_run('', 10, 'f661', 1, 1, 30)
    #     quick_run('*2.4/10(3,5)/15(-4,2)', 21, 'f661', 1, 1, 30)

    #     # normal target
    #     observation_simulation(
    #         target=target_example,
    #         skybg=21,
    #         expt=1,
    #         nframe=1,
    #         band='f661',
    #         emgain=30,
    #         obsid=51012345678,
    #     )

    #     # bias-gain
    #     observation_simulation(
    #         target={},
    #         skybg=None,
    #         expt=1,
    #         nframe=1,
    #         band='f661',
    #         emgain=30,
    #         obsid=50112345678,
    #     )

Chen Yili's avatar
Chen Yili committed
121
122
123

if __name__ == '__main__':
    unittest.main()