Coverage for tests / test_pydantic_utils.py: 25%

95 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-24 08:17 +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# (http://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 <http://www.gnu.org/licenses/>. 

27 

28from __future__ import annotations 

29 

30import unittest 

31import uuid 

32 

33import pydantic 

34from astropy.time import Time 

35 

36from lsst.daf.butler.pydantic_utils import ( 

37 DeferredValidation, 

38 SerializableBytesHex, 

39 SerializableRegion, 

40 SerializableTime, 

41) 

42from lsst.sphgeom import ConvexPolygon, Mq3cPixelization 

43 

44 

45class Inner(pydantic.BaseModel): 

46 """Test model that will be wrapped with DeferredValidation.""" 

47 

48 value: int 

49 

50 @pydantic.model_validator(mode="after") 

51 def _validate(self, info: pydantic.ValidationInfo) -> Inner: 

52 if info.context and "override" in info.context: 

53 self.value = info.context["override"] 

54 return self 

55 

56 

57class SerializedInner(DeferredValidation[Inner]): 

58 """Concrete test subclass of DeferredValidation.""" 

59 

60 pass 

61 

62 

63class OuterWithWrapper(pydantic.BaseModel): 

64 """Test model that holds an `Inner` via `DeferredValidation`.""" 

65 

66 inner: SerializedInner 

67 

68 

69class OuterWithoutWrapper(pydantic.BaseModel): 

70 """Test model that holds an `Inner` directly.""" 

71 

72 inner: Inner 

73 

74 

75class DeferredValidationTestCase(unittest.TestCase): 

76 """Tests for `lsst.daf.butler.pydanic_utils.DeferredValidation`.""" 

77 

78 def test_json_schema(self) -> None: 

79 self.assertEqual( 

80 OuterWithWrapper.model_json_schema()["properties"]["inner"], Inner.model_json_schema() 

81 ) 

82 

83 def test_dump_and_validate(self) -> None: 

84 outer1a = OuterWithWrapper(inner=Inner(value=5)) 

85 json_str = outer1a.model_dump_json() 

86 python_data = outer1a.model_dump() 

87 outer1b = OuterWithoutWrapper(inner=Inner(value=5)) 

88 outer1c = OuterWithWrapper(inner=SerializedInner.from_validated(Inner.model_construct(value=5))) 

89 self.assertEqual(json_str, outer1b.model_dump_json()) 

90 self.assertEqual(python_data, outer1b.model_dump()) 

91 self.assertEqual(json_str, outer1c.model_dump_json()) 

92 self.assertEqual(python_data, outer1c.model_dump()) 

93 self.assertEqual(OuterWithoutWrapper.model_validate_json(json_str).inner.value, 5) 

94 self.assertEqual(OuterWithoutWrapper.model_validate(python_data).inner.value, 5) 

95 outer2a = OuterWithWrapper.model_validate_json(json_str) 

96 outer2b = OuterWithWrapper.model_validate(python_data) 

97 self.assertEqual(outer2a.inner.validated().value, 5) 

98 self.assertEqual(outer2b.inner.validated().value, 5) 

99 self.assertIs(outer2a.inner.validated(), outer2a.inner.validated()) # caching 

100 self.assertIs(outer2b.inner.validated(), outer2b.inner.validated()) # caching 

101 outer2c = OuterWithWrapper.model_validate_json(json_str) 

102 outer2d = OuterWithWrapper.model_validate(python_data) 

103 self.assertEqual(outer2c.inner.validated(override=4).value, 4) 

104 self.assertEqual(outer2d.inner.validated(override=4).value, 4) 

105 self.assertIs(outer2c.inner.validated(override=4), outer2c.inner.validated(override=4)) # caching 

106 self.assertIs(outer2d.inner.validated(override=4), outer2d.inner.validated(override=4)) # caching 

107 

108 

109class SerializableExtensionsTestCase(unittest.TestCase): 

110 """Tests for third-party types we add serializable annotations for.""" 

111 

112 def test_region(self) -> None: 

113 pixelization = Mq3cPixelization(10) 

114 region = pixelization.pixel(12058823) 

115 adapter = pydantic.TypeAdapter(SerializableRegion) 

116 self.assertEqual(adapter.json_schema()["media"]["binaryEncoding"], "base16") 

117 json_roundtripped = adapter.validate_json(adapter.dump_json(region)) 

118 self.assertIsInstance(json_roundtripped, ConvexPolygon) 

119 self.assertEqual(json_roundtripped.getVertices(), region.getVertices()) 

120 python_roundtripped = adapter.validate_python(adapter.dump_python(region)) 

121 self.assertIsInstance(json_roundtripped, ConvexPolygon) 

122 self.assertEqual(python_roundtripped.getVertices(), region.getVertices()) 

123 with self.assertRaises(ValueError): 

124 adapter.validate_python(12) 

125 with self.assertRaises(ValueError): 

126 adapter.validate_json({}) 

127 with self.assertRaises(ValueError): 

128 adapter.validate_json((b"this is not a region").hex()) 

129 with self.assertRaises(ValueError): 

130 adapter.validate_json("this is not a hex string") 

131 

132 def test_time(self) -> None: 

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

134 adapter = pydantic.TypeAdapter(SerializableTime) 

135 self.assertIn("integer nanoseconds", adapter.json_schema()["description"]) 

136 json_roundtripped = adapter.validate_json(adapter.dump_json(time)) 

137 self.assertIsInstance(json_roundtripped, Time) 

138 self.assertEqual(json_roundtripped, time) 

139 python_roundtripped = adapter.validate_python(adapter.dump_python(time)) 

140 self.assertIsInstance(json_roundtripped, Time) 

141 self.assertEqual(python_roundtripped, time) 

142 with self.assertRaises(ValueError): 

143 adapter.validate_python("one") 

144 with self.assertRaises(ValueError): 

145 adapter.validate_json({}) 

146 

147 def test_bytes_hex(self) -> None: 

148 b1 = uuid.uuid4().bytes 

149 adapter = pydantic.TypeAdapter(SerializableBytesHex) 

150 self.assertEqual(adapter.json_schema()["media"]["binaryEncoding"], "base16") 

151 json_roundtripped = adapter.validate_json(adapter.dump_json(b1)) 

152 self.assertIsInstance(json_roundtripped, bytes) 

153 self.assertEqual(json_roundtripped, b1) 

154 python_roundtripped = adapter.validate_python(adapter.dump_python(b1)) 

155 self.assertIsInstance(json_roundtripped, bytes) 

156 self.assertEqual(python_roundtripped, b1) 

157 with self.assertRaises(ValueError): 

158 adapter.validate_python("one") 

159 with self.assertRaises(ValueError): 

160 adapter.validate_json({}) 

161 

162 

163if __name__ == "__main__": 

164 unittest.main()