def testNumberWithValidations(self):
        """Test NumberWithValidations"""
        valid_values = [10.0, 15.0, 20.0]
        for valid_value in valid_values:
            model = NumberWithValidations(valid_value)
            assert model.value == valid_value

        invalid_values = [9.0, 21.0]
        for invalid_value in invalid_values:
            with self.assertRaises(petstore_api.ApiValueError):
                NumberWithValidations(invalid_value)
Example #2
0
 def test_number_validate(self):
     im = InstantiationMetadata()
     path_to_schemas = NumberWithValidations._validate(
         Decimal(11), _instantiation_metadata=im)
     assert path_to_schemas == {
         ('args[0]', ): set([NumberWithValidations, Decimal])
     }
Example #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])
     }
Example #4
0
    def testNumberWithValidations(self):
        """Test NumberWithValidations"""
        valid_values = [10.0, 15.0, 20.0]
        for valid_value in valid_values:
            model = NumberWithValidations(valid_value)
            assert model == valid_value

        value_error_msg_pairs = (
            (9.0,
             r"Invalid value `9.0`, must be a value greater than or equal to `10` at \('args\[0\]',\)"
             ),
            (21.0,
             r"Invalid value `21.0`, must be a value less than or equal to `20` at \('args\[0\]',\)"
             ),
        )
        for invalid_value, error_msg in value_error_msg_pairs:
            with self.assertRaisesRegex(petstore_api.ApiValueError, error_msg):
                NumberWithValidations(invalid_value)
    def test_object_model_with_ref_props(self):
        """Test case for object_model_with_ref_props

        """
        from petstore_api.model.object_model_with_ref_props import ObjectModelWithRefProps
        from petstore_api.model.number_with_validations import NumberWithValidations
        endpoint = self.api.object_model_with_ref_props_endpoint
        assert endpoint.openapi_types['body'] == (ObjectModelWithRefProps, )
        assert endpoint.settings['response_type'] == (
            ObjectModelWithRefProps, )

        json_payloads = [
            {},  # only required + no optional properties works
            {  # optional properties works
                "my_number": 11.0,
                "my_string": 'a',
                "my_boolean": True,
            }
        ]
        # instantiation works
        expected_models = [
            ObjectModelWithRefProps(),
            ObjectModelWithRefProps(my_number=NumberWithValidations(11.0),
                                    my_string='a',
                                    my_boolean=True)
        ]

        pairs = zip(json_payloads, expected_models)
        # serialization + deserialization works
        for (json_payload, expected_model) in pairs:
            with patch.object(RESTClientObject, 'request') as mock_method:
                mock_method.return_value = self.mock_response(json_payload)

                response = self.api.object_model_with_ref_props(
                    body=expected_model)
                self.assert_request_called_with(
                    mock_method,
                    'http://petstore.swagger.io:80/v2/fake/refs/object_model_with_ref_props',
                    body=json_payload,
                )

                assert isinstance(response, expected_model.__class__)
                assert response == expected_model