Coverage for tests/test_pydantic_utils.py: 25%
80 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-13 09:58 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-13 09:58 +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/>.
28from __future__ import annotations
30import unittest
32import pydantic
33from astropy.time import Time
34from lsst.daf.butler.pydantic_utils import DeferredValidation, SerializableRegion, SerializableTime
35from lsst.sphgeom import ConvexPolygon, Mq3cPixelization
38class Inner(pydantic.BaseModel):
39 """Test model that will be wrapped with DeferredValidation."""
41 value: int
43 @pydantic.model_validator(mode="after")
44 def _validate(self, info: pydantic.ValidationInfo) -> Inner:
45 if info.context and "override" in info.context:
46 self.value = info.context["override"]
47 return self
50class SerializedInner(DeferredValidation[Inner]):
51 """Concrete test subclass of DeferredValidation."""
53 pass
56class OuterWithWrapper(pydantic.BaseModel):
57 """Test model that holds an `Inner` via `DeferredValidation`."""
59 inner: SerializedInner
62class OuterWithoutWrapper(pydantic.BaseModel):
63 """Test model that holds an `Inner` directly."""
65 inner: Inner
68class DeferredValidationTestCase(unittest.TestCase):
69 """Tests for `lsst.daf.butler.pydanic_utils.DeferredValidation`."""
71 def test_json_schema(self) -> None:
72 self.assertEqual(
73 OuterWithWrapper.model_json_schema()["properties"]["inner"], Inner.model_json_schema()
74 )
76 def test_dump_and_validate(self) -> None:
77 outer1a = OuterWithWrapper(inner=Inner(value=5))
78 json_str = outer1a.model_dump_json()
79 python_data = outer1a.model_dump()
80 outer1b = OuterWithoutWrapper(inner=Inner(value=5))
81 outer1c = OuterWithWrapper(inner=SerializedInner.from_validated(Inner.model_construct(value=5)))
82 self.assertEqual(json_str, outer1b.model_dump_json())
83 self.assertEqual(python_data, outer1b.model_dump())
84 self.assertEqual(json_str, outer1c.model_dump_json())
85 self.assertEqual(python_data, outer1c.model_dump())
86 self.assertEqual(OuterWithoutWrapper.model_validate_json(json_str).inner.value, 5)
87 self.assertEqual(OuterWithoutWrapper.model_validate(python_data).inner.value, 5)
88 outer2a = OuterWithWrapper.model_validate_json(json_str)
89 outer2b = OuterWithWrapper.model_validate(python_data)
90 self.assertEqual(outer2a.inner.validated().value, 5)
91 self.assertEqual(outer2b.inner.validated().value, 5)
92 self.assertIs(outer2a.inner.validated(), outer2a.inner.validated()) # caching
93 self.assertIs(outer2b.inner.validated(), outer2b.inner.validated()) # caching
94 outer2c = OuterWithWrapper.model_validate_json(json_str)
95 outer2d = OuterWithWrapper.model_validate(python_data)
96 self.assertEqual(outer2c.inner.validated(override=4).value, 4)
97 self.assertEqual(outer2d.inner.validated(override=4).value, 4)
98 self.assertIs(outer2c.inner.validated(override=4), outer2c.inner.validated(override=4)) # caching
99 self.assertIs(outer2d.inner.validated(override=4), outer2d.inner.validated(override=4)) # caching
102class SerializableExtensionsTestCase(unittest.TestCase):
103 """Tests for third-party types we add serializable annotations for."""
105 def test_region(self) -> None:
106 pixelization = Mq3cPixelization(10)
107 region = pixelization.pixel(12058823)
108 adapter = pydantic.TypeAdapter(SerializableRegion)
109 self.assertEqual(adapter.json_schema()["media"]["binaryEncoding"], "base16")
110 json_roundtripped = adapter.validate_json(adapter.dump_json(region))
111 self.assertIsInstance(json_roundtripped, ConvexPolygon)
112 self.assertEqual(json_roundtripped.getVertices(), region.getVertices())
113 python_roundtripped = adapter.validate_python(adapter.dump_python(region))
114 self.assertIsInstance(json_roundtripped, ConvexPolygon)
115 self.assertEqual(python_roundtripped.getVertices(), region.getVertices())
116 with self.assertRaises(ValueError):
117 adapter.validate_python(12)
118 with self.assertRaises(ValueError):
119 adapter.validate_json({})
120 with self.assertRaises(ValueError):
121 adapter.validate_json((b"this is not a region").hex())
122 with self.assertRaises(ValueError):
123 adapter.validate_json("this is not a hex string")
125 def test_time(self) -> None:
126 time = Time("2021-09-09T03:00:00", format="isot", scale="tai")
127 adapter = pydantic.TypeAdapter(SerializableTime)
128 self.assertIn("integer nanoseconds", adapter.json_schema()["description"])
129 json_roundtripped = adapter.validate_json(adapter.dump_json(time))
130 self.assertIsInstance(json_roundtripped, Time)
131 self.assertEqual(json_roundtripped, time)
132 python_roundtripped = adapter.validate_python(adapter.dump_python(time))
133 self.assertIsInstance(json_roundtripped, Time)
134 self.assertEqual(python_roundtripped, time)
135 with self.assertRaises(ValueError):
136 adapter.validate_python("one")
137 with self.assertRaises(ValueError):
138 adapter.validate_json({})
141if __name__ == "__main__":
142 unittest.main()