test_main.py 3.25 KB
Newer Older
Chen Yili's avatar
Chen Yili committed
1
import unittest
GZhao's avatar
GZhao committed
2
3
4
5
6
7
8
9
10
# 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
import os
test_dir = os.path.dirname(__file__)
cases = os.path.join(test_dir, 'testcases')
output = os.path.join(test_dir, 'test_output')
config['output'] = output
from unittest.mock import patch
GZhao's avatar
GZhao committed
11
os.mkdir(output)
Chen Yili's avatar
Chen Yili committed
12

GZhao's avatar
GZhao committed
13
14
15
16
17
18
19
20
a = []
import io
import shutil

def clear_output():
    shutil.rmtree(output)
    os.mkdir(output)

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

    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
59
60
        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
61

GZhao's avatar
GZhao committed
62
63
64
        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
65
66
        )

GZhao's avatar
GZhao committed
67
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
121
122
123
124
        file = os.listdir(output)
        self.assertEqual(len(file), 4)
        
        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
125
126
127

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