File: test_factories.py

package info (click to toggle)
python-openapi-core 0.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,104 kB
  • sloc: python: 19,979; makefile: 44
file content (46 lines) | stat: -rw-r--r-- 1,365 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from dataclasses import dataclass
from dataclasses import is_dataclass
from sys import modules
from types import ModuleType
from typing import Any

import pytest
from jsonschema_path import SchemaPath

from openapi_core.extensions.models.factories import ModelPathFactory


class TestImportModelCreate:
    @pytest.fixture
    def loaded_model_class(self):
        @dataclass
        class BarModel:
            a: str
            b: int

        foo_module = ModuleType("foo")
        foo_module.BarModel = BarModel
        modules["foo"] = foo_module
        yield BarModel
        del modules["foo"]

    def test_dynamic_model(self):
        factory = ModelPathFactory()

        schema = SchemaPath.from_dict({"x-model": "TestModel"})
        test_model_class = factory.create(schema, ["name"])

        assert is_dataclass(test_model_class)
        assert test_model_class.__name__ == "TestModel"
        assert list(test_model_class.__dataclass_fields__.keys()) == ["name"]
        assert str(test_model_class.__dataclass_fields__["name"].type) == str(
            Any
        )

    def test_model_path(self, loaded_model_class):
        factory = ModelPathFactory()

        schema = SchemaPath.from_dict({"x-model-path": "foo.BarModel"})
        test_model_class = factory.create(schema, ["a", "b"])

        assert test_model_class == loaded_model_class