Ejemplo n.º 1
0
def test_bool_type():
    flag = Item(type=Types.bool, default='yes')
    assert flag.default is True
    assert flag.value is True

    flag.value = 'no'
    assert flag.value is False
    assert flag.default is True

    flag.value = True
    assert flag.value is True

    flag.value = '1'
    assert flag.value is True

    flag.value = '0'
    assert flag.value is False

    assert Types.guess(True) == Types.bool
    assert Types.guess(False) == Types.bool
    assert Types.guess(None) == Types.not_set

    assert Types.guess('yes') == Types.str
    assert Types.guess('no') == Types.str
    assert Types.guess('True') == Types.str
    assert Types.guess('False') == Types.str
Ejemplo n.º 2
0
def test_bool_config_preserves_raw_str_value_used_to_set_it():
    c = Item('a', type=bool, default=False)
    assert c.value is False

    assert not c.value
    assert c.str_value == 'False'
    assert c.value is False

    c.value = 'False'
    assert not c.value
    assert c.str_value == 'False'
    assert c.value is False

    c.value = 'no'
    assert not c.value
    assert c.str_value == 'no'
    assert c.value is False

    c.value = '0'
    assert not c.value
    assert c.str_value == '0'
    assert c.value is False

    c.value = '1'
    assert c.value
    assert c.str_value == '1'
    assert c.value is True

    c.reset()
    assert not c.value
    assert c.value is False

    c.value = 'yes'
    assert c.str_value == 'yes'
    assert c.value is True
Ejemplo n.º 3
0
def test_raw_str_value_is_reset_on_non_str_value_set():
    c = Item('a', type=int, default=25)
    c.value = '23'
    assert c.str_value == '23'

    c.value = 25
    assert c.str_value == '25'

    c.value = 24
    assert c.str_value == '24'

    c.value = '22'
    assert c.str_value == '22'
Ejemplo n.º 4
0
def test_items_allows_special_attributes_to_be_prefixed_with_at_symbol_too():
    s = Item(**{'@type': bool, '@value': 'no'})
    assert s.type is Types.bool
    assert s.value is False

    s.value = 'yes'
    assert s.value is True

    t = Item(**{'@default': True})
    assert t.type is Types.bool
    assert t.value is True

    t.value = 'no'
    assert t.value is False
Ejemplo n.º 5
0
def test_int_type():
    number = Item(type=Types.int, default='55')
    assert number.default == 55
    assert number.value == 55
    assert number.str_value == '55'

    number.value = -55
    assert number.value == -55

    number.value = '-555'
    assert number.value == -555

    assert Types.guess(5) == Types.int
    assert Types.guess(-5) == Types.int
    assert Types.guess(0) == Types.int
Ejemplo n.º 6
0
def test_can_set_str_value_to_none():
    c = Item('a', default='haha')
    assert c.value == 'haha'
    assert c.type is Types.str

    c.value = None
    assert c.value is None
Ejemplo n.º 7
0
def test_item_value_can_be_unicode_str():
    c = Item(default=u'Jānis Bērziņš')
    assert c.str_value == u'Jānis Bērziņš'

    c.value = u'Pēteris Liepiņš'
    assert c.str_value == u'Pēteris Liepiņš'
    assert c.default == u'Jānis Bērziņš'
Ejemplo n.º 8
0
def test_float_type():
    rate = Item(type=Types.float, default='0.23')
    assert rate.default == 0.23
    assert rate.value == 0.23

    rate.value = '0.01'
    assert rate.value == 0.01

    rate.value = '-0.23'
    assert rate.value == -0.23

    assert Types.guess(0.23) == Types.float
    assert Types.guess(-0.23) == Types.float

    assert Types.guess('-0.23') is not Types.float
    assert Types.guess('0.23') is not Types.float
Ejemplo n.º 9
0
def test_bool_of_value():
    c = Item('a')

    # not_set evaluates to False
    assert not c.value

    c.value = 'b'
    assert c.value

    c.value = ''
    assert not c.value

    d = Item('a', default='b')
    assert d.value

    d.value = ''
    assert not d.value
Ejemplo n.º 10
0
def test_value_gets_stringified():
    c = Item('a', value='23')
    assert c.value == '23'
    assert c.value != 23

    c.value = 24
    assert c.value == '24'
    assert c.value != 24
Ejemplo n.º 11
0
def test_raw_str_value_is_reset_on_reset():
    c = Item('a', type=int, default=25)
    assert c.str_value == '25'

    c.value = '23'
    assert c.str_value == '23'

    c.reset()
    assert c.str_value == '25'
Ejemplo n.º 12
0
def test_item_with_no_value_and_no_default_returns_not_set_as_value():
    c = Item('a')
    assert c.value is not_set

    c.value = 'hey'
    assert c.value == 'hey'

    c.reset()
    assert c.value is not_set
