|
15 | 15 | Tournament, |
16 | 16 | User, |
17 | 17 | json_pydantic_default, |
| 18 | + EnumFields, |
18 | 19 | ) |
19 | 20 | from tortoise.contrib import test |
20 | 21 | from tortoise.contrib.pydantic import ( |
@@ -1960,3 +1961,122 @@ def test_named_model_with_relations(self): |
1960 | 1961 | Pydantic2 = pydantic_model_creator(self.ModelWithRelations, name="Foo") |
1961 | 1962 |
|
1962 | 1963 | self.assertIs(Pydantic1, Pydantic2) |
| 1964 | + |
| 1965 | + |
| 1966 | +class TestPydanticEnum(test.TestCase): |
| 1967 | + def setUp(self) -> None: |
| 1968 | + self.EnumFields_Pydantic = pydantic_model_creator(EnumFields) |
| 1969 | + |
| 1970 | + def test_int_enum(self): |
| 1971 | + with self.assertRaises(ValidationError) as cm: |
| 1972 | + self.EnumFields_Pydantic.model_validate({"id": 1, "service": 4, "currency": "HUF"}) |
| 1973 | + self.assertEqual( |
| 1974 | + [ |
| 1975 | + { |
| 1976 | + 'type': 'enum', |
| 1977 | + 'loc': ('service',), |
| 1978 | + 'msg': 'Input should be 1, 2 or 3', |
| 1979 | + 'input': 4, |
| 1980 | + 'ctx': {'expected': '1, 2 or 3'} |
| 1981 | + } |
| 1982 | + ], |
| 1983 | + cm.exception.errors(include_url=False) |
| 1984 | + ) |
| 1985 | + with self.assertRaises(ValidationError) as cm: |
| 1986 | + self.EnumFields_Pydantic.model_validate({"id": 1, "service": "a string, not int", "currency": "HUF"}) |
| 1987 | + self.assertEqual( |
| 1988 | + [ |
| 1989 | + { |
| 1990 | + 'type': 'enum', |
| 1991 | + 'loc': ('service',), |
| 1992 | + 'msg': 'Input should be 1, 2 or 3', |
| 1993 | + 'input': "a string, not int", |
| 1994 | + 'ctx': {'expected': '1, 2 or 3'} |
| 1995 | + } |
| 1996 | + ], |
| 1997 | + cm.exception.errors(include_url=False) |
| 1998 | + ) |
| 1999 | + |
| 2000 | + def test_str_enum(self): |
| 2001 | + with self.assertRaises(ValidationError) as cm: |
| 2002 | + self.EnumFields_Pydantic.model_validate({"id": 1, "service": 3, "currency": "GoofyGooberDollar"}) |
| 2003 | + self.assertEqual( |
| 2004 | + [ |
| 2005 | + { |
| 2006 | + 'type': 'enum', |
| 2007 | + 'loc': ('currency',), |
| 2008 | + 'msg': "Input should be 'HUF', 'EUR' or 'USD'", |
| 2009 | + 'input': 'GoofyGooberDollar', |
| 2010 | + 'ctx': {'expected': "'HUF', 'EUR' or 'USD'"} |
| 2011 | + } |
| 2012 | + ], |
| 2013 | + cm.exception.errors(include_url=False) |
| 2014 | + ) |
| 2015 | + with self.assertRaises(ValidationError) as cm: |
| 2016 | + self.EnumFields_Pydantic.model_validate({"id": 1, "service": 3, "currency": 1}) |
| 2017 | + self.assertEqual( |
| 2018 | + [ |
| 2019 | + { |
| 2020 | + 'type': 'enum', |
| 2021 | + 'loc': ('currency',), |
| 2022 | + 'msg': "Input should be 'HUF', 'EUR' or 'USD'", |
| 2023 | + 'input': 1, |
| 2024 | + 'ctx': {'expected': "'HUF', 'EUR' or 'USD'"} |
| 2025 | + } |
| 2026 | + ], |
| 2027 | + cm.exception.errors(include_url=False) |
| 2028 | + ) |
| 2029 | + |
| 2030 | + def test_enum(self): |
| 2031 | + with self.assertRaises(ValidationError) as cm: |
| 2032 | + self.EnumFields_Pydantic.model_validate({"id": 1, "service": 4, "currency": 1}) |
| 2033 | + self.assertEqual( |
| 2034 | + [ |
| 2035 | + { |
| 2036 | + 'type': 'enum', |
| 2037 | + 'loc': ('service',), |
| 2038 | + 'msg': 'Input should be 1, 2 or 3', |
| 2039 | + 'input': 4, 'ctx': {'expected': '1, 2 or 3'} |
| 2040 | + }, |
| 2041 | + { |
| 2042 | + 'type': 'enum', |
| 2043 | + 'loc': ('currency',), |
| 2044 | + 'msg': "Input should be 'HUF', 'EUR' or 'USD'", |
| 2045 | + 'input': 1, |
| 2046 | + 'ctx': {'expected': "'HUF', 'EUR' or 'USD'"} |
| 2047 | + } |
| 2048 | + ], |
| 2049 | + cm.exception.errors(include_url=False) |
| 2050 | + ) |
| 2051 | + |
| 2052 | + # should simply not raise any error: |
| 2053 | + self.EnumFields_Pydantic.model_validate({"id": 1, "service": 3, "currency": "HUF"}) |
| 2054 | + |
| 2055 | + self.assertEqual( |
| 2056 | + { |
| 2057 | + 'additionalProperties': False, |
| 2058 | + 'properties': { |
| 2059 | + 'id': {'maximum': 2147483647, 'minimum': -2147483648, 'title': 'Id', 'type': 'integer'}, |
| 2060 | + 'service': { |
| 2061 | + 'description': 'python_programming: 1<br/>database_design: 2<br/>system_administration: 3', |
| 2062 | + 'enum': [1, 2, 3], |
| 2063 | + 'ge': -32768, |
| 2064 | + 'le': 32767, |
| 2065 | + 'title': 'Service', |
| 2066 | + 'type': 'integer' |
| 2067 | + }, |
| 2068 | + 'currency': { |
| 2069 | + 'default': 'HUF', |
| 2070 | + 'description': 'HUF: HUF<br/>EUR: EUR<br/>USD: USD', |
| 2071 | + 'enum': ['HUF', 'EUR', 'USD'], |
| 2072 | + 'maxLength': 3, |
| 2073 | + 'title': 'Currency', |
| 2074 | + 'type': 'string' |
| 2075 | + } |
| 2076 | + }, |
| 2077 | + 'required': ['id', 'service'], |
| 2078 | + 'title': 'EnumFields', |
| 2079 | + 'type': 'object' |
| 2080 | + }, |
| 2081 | + self.EnumFields_Pydantic.model_json_schema() |
| 2082 | + ) |
0 commit comments