示例#1
0
 def test_to_basic_schema(self):
     schema = Schema()
     schema.x = IntField(default=1)
     schema.y = IntField(default=2)
     field = ListProxy(MockConfig(), schema)
     field.append(schema())
     assert field.to_basic() == [{'x': 1, 'y': 2}]
示例#2
0
 def test_validate_list_proxy(self):
     field = ListField(IntField())
     orig = ListProxy(MockConfig(), IntField(), [1, 2, 3])
     check = field._validate(MockConfig(),
                             ListProxy(MockConfig(), IntField(), orig))
     assert isinstance(check, ListProxy)
     assert check._items == orig
     assert check is not orig
     assert check._items is not orig._items
示例#3
0
    def test_validate_schema_invalid(self):
        schema = Schema()
        schema.x = IntField(default=1)
        schema.y = IntField(default=2)
        cfg = MockConfig()
        proxy = ListProxy(cfg, schema)

        with pytest.raises(ValueError):
            proxy._validate(100)
示例#4
0
    def test_validate_schema_dict(self):
        schema = Schema()
        schema.x = IntField(default=1)
        schema.y = IntField(default=2)
        cfg = MockConfig()
        proxy = ListProxy(cfg, ListField(schema))

        check = proxy._validate({'x': 10})
        assert isinstance(check, Config)
        assert check.x == 10
        assert check.y == 2
        assert check._parent is cfg
        assert check._schema is schema
示例#5
0
    def test_validate_schema_config(self):
        schema = Schema()
        schema.x = IntField(default=1)
        schema.y = IntField(default=2)
        cfg = MockConfig()
        proxy = ListProxy(cfg, ListField(schema))

        val = schema()
        val.x = 10
        check = proxy._validate(val)
        assert isinstance(check, Config)
        assert check is val
        assert check._parent is cfg
        assert check._schema is schema
示例#6
0
    def test_generate_argparse_parser(self, mock_argparse):
        parser = MagicMock()
        mock_argparse.return_value = parser
        schema = Schema()
        schema.long_name = StringField(help='asdf')
        schema.sub.short_name = IntField()
        schema.enable = BoolField()
        schema.runtime = FloatField()
        schema.ignore_me2 = Field()  # no storage_type

        retval = generate_argparse_parser(schema, x=1, y=2)
        assert retval is parser
        mock_argparse.assert_called_once_with(x=1, y=2)
        parser.add_argument.call_args_list == [
            call('--long-name',
                 action='store',
                 dest='long_name',
                 help='asdf',
                 metavar='LONG_NAME'),
            call('--sub-short-name',
                 action='store',
                 dest='sub.short_name',
                 help=None,
                 metavar='SUB_SHORT_NAME'),
            call('--enable', action='store_true', help=None, dest='enable'),
            call('--no-enable', action='store_false', help=None,
                 dest='enable'),
            call('--runtime', action='store', dest='runtime', help=None)
        ]
示例#7
0
 def test_get_item_position_not_exists(self):
     schema = Schema()
     schema.lst = ListField(IntField())
     config = schema()
     proxy = ListProxy(config, schema.lst)
     proxy.extend([1, 2, 3])
     assert proxy._get_item_position(10) == '3'
示例#8
0
    def test_extend_list(self):
        wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3'])
        with patch.object(wrap, '_validate') as mock_validate:
            mock_validate.side_effect = [4, 5]
            wrap.extend([4, '5'])
            mock_validate.mock_calls = [call(4), call('5')]

        assert wrap == [1, 2, 3, 4, 5]
示例#9
0
    def test_extend_proxy(self):
        cfg = MockConfig()
        field = IntField()
        wrap = ListProxy(cfg, ListField(field), [1, 2, '3'])
        with patch.object(wrap, '_validate') as mock_validate:
            wrap.extend(ListProxy(cfg, ListField(field), [4, 5]))
            mock_validate.assert_not_called()

        assert wrap == [1, 2, 3, 4, 5]
