def test_with_max_and_min(self): t = types.Integer(min=123, max=456) self.assertRaises(ValueError, t, 122) t(123) t(300) t(456) self.assertRaises(ValueError, t, 457)
def test_equal_with_equal_custom_item_types(self): it1 = types.Integer() it2 = types.Integer() self.assertTrue(types.Dict(it1) == types.Dict(it2))
def test_repr(self): t = types.Dict(types.Integer()) self.assertEqual('Dict of Integer', repr(t))
def test_custom_value_type(self): self.type_instance = types.Dict(types.Integer()) self.assertConvertedValue('foo:123, bar: 456', { 'foo': 123, 'bar': 456 })
def test_repr(self): t = types.List(types.Integer()) self.assertEqual('List of Integer', repr(t))
def test_bounds_required(self): self.type_instance = types.List(types.Integer(), bounds=True) self.assertInvalid('1,2,3') self.assertInvalid('[1,2,3') self.assertInvalid('1,2,3]')
class IntegerTypeTests(TypeTestHelper, unittest.TestCase): type = types.Integer() def test_empty_string(self): self.assertConvertedValue('', None) def test_whitespace_string(self): self.assertConvertedValue(" \t\t\t\t", None) def test_positive_values_are_valid(self): self.assertConvertedValue('123', 123) def test_zero_is_valid(self): self.assertConvertedValue('0', 0) def test_negative_values_are_valid(self): self.assertConvertedValue('-123', -123) def test_leading_whitespace_is_ignored(self): self.assertConvertedValue(' 5', 5) def test_trailing_whitespace_is_ignored(self): self.assertConvertedValue('7 ', 7) def test_non_digits_are_invalid(self): self.assertInvalid('12a45') def test_repr(self): t = types.Integer() self.assertEqual('Integer', repr(t)) def test_repr_with_min(self): t = types.Integer(min=123) self.assertEqual('Integer(min=123)', repr(t)) def test_repr_with_max(self): t = types.Integer(max=456) self.assertEqual('Integer(max=456)', repr(t)) def test_repr_with_min_and_max(self): t = types.Integer(min=123, max=456) self.assertEqual('Integer(min=123, max=456)', repr(t)) def test_equal(self): self.assertTrue(types.Integer() == types.Integer()) def test_equal_with_same_min_and_no_max(self): self.assertTrue(types.Integer(min=123) == types.Integer(min=123)) def test_equal_with_same_max_and_no_min(self): self.assertTrue(types.Integer(max=123) == types.Integer(max=123)) def test_equal_with_same_min_and_max(self): t1 = types.Integer(min=1, max=123) t2 = types.Integer(min=1, max=123) self.assertTrue(t1 == t2) def test_not_equal(self): self.assertFalse(types.Integer(min=123) == types.Integer(min=456)) def test_not_equal_to_other_class(self): self.assertFalse(types.Integer() == types.String())
#! /usr/bin/env python # coding:utf-8 import sys from oslo.config import cfg from oslo.config import types PortType = types.Integer(1, 65535) disk_list = ['hda 100G', 'hdb 150G', 'hdc 200G'] disk_opts= [ cfg.MultiStrOpt('volume', default=disk_list, help='disk volumes in GB'), cfg.StrOpt('type', default='ssd', help='disk type!'), ] cli_opts = [ cfg.StrOpt('host', default='119.119.119.119', help='IP address to listen on.'), cfg.Opt('port', type=PortType, default=9292, help='Port number to listen on.') ] # oslo.config 默认维护了一个 ConfigOpts 类型的全局变量 CONF。 # 注册参数,以及后续获取参数,都是可以通过 CONF。 CONF = cfg.CONF # t_opts不是命令行参数,所以假如要覆盖默认值,只能通过配置文件改变! # 注册的选项必须是可迭代的!否则会发生错误 #CONF.register_opts(disk_opts) #CONF.register_cli_opts(cli_opts)
def test_not_equal(self): self.assertFalse(types.Integer(min=123) == types.Integer(min=456))
def test_equal_with_same_min_and_max(self): t1 = types.Integer(min=1, max=123) t2 = types.Integer(min=1, max=123) self.assertTrue(t1 == t2)
def test_equal_with_same_max_and_no_min(self): self.assertTrue(types.Integer(max=123) == types.Integer(max=123))
def test_equal(self): self.assertTrue(types.Integer() == types.Integer())
def test_repr_with_min_and_max(self): t = types.Integer(min=123, max=456) self.assertEqual('Integer(min=123, max=456)', repr(t))
def test_repr_with_min(self): t = types.Integer(min=123) self.assertEqual('Integer(min=123)', repr(t))
def test_repr(self): t = types.Integer() self.assertEqual('Integer', repr(t))
def test_not_equal_with_non_equal_custom_item_types(self): it1 = types.Integer() it2 = types.String() self.assertFalse(it1 == it2) self.assertFalse(types.Dict(it1) == types.Dict(it2))
def test_not_equal_to_other_class(self): self.assertFalse(types.String() == types.Integer())
def test_list_of_custom_type(self): self.type_instance = types.List(types.Integer()) self.assertConvertedValue('1,2,3,5', [1, 2, 3, 5])
cfg.IntOpt('max_retry', default=3, help='The number of retry when there is a ' 'connection error.'), cfg.BoolOpt('auto_commit', default=False, help='If automatically commmit when consume ' 'messages.'), cfg.BoolOpt('async', default=True, help='The type of posting.'), cfg.BoolOpt('compact', default=True, help=('Specify if the message received should be parsed.' 'If True, message will not be parsed, otherwise ' 'messages will be parsed.')), cfg.MultiOpt('partitions', item_type=types.Integer(), default=[0], help='The sleep time when no messages on kafka ' 'queue.'), cfg.BoolOpt('drop_data', default=False, help=('Specify if received data should be simply dropped. ' 'This parameter is only for testing purposes.')), ] kafka_group = cfg.OptGroup(name='kafka', title='title') cfg.CONF.register_group(kafka_group) cfg.CONF.register_opts(kafka_opts, kafka_group) influxdb_opts = [ cfg.StrOpt('database_name'),
def test_bounds_parsing(self): self.type_instance = types.List(types.Integer(), bounds=True) self.assertConvertedValue('[1,2,3]', [1, 2, 3])