def test_build_dict(self): d1 = build_dict(self.s(), ('nature.luck', 1)) print d1 validate_dict(d1, self.s()) d2 = build_dict(self.s(), ('nature.luck', 2)) print d2 assert d1['nature']['luck'] != d2['nature']['luck']
def test_build_instance(self): # meet required_fields and strict_fields # required_fields = [ # 'id', 'name', # 'attributes.strength', 'attributes.armor', # 'skills', 'skills.name', 'skills.damage' # ] # strict_fields = ['id', 'slots', 'skills.damage', 'skills.level'] kwargs = dict( id=ObjectId(), name='reorx', attributes={ 'strength': 10, 'armor': 10 }, skills=[{'name': 'test', 'damage': 1.1}], slots=[] ) ins = self.UserDict.build_instance(**kwargs) d = build_dict(self.UserDict.struct, **kwargs) assert d['name'] == 'reorx' assert d['skills'][0]['name'] == 'test' del ins['id'] del d['id'] assert hash_dict(ins) == hash_dict(d) with assert_raises(TypeError): ins = self.UserDict.build_instance(name=1)
def test_build_instance(self): # meet required_fields and strict_fields # required_fields = [ # 'id', 'name', # 'attributes.strength', 'attributes.armor', # 'skills', 'skills.name', 'skills.damage' # ] # strict_fields = ['id', 'slots', 'skills.damage', 'skills.level'] kwargs = dict(id=ObjectId(), name='reorx', attributes={ 'strength': 10, 'armor': 10 }, skills=[{ 'name': 'test', 'damage': 1.1 }], slots=[]) ins = self.UserDict.build_instance(**kwargs) d = build_dict(self.UserDict.struct, **kwargs) assert d['name'] == 'reorx' assert d['skills'][0]['name'] == 'test' del ins['id'] del d['id'] assert hash_dict(ins) == hash_dict(d) with assert_raises(TypeError): ins = self.UserDict.build_instance(name=1)
def test_gen(self): assert isinstance(self.UserDict.gen.id(), ObjectId) d = self.UserDict.gen.attributes() print d assert len(d.keys()) == 3 and 'strength' in d d = self.UserDict.gen.skills.parents(name='foo') assert hash_dict(d) == hash_dict( build_dict(self.UserDict.struct['skills'][0]['parents'][0], name='foo'))