Coverage for tests / test_column_spec.py: 56%

18 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-06 08:30 +0000

1# This file is part of daf_butler. 

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# This software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <https://www.gnu.org/licenses/>. 

27 

28from __future__ import annotations 

29 

30import unittest 

31import uuid 

32 

33from astropy.time import Time 

34 

35from lsst.daf.butler import Timespan 

36from lsst.daf.butler import column_spec as cs 

37from lsst.sphgeom import Mq3cPixelization, Region 

38 

39 

40class ColumnSpecTestCase(unittest.TestCase): 

41 """Tests for the ColumnSpec classes.""" 

42 

43 def setUp(self) -> None: 

44 self.specs = { 

45 int: cs.IntColumnSpec(name="i"), 

46 float: cs.FloatColumnSpec(name="f"), 

47 str: cs.StringColumnSpec(name="s", length=16), 

48 bytes: cs.HashColumnSpec(name="h", nbytes=16), 

49 bool: cs.BoolColumnSpec(name="b"), 

50 uuid.UUID: cs.UUIDColumnSpec(name="u"), 

51 Region: cs.RegionColumnSpec(name="r"), 

52 Timespan: cs.TimespanColumnSpec(name="t"), 

53 Time: cs.DateTimeColumnSpec(name="d"), 

54 } 

55 pix = Mq3cPixelization(10) 

56 self.data = [ 

57 { 

58 "i": 1, 

59 "f": 0.5, 

60 "s": "foo", 

61 "h": uuid.uuid4().bytes, 

62 "b": True, 

63 "u": uuid.uuid4(), 

64 "r": pix.pixel(12058870), 

65 "t": Timespan( 

66 Time("2021-09-09T03:00:00", format="isot", scale="tai"), 

67 Time("2021-09-09T03:01:00", format="isot", scale="tai"), 

68 ), 

69 "d": Time("2021-09-09T03:00:30", format="isot", scale="tai"), 

70 }, 

71 { 

72 "i": 2, 

73 "f": 0.25, 

74 "s": "bar", 

75 "h": uuid.uuid4().bytes, 

76 "b": False, 

77 "u": uuid.uuid4(), 

78 "r": pix.pixel(12058871), 

79 "t": Timespan( 

80 Time("2021-09-09T03:02:00", format="isot", scale="tai"), 

81 Time("2021-09-09T03:03:00", format="isot", scale="tai"), 

82 ), 

83 "d": Time("2021-09-09T03:02:30", format="isot", scale="tai"), 

84 }, 

85 ] 

86 

87 def test_serialize_tuple(self) -> None: 

88 """Test that we can use ColumnSpec to create validators and serializers 

89 for tuples with a specific sequence of types. 

90 """ 

91 type_adapter = cs.make_tuple_type_adapter(self.specs.values()) 

92 py0 = type_adapter.dump_python(tuple(self.data[0].values())) 

93 json0 = type_adapter.dump_json(tuple(self.data[0].values())) 

94 self.assertEqual(type_adapter.validate_python(py0), tuple(self.data[0].values())) 

95 self.assertEqual(type_adapter.validate_json(json0), tuple(self.data[0].values()))