Ejemplo n.º 13
0
def test_item_value_is_not_deep_copied_on_value_request():
    precious_things = ['a', 'b']
    c = Item(default=precious_things)

    c.value = ['c', 'd']
    c.value.append('e')

    assert c.value == ['c', 'd', 'e']
    assert c.default == ['a', 'b']
Ejemplo n.º 14
0
def test_int_value():
    c = Item('a', type=int, default=25)
    assert c.value == 25

    c.value = '23'
    assert c.value == 23
    assert c.value != '23'

    c.reset()
    assert c.value == 25
Ejemplo n.º 15
0
def test_has_value_returns_true_if_value_or_default_is_set():
    c = Item()
    assert not c.has_value
    c.value = '5'
    assert c.has_value
    c.reset()
    assert not c.has_value

    d = Item(default=5)
    assert d.has_value
    d.value = 6
    assert d.has_value
    d.reset()
    assert d.has_value

    e = Item(value=8)
    assert e.has_value
    e.value = 9
    assert e.has_value
    e.reset()
    assert not e.has_value
Ejemplo n.º 16
0
def test_is_default():
    c = Item('a')
    d = Item('a', default=None)
    e = Item('a', value=None)

    assert c.is_default
    assert d.is_default
    assert not e.is_default

    c.value = 'ccc'
    d.value = 'ddd'
    e.value = 'eee'

    assert not c.is_default
    assert not d.is_default
    assert not e.is_default

    c.value = None
    d.value = None
    e.value = None

    assert not c.is_default
    assert d.is_default  # this is the case when is_default is NOT the opposite of has_value
    assert not e.is_default

    c.reset()
    d.reset()
    e.reset()

    assert c.is_default
    assert d.is_default
    assert e.is_default
Ejemplo n.º 17
0
def test_item_sets_unrecognised_kwargs_as_attributes():
    c = Item(help='This is help',
             comment='This is comment',
             default=5,
             something_random=True)
    c.value = 5

    assert c.is_default
    assert c.help == 'This is help'
    assert c.comment == 'This is comment'
    assert c.something_random

    with pytest.raises(AttributeError):
        assert c.something_too_random
Ejemplo n.º 18
0
def test_list_type():
    tags = Item(type=Types.list, default=['untitled', 'new'])

    assert tags.default == ['untitled', 'new']
    assert tags.value == ['untitled', 'new']

    # This should have no effect
    tags.value.append('!!!!')

    assert tags.default == ['untitled', 'new']
    assert tags.value == ['untitled', 'new']

    tags.value = ['real', 'tags', 'now']
    tags.value.append('!!!!')

    assert tags.default == ['untitled', 'new']
    assert tags.value == ['real', 'tags', 'now', '!!!!']

    assert Types.guess([]) == Types.list
    assert Types.guess(()) == Types.list
    assert Types.guess([1, 2, 3]) == Types.list
    assert Types.guess(['1', '2', '3']) == Types.list
Ejemplo n.º 19
0
def test_setting_value_to_not_set_resets_it():
    c = Item('a', default='default', value='custom')
    assert c.value == 'custom'

    c.value = not_set
    assert c.value == 'default'
Ejemplo n.º 20
0
def test_can_set_int_value_to_none():
    c = Item('a', type=int, default=0, value=23)
    assert c.value == 23

    c.value = None
    assert c.value is None
Ejemplo n.º 21
0
def test_bool_str_is_a_str():
    c = Item('a', type=bool)
    assert isinstance(c.str_value, six.string_types)

    c.value = True
    assert isinstance(c.str_value, six.string_types)
Ejemplo n.º 22
0
def test_dict_type():
    db = Item(type=Types.dict,
              default={
                  'user': '******',
                  'password': '******',
                  'host': 'localhost',
                  'db': 'test',
              })

    assert db.default == {
        'user': '******',
        'password': '******',
        'host': 'localhost',
        'db': 'test'
    }
    assert db.value == {
        'user': '******',
        'password': '******',
        'host': 'localhost',
        'db': 'test'
    }

    # This has no effect because it is working on a copy of default value
    db.value['password'] = '******'

    assert db.default == {
        'user': '******',
        'password': '******',
        'host': 'localhost',
        'db': 'test'
    }
    assert db.value == {
        'user': '******',
        'password': '******',
        'host': 'localhost',
        'db': 'test'
    }

    # But once we have a real value, then modifications are for real
    db.value = {
        'user': '******',
        'password': '******',
        'host': 'localhost',
        'db': 'production'
    }
    db.value['password'] = '******'

    assert db.default == {
        'user': '******',
        'password': '******',
        'host': 'localhost',
        'db': 'test'
    }
    assert db.value == {
        'user': '******',
        'password': '******',
        'host': 'localhost',
        'db': 'production'
    }

    assert Types.guess({}) == Types.dict
    assert Types.guess(collections.OrderedDict()) == Types.dict
Ejemplo n.º 23
0
def test_repr_makes_clear_name_and_value():
    c = Item('a', default='hello')
    assert repr(c) == '<Item a \'hello\'>'

    c.value = 'bye!'
    assert repr(c) == '<Item a \'bye!\'>'