Coverage for tests/test_ndf_format_version.py: 27%

56 statements  

« 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. 

11 

12from __future__ import annotations 

13 

14import os 

15import tempfile 

16import unittest 

17 

18import numpy as np 

19 

20from lsst.images import Image 

21from lsst.images.serialization import ArchiveReadError 

22 

23try: 

24 import h5py 

25 

26 HAVE_H5PY = True 

27except ImportError: 

28 HAVE_H5PY = False 

29 

30 

31def _write_simple_image_ndf(path: str) -> None: 

32 """Write a tiny Image to ``path`` as an NDF.""" 

33 # Imported here rather than at module scope because importing 

34 # ``lsst.images.ndf`` hard-requires the optional ``h5py``; this helper is 

35 # only reached from tests that already skip when h5py is missing. 

36 from lsst.images.ndf import write as ndf_write 

37 

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

39 ndf_write(image, path) 

40 

41 

42@unittest.skipUnless(HAVE_H5PY, "NDF backend requires h5py") 

43class NdfFormatVersionTestCase(unittest.TestCase): 

44 """Tests for the NDF DATA_MODEL and FORMAT_VERSION components.""" 

45 

46 def test_write_emits_data_model_and_format_version(self): 

47 """A freshly-written NDF carries DATA_MODEL and FORMAT_VERSION.""" 

48 with tempfile.TemporaryDirectory() as tmp: 

49 path = os.path.join(tmp, "x.sdf") 

50 _write_simple_image_ndf(path) 

51 with h5py.File(path, "r") as f: 

52 self.assertIn("FORMAT_VERSION", f["/MORE/LSST"]) 

53 self.assertIn("DATA_MODEL", f["/MORE/LSST"]) 

54 

55 def test_read_succeeds_when_format_version_matches(self): 

56 """A freshly-written NDF reads successfully.""" 

57 from lsst.images.ndf import NdfInputArchive 

58 

59 with tempfile.TemporaryDirectory() as tmp: 

60 path = os.path.join(tmp, "x.sdf") 

61 _write_simple_image_ndf(path) 

62 with NdfInputArchive.open(path): 

63 pass 

64 

65 def test_read_fails_when_format_version_too_high(self): 

66 """A file with a newer FORMAT_VERSION raises ArchiveReadError.""" 

67 from lsst.images.ndf import NdfInputArchive 

68 

69 with tempfile.TemporaryDirectory() as tmp: 

70 path = os.path.join(tmp, "x.sdf") 

71 _write_simple_image_ndf(path) 

72 with h5py.File(path, "r+") as f: 

73 if "FORMAT_VERSION" in f["/MORE/LSST"]: 

74 del f["/MORE/LSST/FORMAT_VERSION"] 

75 f["/MORE/LSST"].create_dataset("FORMAT_VERSION", data=np.int32(2)) 

76 with self.assertRaises(ArchiveReadError): 

77 with NdfInputArchive.open(path): 

78 pass 

79 

80 def test_read_succeeds_when_format_version_absent(self): 

81 """A legacy file lacking FORMAT_VERSION reads (defaults to 1).""" 

82 from lsst.images.ndf import NdfInputArchive 

83 

84 with tempfile.TemporaryDirectory() as tmp: 

85 path = os.path.join(tmp, "x.sdf") 

86 _write_simple_image_ndf(path) 

87 with h5py.File(path, "r+") as f: 

88 if "FORMAT_VERSION" in f["/MORE/LSST"]: 

89 del f["/MORE/LSST/FORMAT_VERSION"] 

90 if "DATA_MODEL" in f["/MORE/LSST"]: 

91 del f["/MORE/LSST/DATA_MODEL"] 

92 with NdfInputArchive.open(path): 

93 pass 

94 

95 

96if __name__ == "__main__": 

97 unittest.main()