Files
cloudkitty/cloudkitty/tests/test_validation_utils.py
Luka Peschke e05c00e117 Introduce validation utils
This adds a "validation_utils" module to cloudkitty, which aims at
centralizing common voluptuous helpers.

Work items:

* Create the validation_utils module, and move the "get_string_type" helper
  function to it. API-specific helpers have not been moved.

* Update the module of the "get_string_type" helper function in the
  documentation.

* Add two validators to the "validation_utils" module, allowing to check
  the types of a dict's values and keys.

Change-Id: I938f78b9987fcc4752b84cff8881ceecb5e20caf
Story: 2005890
Task: 35657
2019-08-21 16:20:03 +02:00

93 lines
3.7 KiB
Python

# Copyright 2019 Objectif Libre
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
import unittest
import voluptuous.error
from cloudkitty import validation_utils
class DictTypeValidatorTest(unittest.TestCase):
def test_dictvalidator_valid_dict_with_cast(self):
validator = validation_utils.DictTypeValidator(str, str)
self.assertEqual(validator({'a': '1', 'b': 2}), {'a': '1', 'b': '2'})
def test_dictvalidator_valid_dict_without_cast(self):
validator = validation_utils.DictTypeValidator(str, str, cast=False)
self.assertEqual(validator({'a': '1', 'b': '2'}), {'a': '1', 'b': '2'})
def test_dictvalidator_invalid_dict_without_cast(self):
validator = validation_utils.DictTypeValidator(str, str, cast=False)
self.assertRaises(
voluptuous.error.Invalid, validator, {'a': '1', 'b': 2})
def test_dictvalidator_invalid_dict_with_cast(self):
validator = validation_utils.DictTypeValidator(str, int)
self.assertRaises(
voluptuous.error.Invalid, validator, {'a': '1', 'b': 'aa'})
def test_dictvalidator_invalid_type_tuple(self):
validator = validation_utils.DictTypeValidator(str, int)
self.assertRaises(
voluptuous.error.Invalid, validator, ('a', '1'))
def test_dictvalidator_invalid_type_str(self):
validator = validation_utils.DictTypeValidator(str, int)
self.assertRaises(
voluptuous.error.Invalid, validator, 'aaaa')
class IterableValuesDictTest(unittest.TestCase):
def test_iterablevaluesdict_valid_list_and_tuple_with_cast(self):
validator = validation_utils.IterableValuesDict(str, str)
self.assertEqual(
validator({'a': [1, '2'], 'b': ('3', 4)}),
{'a': ['1', '2'], 'b': ('3', '4')},
)
def test_iterablevaluesdict_valid_list_and_tuple_without_cast(self):
validator = validation_utils.IterableValuesDict(str, str)
self.assertEqual(
validator({'a': ['1', '2'], 'b': ('3', '4')}),
{'a': ['1', '2'], 'b': ('3', '4')},
)
def test_iterablevaluesdict_invalid_dict_iterable_without_cast(self):
validator = validation_utils.IterableValuesDict(str, str, cast=False)
self.assertRaises(
voluptuous.error.Invalid, validator, {'a': ['1'], 'b': (2, )})
def test_iterablevaluesdict_invalid_dict_iterable_with_cast(self):
validator = validation_utils.IterableValuesDict(str, int, cast=False)
self.assertRaises(
voluptuous.error.Invalid, validator, {'a': ['1'], 'b': ('aa', )})
def test_iterablevaluesdict_invalid_iterable_with_cast(self):
validator = validation_utils.IterableValuesDict(str, int)
self.assertRaises(
voluptuous.error.Invalid, validator, {'a': ['1'], 'b': 42, })
def test_iterablevaluesdict_invalid_type_tuple(self):
validator = validation_utils.IterableValuesDict(str, int)
self.assertRaises(
voluptuous.error.Invalid, validator, ('a', '1'))
def test_iterablevaluesdict_invalid_type_str(self):
validator = validation_utils.IterableValuesDict(str, int)
self.assertRaises(
voluptuous.error.Invalid, validator, 'aaaa')