Coverage for tests/test_pydantic_utils.py: 40%
44 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-05 10:00 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-05 10:00 +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 lsst.daf.butler.pydantic_utils import DeferredValidation
36class Inner(pydantic.BaseModel):
37 """Test model that will be wrapped with DeferredValidation."""
39 value: int
41 @pydantic.model_validator(mode="after")
42 def _validate(self, info: pydantic.ValidationInfo) -> Inner:
43 if info.context and "override" in info.context:
44 self.value = info.context["override"]
45 return self
48class SerializedInner(DeferredValidation[Inner]):
49 """Concrete test subclass of DeferredValidation."""
51 pass
54class OuterWithWrapper(pydantic.BaseModel):
55 """Test model that holds an `Inner` via `DeferredValidation`."""
57 inner: SerializedInner
60class OuterWithoutWrapper(pydantic.BaseModel):
61 """Test model that holds an `Inner` directly."""
63 inner: Inner
66class DeferredValidationTestCase(unittest.TestCase):
67 """Tests for `lsst.daf.butle.pydanic_utils.DeferredValidation`."""
69 def test_json_schema(self) -> None:
70 self.assertEqual(
71 OuterWithWrapper.model_json_schema()["properties"]["inner"], Inner.model_json_schema()
72 )
74 def test_dump_and_validate(self) -> None:
75 outer1a = OuterWithWrapper(inner=Inner(value=5))
76 json_str = outer1a.model_dump_json()
77 python_data = outer1a.model_dump()
78 outer1b = OuterWithoutWrapper(inner=Inner(value=5))
79 outer1c = OuterWithWrapper(inner=SerializedInner.from_validated(Inner.model_construct(value=5)))
80 self.assertEqual(json_str, outer1b.model_dump_json())
81 self.assertEqual(python_data, outer1b.model_dump())
82 self.assertEqual(json_str, outer1c.model_dump_json())
83 self.assertEqual(python_data, outer1c.model_dump())
84 self.assertEqual(OuterWithoutWrapper.model_validate_json(json_str).inner.value, 5)
85 self.assertEqual(OuterWithoutWrapper.model_validate(python_data).inner.value, 5)
86 outer2a = OuterWithWrapper.model_validate_json(json_str)
87 outer2b = OuterWithWrapper.model_validate(python_data)
88 self.assertEqual(outer2a.inner.validated().value, 5)
89 self.assertEqual(outer2b.inner.validated().value, 5)
90 self.assertIs(outer2a.inner.validated(), outer2a.inner.validated()) # caching
91 self.assertIs(outer2b.inner.validated(), outer2b.inner.validated()) # caching
92 outer2c = OuterWithWrapper.model_validate_json(json_str)
93 outer2d = OuterWithWrapper.model_validate(python_data)
94 self.assertEqual(outer2c.inner.validated(override=4).value, 4)
95 self.assertEqual(outer2d.inner.validated(override=4).value, 4)
96 self.assertIs(outer2c.inner.validated(override=4), outer2c.inner.validated(override=4)) # caching
97 self.assertIs(outer2d.inner.validated(override=4), outer2d.inner.validated(override=4)) # caching
100if __name__ == "__main__":
101 unittest.main()