dkist-header-validator 5.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,233 @@
1
+ from io import BytesIO
2
+ from pathlib import Path
3
+
4
+ import pytest
5
+ from astropy.io import fits
6
+ from astropy.io.fits.hdu.hdulist import HDUList
7
+
8
+ from dkist_header_validator import ReturnTypeException
9
+ from dkist_header_validator import spec122_validator
10
+ from dkist_header_validator import Spec122ValidationException
11
+
12
+
13
+ def test_translate_spec122_to_214_l0_base(valid_spec_122_object):
14
+ """
15
+ Validates and tries to translate a fits header against the SPEC-122 schema
16
+ Given: A valid SPEC-122 fits header
17
+ When: Validating headers
18
+ Then: return translated HDUList and do not raise an exception
19
+ """
20
+ # raises exception on failure
21
+ spec122_validator.validate_and_translate_to_214_l0(valid_spec_122_object)
22
+
23
+
24
+ def test_translate_required_only_headers_to_l0(valid_spec_122_object_required_only):
25
+ """
26
+ Validates and translates a spec122 compliant header with only the keywords required by the DC
27
+ Given: A spec122 compliant header with only required keywords
28
+ When: Validating and translating headers
29
+ Then: return a translated 214l0 HDUList and do not raise an exception
30
+ """
31
+ spec122_validator.validate_and_translate_to_214_l0(valid_spec_122_object_required_only)
32
+
33
+
34
+ def test_translate_expected_only_headers_to_l0(valid_spec_122_object_expected_only):
35
+ """
36
+ Validates and translates a spec122 compliant header with only the keywords expected by the DC
37
+ Given: A spec122 compliant header with only expected keywords
38
+ When: Validating and translating headers
39
+ Then: return a translated 214l0 HDUList and do not raise an exception
40
+ """
41
+ spec122_validator.validate_and_translate_to_214_l0(valid_spec_122_object_expected_only)
42
+
43
+
44
+ def test_translate_spec122_to_214_l0_return_dictionary(
45
+ valid_spec_122_object,
46
+ ):
47
+ """
48
+ Validates and tries to translate a fits header against the SPEC-122 schema
49
+ Given: A valid SPEC-122 fits header
50
+ When: Validating headers
51
+ Then: return translated dictionary and do not raise an exception
52
+ """
53
+ # raises exception on failure
54
+ result = spec122_validator.validate_and_translate_to_214_l0(
55
+ valid_spec_122_object, return_type=dict
56
+ )
57
+ assert isinstance(result, dict)
58
+
59
+
60
+ def test_translate_spec122_to_214_l0_return_header(
61
+ valid_spec_122_object,
62
+ ):
63
+ """
64
+ Validates and tries to translate a fits header against the SPEC-122 schema
65
+ Given: A valid SPEC-122 fits header
66
+ When: Validating headers
67
+ Then: return translated fits.header.Header object and do not raise an exception
68
+ """
69
+ # raises exception on failure
70
+ result = spec122_validator.validate_and_translate_to_214_l0(
71
+ valid_spec_122_object, return_type=fits.header.Header
72
+ )
73
+ assert isinstance(result, fits.header.Header)
74
+
75
+
76
+ def test_translate_spec122_to_214_l0_return_bytesio(valid_spec_122_file):
77
+ """
78
+ Validates and tries to translate a fits header against the SPEC-122 schema
79
+ Given: A valid SPEC-122 fits header
80
+ When: Validating headers
81
+ Then: return translated BytesIO object and do not raise an exception
82
+ """
83
+ # raises exception on failure
84
+ result = spec122_validator.validate_and_translate_to_214_l0(
85
+ valid_spec_122_file, return_type=BytesIO
86
+ )
87
+ assert isinstance(result, BytesIO)
88
+
89
+
90
+ def test_translate_spec122_to_214_l0_return_hdu(valid_spec_122_file):
91
+ """
92
+ Validates and tries to translate a fits header against the SPEC-122 schema
93
+ Given: A valid SPEC-122 fits header
94
+ When: Validating headers
95
+ Then: return translated PrimaryHDU object and do not raise an exception
96
+ """
97
+ # raises exception on failure
98
+ result = spec122_validator.validate_and_translate_to_214_l0(
99
+ valid_spec_122_file, return_type=fits.PrimaryHDU
100
+ )
101
+ assert isinstance(result, fits.PrimaryHDU)
102
+
103
+
104
+ def test_translate_spec122_to_214_l0_return_file(valid_spec_122_file):
105
+ """
106
+ Validates and tries to translate a fits file against the SPEC-122 schema
107
+ Given: A valid SPEC-122 fits file
108
+ When: Validating file
109
+ Then: return translated file object and do not raise an exception
110
+ """
111
+ # raises exception on failure
112
+ result = spec122_validator.validate_and_translate_to_214_l0(
113
+ valid_spec_122_file, return_type=Path
114
+ )
115
+ assert isinstance(result, Path)
116
+
117
+
118
+ def test_spec122_to_214_l0_extrakeys_allowed(valid_spec_122_object_extra_keys):
119
+ """
120
+ Validates a fits header against the SPEC-0122 schema
121
+ Given: A valid SPEC-0122 fits header with extra keys
122
+ When: Validating headers
123
+ Then: return HDUList and do not raise an exception
124
+ """
125
+ # raises exception on failure
126
+ spec122_validator.validate_and_translate_to_214_l0(valid_spec_122_object_extra_keys)
127
+
128
+
129
+ def test_spec122_to_214_l0_valid_extrakeys_not_allowed(valid_spec_122_object_extra_keys):
130
+ """
131
+ Validates a fits header against the SPEC-0122 schema
132
+ Given: A valid SPEC-0122 fits header with extra keys
133
+ When: Validating headers
134
+ Then: Raise a Spec122ValidationException
135
+ """
136
+ with pytest.raises(Spec122ValidationException):
137
+ spec122_validator.validate_and_translate_to_214_l0(
138
+ valid_spec_122_object_extra_keys, extra=False
139
+ )
140
+
141
+
142
+ def test_translate_compressed_spec122_to_214_l0(valid_spec_122_compressed):
143
+ """
144
+ Validates and translates a compressed spec122 compliant file
145
+ Given: A valid compressed SPEC-0122 file
146
+ When: Validating headers
147
+ Then: return valid HDUList and do not raise an exception
148
+ """
149
+ result = spec122_validator.validate_and_translate_to_214_l0(valid_spec_122_compressed)
150
+ assert isinstance(result, fits.HDUList)
151
+
152
+
153
+ def test_visp_translate_to_214_l0(valid_spec_122_visp):
154
+ """
155
+ Validates a visp fits header against the SPEC-122 schema
156
+ Given: A valid visp SPEC-122 fits header
157
+ When: Validating headers
158
+ Then: return validated HDUList and do not raise an exception
159
+ """
160
+ # raises exception on failure
161
+ spec122_validator.validate_and_translate_to_214_l0(valid_spec_122_visp, return_type=dict)
162
+
163
+
164
+ def test_translate_to_214_l0_return_primaryhdu(valid_spec_122_file):
165
+ """
166
+ Validates a fits file against the SPEC-122 schema
167
+ Given: A valid SPEC-122 fits file
168
+ When: Validating and translating headers
169
+ Then: return validated PrimaryHDU and do not raise an exception
170
+ """
171
+ # raises exception on failure
172
+ spec122_validator.validate_and_translate_to_214_l0(
173
+ valid_spec_122_file, return_type=fits.PrimaryHDU
174
+ )
175
+
176
+
177
+ def test_translate_to_214_l0_return_primaryhdu_fail(valid_spec_122_no_file):
178
+ """
179
+ Validates and tries to translate a fits header against the SPEC-122 schema
180
+ Given: A valid visp SPEC-122 fits header
181
+ When: Validating headers
182
+ Then: Raise a Spec122ValidationException
183
+ """
184
+ with pytest.raises(ReturnTypeException):
185
+ spec122_validator.validate_and_translate_to_214_l0(
186
+ valid_spec_122_no_file, return_type=fits.PrimaryHDU
187
+ )
188
+
189
+
190
+ def test_translate_to_214_l0_datainsecondhdu(valid_spec_122_second_hdu):
191
+ """
192
+ Validates headers with data stored in second HDU
193
+ Given: A valid SPEC-122 file or with data stored in second HDU
194
+ When: Validating and translating headers
195
+ Then: Raise an exception
196
+ """
197
+ # raises exception on failure
198
+ spec122_validator.validate_and_translate_to_214_l0(valid_spec_122_second_hdu, return_type=Path)
199
+
200
+
201
+ def test_translate_spec122_to_214_l0_check_remaining_122_keys(valid_spec_122_dict_only):
202
+ """
203
+ Validates and tries to translate a fits header against the SPEC-122 schema.
204
+ Checks to make sure that no original 122 keys were dropped.
205
+ Given: A valid SPEC-122 fits header
206
+ When: Validating headers
207
+ Then: return translated HDUList and do not raise an exception
208
+ """
209
+ # raises exception on failure
210
+ hdr = spec122_validator.validate_and_translate_to_214_l0(
211
+ valid_spec_122_dict_only, return_type=dict
212
+ )
213
+ for key in valid_spec_122_dict_only.keys():
214
+ if key not in hdr.keys():
215
+ raise KeyError(f" Keyword {key!r} from original header dropped during translation!")
216
+
217
+
218
+ def test_translate_bytesio_spec122_to_214_l0_return_primaryhdu(valid_spec_122_hdulist_only):
219
+ """
220
+ Validates and tries to translate a BytesIO object to a 214 l0 PrimaryHDU.
221
+ Given: A valid SPEC-122 BytesIO object
222
+ When: Validating and translating headers
223
+ Then: return translated PrimaryHDU and do not raise an exception
224
+ """
225
+
226
+ target = BytesIO()
227
+ valid_spec_122_hdulist_only.writeto(
228
+ target, output_verify="exception", overwrite=True, checksum=True
229
+ )
230
+ target.seek(0)
231
+ spec122_validator.validate_and_translate_to_214_l0(
232
+ input_headers=target, return_type=fits.PrimaryHDU, extra=True
233
+ )
@@ -0,0 +1,144 @@
1
+ from io import BytesIO
2
+ from pathlib import Path
3
+
4
+ from astropy.io import fits
5
+
6
+ from dkist_header_validator import spec122_validator
7
+
8
+
9
+ def test_spec122(valid_spec_122_object):
10
+ """
11
+ Validates a fits header against the SPEC-122 schema
12
+ Given: A valid SPEC-122 fits header
13
+ When: Validating headers
14
+ Then: return validated HDUList and do not raise an exception
15
+ """
16
+ # raises exception on failure
17
+ spec122_validator.validate(valid_spec_122_object, extra=False)
18
+
19
+
20
+ def test_spec122_return_dictionary(valid_spec_122_object):
21
+ """
22
+ Validates a fits header against the SPEC-122 schema
23
+ Given: A valid SPEC-122 fits header
24
+ When: Validating headers
25
+ Then: return validated dictionary and do not raise an exception
26
+ """
27
+ # raises exception on failure
28
+ result = spec122_validator.validate(valid_spec_122_object, return_type=dict, extra=False)
29
+ assert isinstance(result, dict)
30
+
31
+
32
+ def test_spec122_return_fits_header(valid_spec_122_object):
33
+ """
34
+ Validates a fits header against the SPEC-122 schema
35
+ Given: A valid SPEC-122 fits header
36
+ When: Validating headers
37
+ Then: return validated fits.header.Header object and do not raise an exception
38
+ """
39
+ # raises exception on failure
40
+ result = spec122_validator.validate(
41
+ valid_spec_122_object, return_type=fits.header.Header, extra=False
42
+ )
43
+ assert isinstance(result, fits.header.Header)
44
+
45
+
46
+ def test_spec122_return_BytesIO(valid_spec_122_file):
47
+ """
48
+ Validates a fits header against the SPEC-122 schema
49
+ Given: A valid SPEC-122 fits header
50
+ When: Validating headers
51
+ Then: return validated BytesIO object and do not raise an exception
52
+ """
53
+ # raises exception on failure
54
+ result = spec122_validator.validate(valid_spec_122_file, return_type=BytesIO, extra=False)
55
+ assert isinstance(result, BytesIO)
56
+
57
+
58
+ def test_spec122_return_HDU(valid_spec_122_file):
59
+ """
60
+ Validates a fits header against the SPEC-122 schema
61
+ Given: A valid SPEC-122 fits header
62
+ When: Validating headers
63
+ Then: return validated PrimaryHDU object and do not raise an exception
64
+ """
65
+ # raises exception on failure
66
+ result = spec122_validator.validate(
67
+ valid_spec_122_file, return_type=fits.PrimaryHDU, extra=False
68
+ )
69
+ assert isinstance(result, fits.PrimaryHDU)
70
+
71
+
72
+ def test_spec122_return_file(valid_spec_122_file):
73
+ """
74
+ Validates a fits header against the SPEC-122 schema
75
+ Given: A valid SPEC-122 fits header
76
+ When: Validating headers
77
+ Then: return validated file and do not raise an exception
78
+ """
79
+ # raises exception on failure
80
+ result = spec122_validator.validate(valid_spec_122_file, return_type=Path, extra=False)
81
+ assert isinstance(result, Path)
82
+
83
+
84
+ def test_validate_required_only_headers(valid_spec_122_object_required_only):
85
+ """
86
+ Validates a spec122 compliant header with only the keywords required by the DC
87
+ Given: A spec122 compliant header with only required keywords
88
+ When: Validating headers
89
+ Then: return a validated HDUList and do not raise an exception
90
+ """
91
+ spec122_validator.validate(valid_spec_122_object_required_only)
92
+
93
+
94
+ def test_validate_expected_only_headers(valid_spec_122_object_expected_only):
95
+ """
96
+ Validates a spec122 compliant header with only the keywords expected by the DC
97
+ Given: A spec122 compliant header with only exxpected keywords
98
+ When: Validating headers
99
+ Then: return a validated HDUList and do not raise an exception
100
+ """
101
+ spec122_validator.validate(valid_spec_122_object_expected_only)
102
+
103
+
104
+ def test_compressed_spec122_valid(valid_spec_122_compressed):
105
+ """
106
+ Validates a compressed spec122 compliant file
107
+ Given: A valid compressed SPEC-0122 file
108
+ When: Validating headers
109
+ Then: return valid HDUList and do not raise an exception
110
+ """
111
+ spec122_validator.validate(valid_spec_122_compressed)
112
+
113
+
114
+ def test_visp_spec122(valid_spec_122_visp):
115
+ """
116
+ Validates a visp fits header against the SPEC-122 schema
117
+ Given: A valid visp SPEC-122 fits header
118
+ When: Validating headers
119
+ Then: return validated HDUList and do not raise an exception
120
+ """
121
+ # raises exception on failure
122
+ spec122_validator.validate(valid_spec_122_visp, extra=False)
123
+
124
+
125
+ def test_datainsecondHDU(valid_spec_122_second_hdu):
126
+ """
127
+ Validates headers with data stored in second HDU
128
+ Given: A valid SPEC-122 file or with data stored in second HDU
129
+ When: Validating headers
130
+ Then: Do not raise an exception
131
+ """
132
+ # raises exception on failure
133
+ spec122_validator.validate(valid_spec_122_second_hdu, return_type=Path)
134
+
135
+
136
+ def test_casesensitivity(valid_spec_122_casesensitive):
137
+ """
138
+ Validates headers with a keyword that will need a case sensitive change
139
+ Given: Headers with a keyword that will need a case sensitive change
140
+ When: Validating headers
141
+ Then: Do not raise and exception
142
+ """
143
+ # raises exception on failure
144
+ spec122_validator.validate(valid_spec_122_casesensitive)
@@ -0,0 +1,118 @@
1
+ from io import BytesIO
2
+ from pathlib import Path
3
+
4
+ import numpy as np
5
+ import pytest
6
+
7
+ from dkist_header_validator import spec122_validator
8
+ from dkist_header_validator import Spec122ValidationException
9
+ from dkist_header_validator.exceptions import ReturnTypeException
10
+ from dkist_header_validator.exceptions import ValidationException
11
+
12
+
13
+ def test_spec122_return_BytesIO_without_data(valid_spec_122_no_file):
14
+ """
15
+ Validates a fits header against the SPEC-122 schema
16
+ Given: A valid SPEC-122 fits header, no data attached
17
+ When: Validating headers
18
+ Then: Raise return type exception
19
+ """
20
+ # raises exception on failure
21
+ with pytest.raises(ReturnTypeException):
22
+ result = spec122_validator.validate(
23
+ valid_spec_122_no_file, return_type=BytesIO, extra=False
24
+ )
25
+ assert isinstance(result, BytesIO)
26
+
27
+
28
+ def test_spec122_return_file_without_data(valid_spec_122_no_file):
29
+ """
30
+ Validates a fits header against the SPEC-122 schema
31
+ Given: A valid SPEC-122 fits header, no data attached
32
+ When: Validating headers
33
+ Then: raise a return type exception
34
+ """
35
+ # raises exception on failure
36
+ with pytest.raises(ReturnTypeException):
37
+ result = spec122_validator.validate(valid_spec_122_no_file, return_type=Path, extra=False)
38
+ assert isinstance(result, Path)
39
+
40
+
41
+ def test_spec122_invalid_headers(invalid_spec_122_object):
42
+ """
43
+ Validates an invalid fits header against the SPEC-0122 schema
44
+ Given: A invalid SPEC-0122 fits header
45
+ When: Validating headers
46
+ Then: raise a Spec122ValidationException
47
+ """
48
+ with pytest.raises(Spec122ValidationException):
49
+ spec122_validator.validate(invalid_spec_122_object)
50
+
51
+
52
+ @pytest.fixture(scope="module")
53
+ def invalid_file_params(tmpdir_factory):
54
+ """
55
+ Create a dict of invalid file params to be used in failing
56
+ tests below.
57
+ """
58
+ temp_dir = tmpdir_factory.mktemp("invalid_file_params_temp")
59
+ non_existent_file_name = temp_dir.join("tmp_fits_file.fits")
60
+ non_fits_file_name = temp_dir.join("tmp_this_is_not_a_fits_file.dat")
61
+ temp_array = np.ones(1, dtype=np.int16)
62
+ temp_array.tofile(str(non_fits_file_name))
63
+ yield {"file not found": non_existent_file_name, "file_not_fits": non_fits_file_name}
64
+
65
+
66
+ @pytest.fixture(scope="function", params=["file not found", "file_not_fits"])
67
+ def invalid_file_param(request, invalid_file_params):
68
+ yield invalid_file_params[request.param]
69
+
70
+
71
+ def test_file_errors(invalid_file_param):
72
+ """
73
+ Validates an invalid file spec
74
+ Given: A invalid file specification: non-existent file or non-fits file
75
+ When: Validating headers
76
+ Then: raise a Spec122ValidationException
77
+ """
78
+
79
+ with pytest.raises(ValidationException):
80
+ spec122_validator.validate(invalid_file_param)
81
+
82
+
83
+ def test_toomanyHDUs_translate_to_214_l0(valid_spec_122_too_many_HDUs):
84
+ """
85
+ Validates headers with too many (more than 2) HDUs
86
+ Given: A valid SPEC-122 file or HDUList with more than two headers
87
+ When: Validating and translating headers
88
+ Then: Raise an exception
89
+ """
90
+ # raises exception on failure
91
+ with pytest.raises(ValidationException):
92
+ spec122_validator.validate_and_translate_to_214_l0(valid_spec_122_too_many_HDUs)
93
+
94
+
95
+ def test_toomanyHDUs_validate(valid_spec_122_too_many_HDUs):
96
+ """
97
+ Validates headers with too many (more than 2) HDUs
98
+ Given: A valid SPEC-122 file or HDUList with more than two headers
99
+ When: Validating headers
100
+ Then: Raise an exception
101
+ """
102
+ # raises exception on failure
103
+ with pytest.raises(ValidationException):
104
+ spec122_validator.validate(valid_spec_122_too_many_HDUs)
105
+
106
+
107
+ @pytest.mark.parametrize("instrument", ["cryo-nirsp", "dlnirsp", "vbi", "visp", "vtf"])
108
+ def test_instrument_required_key_missing(invalid_instrument_table_spec_122_object):
109
+ """
110
+ Given: Headers from a specific instrument, but with one of the required header keys removed
111
+ When: Validating headers
112
+ Then: The correct Error is raised
113
+ """
114
+ fits_object, missing_key = invalid_instrument_table_spec_122_object
115
+ with pytest.raises(
116
+ Spec122ValidationException, match=f"'{missing_key}': 'required key not provided"
117
+ ):
118
+ spec122_validator.validate(fits_object)