From cffcb39c7c3250d05c6e992ca96efcb2295d04e7 Mon Sep 17 00:00:00 2001 From: Koudai Aono Date: Mon, 5 Aug 2019 20:46:56 +0900 Subject: [PATCH] add unittest for python3.7 --- tests/test_forward_ref.py | 140 ++++++++++++++++++++++++++++++-------- 1 file changed, 110 insertions(+), 30 deletions(-) diff --git a/tests/test_forward_ref.py b/tests/test_forward_ref.py index 88e140b..0fd5bed 100644 --- a/tests/test_forward_ref.py +++ b/tests/test_forward_ref.py @@ -42,11 +42,8 @@ def test_basic_forward_ref(create_module): module = create_module( """ from typing import Optional -try: - from typing import ForwardRef -except ImportError: - from typing import _ForwardRef as ForwardRef from pydantic import BaseModel +from pydantic.utils import ForwardRef class Foo(BaseModel): a: int @@ -65,11 +62,8 @@ class Bar(BaseModel): def test_self_forward_ref_module(create_module): module = create_module( """ -try: - from typing import ForwardRef -except ImportError: - from typing import _ForwardRef as ForwardRef from pydantic import BaseModel +from pydantic.utils import ForwardRef Foo = ForwardRef('Foo') @@ -89,11 +83,8 @@ def test_self_forward_ref_collection(create_module): module = create_module( """ from typing import List, Dict -try: - from typing import ForwardRef -except ImportError: - from typing import _ForwardRef as ForwardRef from pydantic import BaseModel +from pydantic.utils import ForwardRef Foo = ForwardRef('Foo') @@ -125,11 +116,8 @@ Foo.update_forward_refs() def test_self_forward_ref_local(create_module): module = create_module( """ -try: - from typing import ForwardRef -except ImportError: - from typing import _ForwardRef as ForwardRef from pydantic import BaseModel +from pydantic.utils import ForwardRef def main(): Foo = ForwardRef('Foo') @@ -150,11 +138,8 @@ def main(): def test_missing_update_forward_refs(create_module): module = create_module( """ -try: - from typing import ForwardRef -except ImportError: - from typing import _ForwardRef as ForwardRef from pydantic import BaseModel +from pydantic.utils import ForwardRef Foo = ForwardRef('Foo') @@ -183,16 +168,29 @@ class Dataclass: assert m.url == 'http://example.com' +@skip_not_37 +def test_forward_ref_dataclass_with_future_annotations(create_module): + module = create_module( + """ +from __future__ import annotations +from pydantic import UrlStr +from pydantic.dataclasses import dataclass + +@dataclass +class Dataclass: + url: UrlStr + """ + ) + m = module.Dataclass('http://example.com ') + assert m.url == 'http://example.com' + + def test_forward_ref_sub_types(create_module): module = create_module( """ from typing import Union -try: - from typing import ForwardRef -except ImportError: - from typing import _ForwardRef as ForwardRef from pydantic import BaseModel - +from pydantic.utils import ForwardRef class Leaf(BaseModel): a: str @@ -223,12 +221,8 @@ def test_forward_ref_nested_sub_types(create_module): module = create_module( """ from typing import Tuple, Union -try: - from typing import ForwardRef -except ImportError: - from typing import _ForwardRef as ForwardRef from pydantic import BaseModel - +from pydantic.utils import ForwardRef class Leaf(BaseModel): a: str @@ -294,6 +288,43 @@ Account.update_forward_refs() } +@skip_not_37 +def test_self_reference_json_schema_with_future_annotations(create_module): + module = create_module( + """ +from __future__ import annotations +from typing import List +from pydantic import BaseModel, Schema + +class Account(BaseModel): + name: str + subaccounts: List[Account] = [] + +Account.update_forward_refs() + """ + ) + Account = module.Account + assert Account.schema() == { + '$ref': '#/definitions/Account', + 'definitions': { + 'Account': { + 'title': 'Account', + 'type': 'object', + 'properties': { + 'name': {'title': 'Name', 'type': 'string'}, + 'subaccounts': { + 'title': 'Subaccounts', + 'default': [], + 'type': 'array', + 'items': {'$ref': '#/definitions/Account'}, + }, + }, + 'required': ['name'], + } + }, + } + + def test_circular_reference_json_schema(create_module): module = create_module( """ @@ -339,3 +370,52 @@ Owner.update_forward_refs() }, }, } + + +@skip_not_37 +def test_circular_reference_json_schema_with_future_annotations(create_module): + module = create_module( + """ +from __future__ import annotations +from typing import List +from pydantic import BaseModel, Schema + +class Owner(BaseModel): + account: Account + +class Account(BaseModel): + name: str + owner: Owner + subaccounts: List[Account] = [] + +Account.update_forward_refs() +Owner.update_forward_refs() + """ + ) + Account = module.Account + assert Account.schema() == { + '$ref': '#/definitions/Account', + 'definitions': { + 'Account': { + 'title': 'Account', + 'type': 'object', + 'properties': { + 'name': {'title': 'Name', 'type': 'string'}, + 'owner': {'$ref': '#/definitions/Owner'}, + 'subaccounts': { + 'title': 'Subaccounts', + 'default': [], + 'type': 'array', + 'items': {'$ref': '#/definitions/Account'}, + }, + }, + 'required': ['name', 'owner'], + }, + 'Owner': { + 'title': 'Owner', + 'type': 'object', + 'properties': {'account': {'$ref': '#/definitions/Account'}}, + 'required': ['account'], + }, + }, + }