File size: 7,399 Bytes
008c0fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import pytest
from unittest.mock import MagicMock, patch
from modules.DataLoading.DataIngestion import (
    create_loadingdata_output_box,
    load_event_data,
    save_loaded_files,
    delete_selected_files,
    preview_loaded_files,
    clear_loaded_files,
    create_event_list,
    simulate_event_list,
    create_warning_handler,
)


def test_create_loadingdata_output_box():
    content = "File loaded successfully."
    output_box = create_loadingdata_output_box(content)
    assert output_box.output_content == content


@patch("dataingestion.loaded_event_data", [])
def test_load_event_data_no_file_selected(
    output_box_container, warning_box_container, warning_handler, mock_file_selector, filename_input, format_input, format_checkbox
):
    # Set up file selector with no selection
    mock_file_selector.value = []
    load_event_data(
        event=None,
        file_selector=mock_file_selector,
        filename_input=filename_input,
        format_input=format_input,
        format_checkbox=format_checkbox,
        output_box_container=output_box_container,
        warning_box_container=warning_box_container,
        warning_handler=warning_handler,
    )
    assert "No file selected" in output_box_container[0].output_content


@patch("dataingestion.loaded_event_data", [])
@patch("dataingestion.EventList.read")
def test_load_event_data_success(mock_read, output_box_container, warning_box_container, warning_handler, mock_file_selector, filename_input, format_input, format_checkbox):
    # Mock EventList read to return a valid event
    mock_read.return_value = MagicMock()
    
    load_event_data(
        event=None,
        file_selector=mock_file_selector,
        filename_input=filename_input,
        format_input=format_input,
        format_checkbox=format_checkbox,
        output_box_container=output_box_container,
        warning_box_container=warning_box_container,
        warning_handler=warning_handler,
    )
    assert len(output_box_container) > 0
    assert "loaded successfully" in output_box_container[0].output_content


@patch("dataingestion.loaded_event_data", [("file1", MagicMock())])
def test_load_event_data_duplicate_file(
    output_box_container, warning_box_container, warning_handler, mock_file_selector, filename_input, format_input, format_checkbox
):
    # Test with duplicate file name
    filename_input.value = "file1"
    load_event_data(
        event=None,
        file_selector=mock_file_selector,
        filename_input=filename_input,
        format_input=format_input,
        format_checkbox=format_checkbox,
        output_box_container=output_box_container,
        warning_box_container=warning_box_container,
        warning_handler=warning_handler,
    )
    assert "already exists in memory" in output_box_container[0].output_content


@patch("dataingestion.os.path.exists", return_value=False)
@patch("dataingestion.loaded_event_data", [("file1", MagicMock())])
def test_save_loaded_files_success(mock_exists, output_box_container, warning_box_container, warning_handler, filename_input, format_input, format_checkbox):
    save_loaded_files(
        event=None,
        filename_input=filename_input,
        format_input=format_input,
        format_checkbox=format_checkbox,
        output_box_container=output_box_container,
        warning_box_container=warning_box_container,
        warning_handler=warning_handler,
    )
    assert "saved successfully" in output_box_container[0].output_content


@patch("dataingestion.os.path.exists", return_value=True)
@patch("dataingestion.loaded_event_data", [("file1", MagicMock())])
def test_save_loaded_files_duplicate_name(mock_exists, output_box_container, warning_box_container, warning_handler, filename_input, format_input, format_checkbox):
    save_loaded_files(
        event=None,
        filename_input=filename_input,
        format_input=format_input,
        format_checkbox=format_checkbox,
        output_box_container=output_box_container,
        warning_box_container=warning_box_container,
        warning_handler=warning_handler,
    )
    assert "already exists" in output_box_container[0].output_content


@patch("dataingestion.os.remove")
def test_delete_selected_files_success(mock_remove, output_box_container, warning_box_container, warning_handler, mock_file_selector):
    delete_selected_files(
        event=None,
        file_selector=mock_file_selector,
        output_box_container=output_box_container,
        warning_box_container=warning_box_container,
        warning_handler=warning_handler,
    )
    assert "deleted successfully" in output_box_container[0].output_content


def test_preview_loaded_files_no_data(output_box_container, warning_box_container, warning_handler):
    preview_loaded_files(
        event=None,
        output_box_container=output_box_container,
        warning_box_container=warning_box_container,
        warning_handler=warning_handler,
    )
    assert "No valid files or light curves loaded" in output_box_container[0].output_content


@patch("dataingestion.loaded_event_data", [("event1", MagicMock(time=[0.1, 0.2], mjdref=58000, gti=[[0, 1]]) )])
def test_preview_loaded_files_with_data(output_box_container, warning_box_container, warning_handler):
    preview_loaded_files(
        event=None,
        output_box_container=output_box_container,
        warning_box_container=warning_box_container,
        warning_handler=warning_handler,
    )
    assert "Event List - event1" in output_box_container[0].output_content


@patch("dataingestion.loaded_event_data", [("event1", MagicMock())])
def test_clear_loaded_files(output_box_container, warning_box_container):
    clear_loaded_files(
        event=None,
        output_box_container=output_box_container,
        warning_box_container=warning_box_container,
    )
    assert "cleared" in output_box_container[0].output_content


def test_create_event_list_missing_data(output_box_container, warning_box_container, warning_handler):
    create_event_list(
        event=None,
        times_input=MagicMock(value=""),
        energy_input=MagicMock(value=""),
        pi_input=MagicMock(value=""),
        gti_input=MagicMock(value=""),
        mjdref_input=MagicMock(value=""),
        name_input=MagicMock(value=""),
        output_box_container=output_box_container,
        warning_box_container=warning_box_container,
        warning_handler=warning_handler,
    )
    assert "Please enter Photon Arrival Times and MJDREF" in output_box_container[0].output_content


def test_simulate_event_list(output_box_container, warning_box_container, warning_handler):
    simulate_event_list(
        event=None,
        time_slider=MagicMock(value=10),
        count_slider=MagicMock(value=5),
        dt_input=MagicMock(value=0.1),
        name_input=MagicMock(value="simulated_event"),
        method_selector=MagicMock(value="Standard Method"),
        output_box_container=output_box_container,
        warning_box_container=warning_box_container,
        warning_handler=warning_handler,
    )
    assert "simulated successfully" in output_box_container[0].output_content


def test_create_warning_handler():
    handler = create_warning_handler()
    with pytest.warns(None) as record:
        handler.warn("Test warning", category=UserWarning)
    assert len(record) == 1
    assert record[0].message.args[0] == "Test warning"