Skip to content

Commit

Permalink
refactor: complete testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven committed Aug 14, 2017
1 parent 2f8f14d commit b6f42f5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- 去掉 serialize 和 deserialize 接口

#### 1.0.0a0

### 0.0.1a3 - 2017-5-24

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ artists = [
album = AlbumModel(**album)
artists = [ArtistModel(**artist) for artist in artists]
song = SongModel(title=title, album=album, artists=artists)
song.artists[0].name
```
12 changes: 4 additions & 8 deletions april/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,10 @@ def validate_field(cls, name, value):
"""validate the type of field value"""
fields_dict = dict(cls._fields)
field_type = fields_dict[name]
if is_nested_type(field_type):
field_type.is_instance(value)
else:
if not isinstance(value, field_type):
raise ValidationError(
'the value of field:"{}" should be "{}", got {}'.format
(name, field_type, value))

if not any([is_nested_type(field_type) and field_type.is_instance(value),
isinstance(value, field_type)]):
raise ValidationError('the value of filed:"{}" should be "{}", got {}'
.format(name, field_type, type(value)))
@classmethod
def is_field(cls, name):
"""check if a attribute belongs to model fields"""
Expand Down
17 changes: 9 additions & 8 deletions april/tipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,26 @@


class BaseContainerType(object):
def __new__(cls, obj_list):
if not isinstance(obj_list, list):
raise TypeError('obj_list should be a list')
def __new__(cls, objects):
if not isinstance(objects, (list, set, tuple)):
raise TypeError('only list set or tuple can be container type, got {}'
.format(type(objects)))

for obj in obj_list:
for obj in objects:
if not isinstance(obj, cls.nested_type):
raise TypeError('object must be a %s' % cls.nested_type.__name__)

return obj_list
return objects

@classmethod
def is_instance(cls, data):
if not isinstance(data, (list, set, tuple)):
raise ValidationError('value should be a list set or tuple, got {}'
.format(type(data)))
return False

for each in data:
if not isinstance(each, cls.nested_type):
raise ValidationError("nested element should be '%s'" % cls.nested_type)
return False
return True


class listof:
Expand Down
32 changes: 17 additions & 15 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,23 @@ class UserModel(Model):

UserModel.validate({'name': 'lucy', 'strict': True})

# def test_inherit_usage(self):
#
# class CarModel(Model):
# trade_mark = str
#
# class BaseUserModel(Model):
# name = str
# car = CarModel
#
# class UserModel(BaseUserModel):
# age = int
#
# user = UserModel(name='lucy', age=11, car={'trade_mark': 'benzi'})
# self.assertEqual(user.name, 'lucy')
# self.assertEqual(user.car.trade_mark, 'benzi')
def test_inherit_usage(self):

class CarModel(Model):
trade_mark = str

class BaseUserModel(Model):
name = str
car = CarModel

class UserModel(BaseUserModel):
age = int

user = UserModel(name='lucy',
age=11,
car=CarModel(**{'trade_mark': 'benzi'}))
self.assertEqual(user.name, 'lucy')
self.assertEqual(user.car.trade_mark, 'benzi')

def test_inherit_optional_feilds(self):

Expand Down
6 changes: 5 additions & 1 deletion tests/test_tipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ def test_listof_4(self):

def test_isinstance(self):
listof_str = listof(str)
listof_str(['hello', 'world'])
self.assertFalse(listof_str.is_instance(1))

def test_validate(self):
listof_str = listof(str)
self.assertRaises(TypeError, listof_str, 1)

0 comments on commit b6f42f5

Please sign in to comment.