def test_dict_validate_from_openapi_data_instantiation(self):
        expected_call_by_index = {
            0: [
                Foo, (frozendict({'bar': 'a'}), ),
                InstantiationMetadata(path_to_item=('args[0]', ),
                                      from_server=True)
            ],
            1: [
                StrSchema, ('a', ),
                InstantiationMetadata(path_to_item=('args[0]', 'bar'),
                                      from_server=True)
            ]
        }
        call_index = 0
        result_by_call_index = {
            0: defaultdict(set, [(('args[0]', ), set([Foo, frozendict]))]),
            1: defaultdict(set, [(('args[0]', 'bar'), set([StrSchema, str]))]),
        }

        @classmethod
        def new_validate(cls,
                         *args,
                         _instantiation_metadata: typing.
                         Optional[InstantiationMetadata] = None):
            nonlocal call_index
            assert [cls, args, _instantiation_metadata
                    ] == expected_call_by_index[call_index]
            result = result_by_call_index.get(call_index)
            call_index += 1
            if result is None:
                raise petstore_api.ApiValueError('boom')
            return result

        with patch.object(Schema, '_validate', new=new_validate):
            Foo._from_openapi_data({'bar': 'a'})
    def test_empty_dict_validate(self):
        return_value = {('args[0]', ): set([Foo, frozendict])}
        with patch.object(Schema, '_validate',
                          return_value=return_value) as mock_validate:
            instance = Foo({})
            assert mock_validate.call_count == 1

        with patch.object(Schema, '_validate',
                          return_value=return_value) as mock_validate:
            Foo._from_openapi_data({})
            assert mock_validate.call_count == 1
Beispiel #3
0
    def test_dict_validate_direct_instantiation_cast_item(self):
        expected_call_by_index = {
            0: [
                Foo,
                (frozendict({"bar": "a"}), ),
                ValidationMetadata(path_to_item=("args[0]", )),
            ],
        }
        call_index = 0
        result_by_call_index = {
            0: defaultdict(set, [(("args[0]", ), set([Foo, frozendict]))]),
        }

        @classmethod
        def new_validate(
            cls,
            *args,
            validation_metadata: typing.Optional[ValidationMetadata] = None,
        ):
            nonlocal call_index
            assert [cls, args,
                    validation_metadata] == expected_call_by_index[call_index]
            result = result_by_call_index.get(call_index)
            call_index += 1
            if result is None:
                raise petstore_api.ApiValueError("boom")
            return result

        bar = StrSchema("a")
        with patch.object(Schema, "_validate", new=new_validate):
            Foo(bar=bar)
 def test_dict_validate(self):
     im = InstantiationMetadata()
     path_to_schemas = Foo._validate(frozendict({
         'bar': 'a',
         'additional': Decimal(0)
     }),
                                     _instantiation_metadata=im)
     assert path_to_schemas == {
         ('args[0]', ): set([Foo, frozendict]),
         ('args[0]', 'bar'): set([StrSchema, str]),
         ('args[0]', 'additional'): set([AnyTypeSchema, Decimal])
     }
Beispiel #5
0
 def test_dict_validate(self):
     vm = ValidationMetadata()
     path_to_schemas = Foo._validate(
         frozendict({
             "bar": "a",
             "additional": Decimal(0)
         }),
         validation_metadata=vm,
     )
     assert path_to_schemas == {
         ("args[0]", ): set([Foo, frozendict]),
         ("args[0]", "bar"): set([StrSchema, str]),
         ("args[0]", "additional"): set([AnyTypeSchema, Decimal]),
     }
 def test_empty_dict_validate(self):
     im = InstantiationMetadata()
     path_to_schemas = Foo._validate(frozendict({}),
                                     _instantiation_metadata=im)
     assert path_to_schemas == {('args[0]', ): set([Foo, frozendict])}
Beispiel #7
0
 def test_empty_dict_validate(self):
     vm = ValidationMetadata()
     path_to_schemas = Foo._validate(frozendict({}), validation_metadata=vm)
     assert path_to_schemas == {("args[0]", ): set([Foo, frozendict])}