Esempio n. 1
0
 def test_empty_list_validate(self):
     vm = ValidationMetadata()
     path_to_schemas = ArrayHoldingAnyType._validate((),
                                                     validation_metadata=vm)
     assert path_to_schemas == {
         ("args[0]", ): set([ArrayHoldingAnyType, tuple])
     }
Esempio n. 2
0
 def test_str_validate(self):
     vm = ValidationMetadata()
     path_to_schemas = StringWithValidation._validate(
         "abcdefg", validation_metadata=vm)
     assert path_to_schemas == {
         ("args[0]", ): set([StringWithValidation, str])
     }
Esempio n. 3
0
 def test_number_validate(self):
     vm = ValidationMetadata()
     path_to_schemas = NumberWithValidations._validate(
         Decimal(11), validation_metadata=vm)
     assert path_to_schemas == {
         ("args[0]", ): set([NumberWithValidations, Decimal])
     }
Esempio n. 4
0
    def test_list_validate_direct_instantiation_cast_item(self):
        # validation is skipped if items are of the correct type
        expected_call_by_index = {
            0: [
                ArrayWithValidationsInItems,
                ((Decimal("7"), ), ),
                ValidationMetadata(path_to_item=("args[0]", )),
            ],
        }
        call_index = 0
        result_by_call_index = {
            0:
            defaultdict(
                set,
                [(("args[0]", ), set([ArrayWithValidationsInItems, tuple]))]),
        }

        @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

        item = ArrayWithValidationsInItems._items(7)
        with patch.object(Schema, "_validate", new=new_validate):
            ArrayWithValidationsInItems([item])
Esempio n. 5
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)
Esempio n. 6
0
    def test_list_validate_from_openai_data_instantiation(self):
        expected_call_by_index = {
            0: [
                ArrayWithValidationsInItems,
                ((Decimal("7"), ), ),
                ValidationMetadata(path_to_item=("args[0]", ),
                                   from_server=True),
            ],
            1: [
                ArrayWithValidationsInItems._items,
                (Decimal("7"), ),
                ValidationMetadata(path_to_item=("args[0]", 0),
                                   from_server=True),
            ],
        }
        call_index = 0
        result_by_call_index = {
            0:
            defaultdict(
                set,
                [(("args[0]", ), set([ArrayWithValidationsInItems, tuple]))]),
            1:
            defaultdict(
                set,
                [(("args[0]", 0),
                  set([ArrayWithValidationsInItems._items, Decimal]))],
            ),
        }

        @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):
            ArrayWithValidationsInItems._from_openapi_data([7])
Esempio n. 7
0
 def test_list_validate(self):
     vm = ValidationMetadata()
     path_to_schemas = ArrayHoldingAnyType._validate((Decimal(1), "a"),
                                                     validation_metadata=vm)
     assert path_to_schemas == {
         ("args[0]", ): set([ArrayHoldingAnyType, tuple]),
         ("args[0]", 0): set([AnyTypeSchema, Decimal]),
         ("args[0]", 1): set([AnyTypeSchema, str]),
     }
Esempio n. 8
0
 def test_oneof_composition_pig_validate(self):
     vm = ValidationMetadata()
     path_to_schemas = Pig._validate(frozendict(className="DanishPig"),
                                     validation_metadata=vm)
     assert path_to_schemas == {
         ("args[0]", ):
         set([Pig, DanishPig, frozendict]),
         ("args[0]", "className"):
         set([DanishPig.className, AnyTypeSchema, str]),
     }
Esempio n. 9
0
 def test_discriminated_dict_validate(self):
     vm = ValidationMetadata()
     path_to_schemas = Animal._validate(frozendict(className="Dog",
                                                   color="black"),
                                        validation_metadata=vm)
     assert path_to_schemas == {
         ("args[0]", ): set([Animal, Dog, DogAllOf, frozendict]),
         ("args[0]", "className"): set([StrSchema, AnyTypeSchema, str]),
         ("args[0]", "color"): set([StrSchema, AnyTypeSchema, str]),
     }
Esempio n. 10
0
 def test_anyof_composition_gm_fruit_validate(self):
     vm = ValidationMetadata()
     path_to_schemas = GmFruit._validate(
         frozendict(cultivar="GoldenDelicious", lengthCm=Decimal(10)),
         validation_metadata=vm,
     )
     assert path_to_schemas == {
         ("args[0]", ): set([GmFruit, Apple, Banana, frozendict]),
         ("args[0]", "cultivar"): set([Apple.cultivar, AnyTypeSchema, str]),
         ("args[0]", "lengthCm"):
         set([AnyTypeSchema, NumberSchema, Decimal]),
     }
Esempio n. 11
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]),
     }
Esempio n. 12
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])}
Esempio n. 13
0
 def test_nullable_enum_validate(self):
     vm = ValidationMetadata()
     path_to_schemas = StringEnum._validate(None, validation_metadata=vm)
     assert path_to_schemas == {("args[0]", ): set([StringEnum])}
Esempio n. 14
0
 def test_str_enum_validate(self):
     vm = ValidationMetadata()
     path_to_schemas = StringEnum._validate("placed",
                                            validation_metadata=vm)
     assert path_to_schemas == {("args[0]", ): set([StringEnum])}
Esempio n. 15
0
 def test_bool_enum_validate(self):
     vm = ValidationMetadata()
     path_to_schemas = BooleanEnum._validate(True, validation_metadata=vm)
     assert path_to_schemas == {("args[0]", ): set([BooleanEnum])}