Coverage for tests/test_serialization_backends.py: 100%

40 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-26 00:46 -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 Box, Image 

21from lsst.images import fits as images_fits 

22from lsst.images.serialization import Backend, backend_for_path 

23 

24 

25class BackendForPathTestCase(unittest.TestCase): 

26 """Tests for suffix -> backend resolution.""" 

27 

28 def test_fits(self) -> None: 

29 from lsst.images.fits import FitsInputArchive 

30 

31 b = backend_for_path("a/b/c.fits") 

32 self.assertIsInstance(b, Backend) 

33 self.assertEqual(b.name, "fits") 

34 self.assertIs(b.input_archive, FitsInputArchive) 

35 self.assertTrue(callable(b.write)) 

36 

37 def test_fits_gz(self) -> None: 

38 self.assertEqual(backend_for_path("c.fits.gz").name, "fits") 

39 

40 def test_json(self) -> None: 

41 from lsst.images.json import JsonInputArchive 

42 

43 b = backend_for_path("c.json") 

44 self.assertEqual(b.name, "json") 

45 self.assertIs(b.input_archive, JsonInputArchive) 

46 

47 def test_ndf(self) -> None: 

48 self.assertEqual(backend_for_path("c.sdf").name, "ndf") 

49 self.assertEqual(backend_for_path("c.ndf").name, "ndf") 

50 

51 def test_unknown(self) -> None: 

52 with self.assertRaises(ValueError) as cm: 

53 backend_for_path("c.txt") 

54 self.assertIn(".fits", str(cm.exception)) 

55 

56 

57class MinifyDispatchTestCase(unittest.TestCase): 

58 """minify resolves backend and schema via the shared APIs.""" 

59 

60 def test_minify_unsupported_schema_uses_shared_dispatch(self) -> None: 

61 from lsst.images.tests._minify_for_fixtures import minify 

62 

63 tmp = tempfile.mkdtemp() 

64 src = os.path.join(tmp, "plain.fits") 

65 out = os.path.join(tmp, "plain.json") 

66 images_fits.write(Image(np.zeros((4, 4), dtype=np.float32), bbox=Box.factory[0:4, 0:4]), src) 

67 # Reaching the "no subsetter" error proves backend_for_path and 

68 # get_basic_info ran and detected schema_name "image". 

69 with self.assertRaises(NotImplementedError) as cm: 

70 minify(src, out) 

71 self.assertIn("image", str(cm.exception)) 

72 

73 

74if __name__ == "__main__": 

75 unittest.main()