示例#10
0
 def test_validate_item_valid(self):
     schema = Schema()
     field = IntField()
     list_field = schema.x = ListField(field)
     config = schema()
     proxy = ListProxy(config, list_field)
     with patch.object(field, 'validate') as mock_validate:
         retval = mock_validate.return_value = object()
         assert proxy._validate(2) is retval
         mock_validate.assert_called_once_with(config, 2)
示例#11
0
 def test_validate_item_validation_error(self):
     schema = Schema()
     field = IntField()
     list_field = schema.x = ListField(field)
     config = schema()
     proxy = ListProxy(config, list_field)
     orig_exc = ValidationError(config, list_field, ValueError('asdf'))
     with patch.object(field, 'validate') as mock_validate:
         mock_validate.side_effect = orig_exc
         with pytest.raises(ValueError):
             proxy._validate(2)
示例#12
0
 def test_setitem_slice(self):
     wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3'])
     wrap[1:3] = ['4', '5']
     assert wrap == [1, 4, 5]
示例#13
0
 def test_setitem(self):
     wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3'])
     wrap[1] = '5'
     assert wrap == [1, 5, 3]
示例#14
0
 def test_iadd(self):
     wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3'])
     wrap += [4, 5]
     assert wrap == [1, 2, 3, 4, 5]
示例#15
0
 def test_add_wrapper(self):
     wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3'])
     wrap2 = ListProxy(MockConfig(), ListField(IntField()), [4, '5'])
     wrap3 = wrap + wrap2
     assert wrap3 == [1, 2, 3, 4, 5]
示例#16
0
 def test_to_basic_schema(self):
     schema = Schema()
     schema.x = IntField(default=1)
     schema.y = IntField(default=2)
     field = ListField(schema)
     assert field.to_basic(MockConfig(), [schema()]) == [{'x': 1, 'y': 2}]
示例#17
0
 def test_valid_int(self):
     field = IntField()
     assert field.validate(MockConfig(), '100') == 100
示例#18
0
 def test_to_basic_none(self):
     field = ListField(IntField(), default=None, key='asdf')
     assert field.to_basic(MockConfig(), None) is None
示例#19
0
 def test_default_list_wrap(self):
     cfg = MockConfig()
     field = ListField(IntField(), default=lambda: [1, 2, 3], key='asdf')
     field.__setdefault__(cfg)
     cfg._set_default_value.assert_called_once_with(
         'asdf', ListProxy(cfg, field, [1, 2, 3]))
示例#20
0
 def test_copy(self):
     wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3'])
     wrap2 = wrap.copy()
     assert wrap == wrap2
     assert wrap.list_field is wrap2.list_field
     assert wrap.cfg is wrap2.cfg
示例#21
0
 def test_insert(self):
     wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3'])
     wrap.insert(1, '6')
     assert wrap == [1, 6, 2, 3]
示例#22
0
 def test_min_valid(self):
     field = IntField(min=5)
     assert field.validate(MockConfig(), '5') == 5
示例#23
0
 def test_to_basic_empty(self):
     field = ListField(IntField(), default=None, key='asdf')
     assert field.to_basic(MockConfig(), []) == []
示例#24
0
 def test_max_valid(self):
     field = IntField(max=10)
     assert field.validate(MockConfig(), 10) == 10
示例#25
0
 def test_invalid_int(self):
     field = IntField()
     with pytest.raises(ValueError):
         field.validate(MockConfig(), 'asdf')
示例#26
0
 def test_eq_not_list(self):
     wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3'])
     assert wrap != 'hello'
示例#27
0
 def test_min_invalid(self):
     field = IntField(min=5)
     with pytest.raises(ValueError):
         field.validate(MockConfig(), 4)
示例#28
0
 def test_append(self):
     wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3'])
     wrap.append('4')
     assert wrap == [1, 2, 3, 4]
示例#29
0
 def test_max_invalid(self):
     field = IntField(max=10)
     with pytest.raises(ValueError):
         field.validate(MockConfig(), '11')
示例#30
0
 def test_add_list(self):
     wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3'])
     wrap2 = wrap + [4, 5]
     assert wrap2 == [1, 2, 3, 4, 5]