Coverage for tests/test_fits_format_version.py: 26%
47 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-03 01:09 -0700
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-03 01:09 -0700
1# This file is part of lsst-images.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12from __future__ import annotations
14import os
15import tempfile
16import unittest
18import astropy.io.fits
20from lsst.images import Image
21from lsst.images.fits import FitsInputArchive
22from lsst.images.serialization import ArchiveReadError
25def _write_simple_image_fits(path: str) -> None:
26 """Write a tiny Image to ``path`` via the high-level API."""
27 image = Image(0.0, shape=(4, 4), dtype="float32")
28 image.write_fits(path)
31class FitsFormatVersionTestCase(unittest.TestCase):
32 """Tests for the FITS FMTVER and DATAMODL primary-header keywords."""
34 def test_write_emits_fmtver_and_datamodl(self):
35 """A freshly-written FITS carries FMTVER=1 and the root DATAMODL."""
36 with tempfile.TemporaryDirectory() as tmp:
37 path = os.path.join(tmp, "x.fits")
38 _write_simple_image_fits(path)
39 with astropy.io.fits.open(path) as hdul:
40 self.assertEqual(hdul[0].header["FMTVER"], 1)
41 self.assertEqual(
42 hdul[0].header["DATAMODL"],
43 "https://images.lsst.io/schemas/image-1.0.0",
44 )
46 def test_read_succeeds_when_fmtver_matches(self):
47 """Round-trip read of a freshly-written file succeeds without error."""
48 with tempfile.TemporaryDirectory() as tmp:
49 path = os.path.join(tmp, "x.fits")
50 _write_simple_image_fits(path)
51 with FitsInputArchive.open(path):
52 pass
54 def test_read_fails_when_fmtver_too_high(self):
55 """A file whose FMTVER is newer than this release raises."""
56 with tempfile.TemporaryDirectory() as tmp:
57 path = os.path.join(tmp, "x.fits")
58 _write_simple_image_fits(path)
59 with astropy.io.fits.open(path, mode="update") as hdul:
60 hdul[0].header["FMTVER"] = 2
61 hdul.flush()
62 with self.assertRaises(ArchiveReadError):
63 with FitsInputArchive.open(path):
64 pass
66 def test_read_succeeds_when_fmtver_absent(self):
67 """A legacy file lacking FMTVER reads successfully (defaults to 1)."""
68 with tempfile.TemporaryDirectory() as tmp:
69 path = os.path.join(tmp, "x.fits")
70 _write_simple_image_fits(path)
71 with astropy.io.fits.open(path, mode="update") as hdul:
72 if "FMTVER" in hdul[0].header:
73 del hdul[0].header["FMTVER"]
74 if "DATAMODL" in hdul[0].header:
75 del hdul[0].header["DATAMODL"]
76 hdul.flush()
77 with FitsInputArchive.open(path):
78 pass
81if __name__ == "__main__":
82 unittest.main()