예제 #1
0
    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'})
예제 #2
0
    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
예제 #3
0
    def test_dict_validate_from_openapi_data_instantiation(self):
        expected_call_by_index = {
            0: [
                Foo,
                (frozendict({"bar": "a"}), ),
                ValidationMetadata(path_to_item=("args[0]", ),
                                   from_server=True),
            ],
            1: [
                StrSchema,
                ("a", ),
                ValidationMetadata(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,
            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

        with patch.object(Schema, "_validate", new=new_validate):
            Foo._from_openapi_data({"bar": "a"})