当前位置: 首页>>代码示例>>Python>>正文


Python Validator.validate_number方法代码示例

本文整理汇总了Python中validator.Validator.validate_number方法的典型用法代码示例。如果您正苦于以下问题:Python Validator.validate_number方法的具体用法?Python Validator.validate_number怎么用?Python Validator.validate_number使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在validator.Validator的用法示例。


在下文中一共展示了Validator.validate_number方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Zoo

# 需要导入模块: from validator import Validator [as 别名]
# 或者: from validator.Validator import validate_number [as 别名]
class Zoo(object):
    def __init__(self, name, capacity, budget):
        self.__validator = Validator()

        self.name = name
        self.capacity = capacity
        self.budget = budget

    @property
    def name(self):
        return self.__name

    @name.setter
    def name(self, name):
        self.__validator.validate_name(2, 'zoos', name)
        self.__name = name

    @property
    def capacity(self):
        return self.__capacity

    @capacity.setter
    def capacity(self, capacity):
        self.__validator.validate_number(1, capacity, 'capacity')
        self.__capacity = capacity

    @property
    def budget(self):
        return self.__budget

    @budget.setter
    def budget(self, budget):
        self.__validator.validate_number(0, budget, 'budget')
        self.__budget = budget
开发者ID:EmanuelaMollova,项目名称:Zoo,代码行数:36,代码来源:zoo.py

示例2: ValidatorTest

# 需要导入模块: from validator import Validator [as 别名]
# 或者: from validator.Validator import validate_number [as 别名]
class ValidatorTest(unittest.TestCase):

    def setUp(self):
        self.v = Validator('../../database/zoos_test_database.db')

    def test_validate_name_returns_none_on_valid_name(self):
        self.assertEqual(None, self.v.validate_name(2, 'zoos', 'aa'))
        self.assertEqual(None, self.v.validate_name(0, 'zoos', 'aa'))
        self.assertEqual(None, self.v.validate_name(1, 'zoos', 'a'))

    def test_validate_name_raises_exception_on_invalid_name(self):
        self.assertRaises(Exception, self.v.validate_name, 2, 'zoos', 'a')
        self.assertRaises(Exception, self.v.validate_name, 1, 'zoos', '')

    def test_validate_name_raises_exception_on_used_name(self):
        self.assertRaises(
            Exception, self.v.validate_name, 2, 'zoos', 'Zoo Sofia'
        )

        self.assertRaises(
            Exception, self.v.validate_name, 2, 'animals', 'Nikky'
        )

        self.assertRaises(
            Exception, self.v.validate_name, 2, 'species', 'lion'
        )

    def test_validate_number_returns_none_on_valid_number(self):
        self.assertEqual(None, self.v.validate_number(1, 1, ''))
        self.assertEqual(None, self.v.validate_number(0, 0, ''))
        self.assertEqual(None, self.v.validate_number(0, 10, ''))
        self.assertEqual(None, self.v.validate_number(0, 3.56, ''))
        self.assertEqual(None, self.v.validate_number(0, 0.01, ''))

    def test_validate_number_raises_exception_on_invalid_number(self):
        self.assertRaises(Exception, self.v.validate_number, 2, 1, '')
        self.assertRaises(Exception, self.v.validate_number, 0, -1, '')
        self.assertRaises(Exception, self.v.validate_number, 1, 0, '')

    def test_validate_number_works_with_strings(self):
        self.assertEqual(None, self.v.validate_number(1, '1', ''))
        self.assertEqual(None, self.v.validate_number(0, '0', ''))
        self.assertEqual(None, self.v.validate_number(0, '10', ''))
        self.assertEqual(None, self.v.validate_number(0, '0.001', ''))

        self.assertRaises(Exception, self.v.validate_number, 2, '1', '')
        self.assertRaises(Exception, self.v.validate_number, 0, '-1', '')
        self.assertRaises(Exception, self.v.validate_number, 1, '0', '')
        self.assertRaises(Exception, self.v.validate_number, 1, '0.76', '')

    def test_validate_returns_none_on_valid_parameter(self):
        lambda_function = lambda x: x < 10
        self.assertEqual(None, self.v.validate(lambda_function, 2, ''))

        lambda_function = lambda x: x ** 3 == 8
        self.assertEqual(None, self.v.validate(lambda_function, 2, ''))

    def test_validate_raises_exception_on_invalid_parameter(self):
        lf = lambda x: x < 10
        self.assertRaises(Exception, self.v.validate, lf, 20, '')

        lf = lambda x: x ** 3 == 8
        self.assertRaises(Exception, self.v.validate, lf, 20, '')
开发者ID:EmanuelaMollova,项目名称:Zoo,代码行数:65,代码来源:test_validator.py


注:本文中的validator.Validator.validate_number方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。