Coverage for tests/test_fits_format_version.py: 93%

41 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-16 15:24 -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. 

11 

12from __future__ import annotations 

13 

14from pathlib import Path 

15 

16import astropy.io.fits 

17import pytest 

18 

19from lsst.images import Image 

20from lsst.images.fits import FitsInputArchive 

21from lsst.images.serialization import ArchiveReadError, write_archive 

22 

23 

24def _write_simple_image_fits(path: Path | str) -> None: 

25 """Write a tiny Image to ``path`` via the high-level API.""" 

26 image = Image(0.0, shape=(4, 4), dtype="float32") 

27 write_archive(image, path) 

28 

29 

30def test_write_emits_fmtver_and_datamodl(tmp_path: Path) -> None: 

31 """Verify a freshly-written FITS carries FMTVER=1 and the root DATAMODL.""" 

32 path = tmp_path / "x.fits" 

33 _write_simple_image_fits(path) 

34 with astropy.io.fits.open(path) as hdul: 

35 assert hdul[0].header["FMTVER"] == 1 

36 assert hdul[0].header["DATAMODL"] == "https://images.lsst.io/schemas/image-1.0.0" 

37 

38 

39def test_read_succeeds_when_fmtver_matches(tmp_path: Path) -> None: 

40 """Verify that a round-trip read of a freshly-written file succeeds.""" 

41 path = tmp_path / "x.fits" 

42 _write_simple_image_fits(path) 

43 with FitsInputArchive.open(path): 

44 pass 

45 

46 

47def test_read_fails_when_fmtver_too_high(tmp_path: Path) -> None: 

48 """Verify that a file whose FMTVER is newer than this release raises.""" 

49 path = tmp_path / "x.fits" 

50 _write_simple_image_fits(path) 

51 with astropy.io.fits.open(path, mode="update") as hdul: 

52 hdul[0].header["FMTVER"] = 2 

53 hdul.flush() 

54 with pytest.raises(ArchiveReadError): 

55 with FitsInputArchive.open(path): 

56 pass 

57 

58 

59def test_read_succeeds_when_fmtver_absent(tmp_path: Path) -> None: 

60 """Verify a legacy file lacking FMTVER reads successfully. 

61 

62 The reader should default to format version 1 when FMTVER is absent. 

63 """ 

64 path = tmp_path / "x.fits" 

65 _write_simple_image_fits(path) 

66 with astropy.io.fits.open(path, mode="update") as hdul: 

67 if "FMTVER" in hdul[0].header: 67 ↛ 69line 67 didn't jump to line 69 because the condition on line 67 was always true

68 del hdul[0].header["FMTVER"] 

69 if "DATAMODL" in hdul[0].header: 69 ↛ 71line 69 didn't jump to line 71 because the condition on line 69 was always true

70 del hdul[0].header["DATAMODL"] 

71 hdul.flush() 

72 with FitsInputArchive.open(path): 

73 pass