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'
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}]
def test_validate_not_field(self): schema = Schema() schema.lst = ListField(int) config = schema() proxy = ListProxy(config, schema.lst) with pytest.raises(TypeError): proxy._validate(10)
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]
def test_validate_list_proxy(self): field = ListField(IntField()) orig = ListProxy(MockConfig(), field, [1, 2, 3]) check = field._validate(MockConfig(), ListProxy(MockConfig(), field, orig)) assert isinstance(check, ListProxy) assert check == orig assert check is not orig
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]
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)
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)
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)
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
def test_listfield_no_field(self): schema = Schema() list_field = schema.lst = ListField() config = schema() with pytest.raises(TypeError): ListProxy(config, list_field)
def test_eq_not_list(self): wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3']) assert wrap != 'hello'
def test_append(self): wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3']) wrap.append('4') assert wrap == [1, 2, 3, 4]
def test_index(self): wrap = ListProxy(MockConfig(), IntField(), [1, 2, '3']) assert wrap.index(3) == 2
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]))
def test_to_basic(self): field = ListField(IntField(), required=True) wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3']) assert field.to_basic(MockConfig(), wrap) == [1, 2, 3]
def test_eq_proxy(self): wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3']) wrap2 = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3']) assert wrap == wrap2
def test_eq_list(self): wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3']) assert wrap == [1, 2, 3]
def test_count(self): wrap = ListProxy(MockConfig(), IntField(), [1, 2, '3']) assert wrap.count(2) == 1
def test_iadd(self): wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3']) wrap += [4, 5] assert wrap == [1, 2, 3, 4, 5]
def test_add_list(self): wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3']) wrap2 = wrap + [4, 5] assert wrap2 == [1, 2, 3, 4, 5]
def test_pop_none(self): wrap = ListProxy(MockConfig(), IntField(), [1, 2, '3']) val = wrap.pop() assert val == 3 assert wrap._items == [1, 2]
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]
def test_clear(self): wrap = ListProxy(MockConfig(), IntField(), [1, 2, '3']) wrap.clear() assert wrap._items == []
def test_setitem(self): wrap = ListProxy(MockConfig(), ListField(IntField()), [1, 2, '3']) wrap[1] = '5' assert wrap == [1, 5, 3]
def test_copy(self): wrap = ListProxy(MockConfig(), IntField(), [1, 2, '3']) wrap2 = wrap.copy() assert wrap._items == wrap2._items assert wrap.field is wrap2.field assert wrap.cfg is wrap2.cfg