Browse Source

[types] make load_into a classmethod, add testing for types

pull/38/head
andrei 8 years ago
parent
commit
359795e410
  1. 7
      disco/types/base.py
  2. 33
      tests/test_types.py

7
disco/types/base.py

@ -292,8 +292,9 @@ class Model(six.with_metaclass(ModelMeta, Chainable)):
def load(self, obj, consume=False, skip=None): def load(self, obj, consume=False, skip=None):
return self.load_into(self, obj, consume, skip) return self.load_into(self, obj, consume, skip)
def load_into(self, inst, obj, consume=False, skip=None): @classmethod
for name, field in six.iteritems(self._fields): def load_into(cls, inst, obj, consume=False, skip=None):
for name, field in six.iteritems(cls._fields):
should_skip = skip and name in skip should_skip = skip and name in skip
if consume and not should_skip: if consume and not should_skip:
@ -312,7 +313,7 @@ class Model(six.with_metaclass(ModelMeta, Chainable)):
setattr(inst, field.dst_name, raw) setattr(inst, field.dst_name, raw)
continue continue
value = field.try_convert(raw, self.client) value = field.try_convert(raw, inst.client)
setattr(inst, field.dst_name, value) setattr(inst, field.dst_name, value)
def update(self, other, ignored=None): def update(self, other, ignored=None):

33
tests/test_types.py

@ -0,0 +1,33 @@
from unittest import TestCase
from disco.types.base import Model, Field
class _M(Model):
a = Field(int)
b = Field(float)
c = Field(str)
class TestModel(TestCase):
def test_model_simple_loading(self):
inst = _M(dict(a=1, b=1.1, c='test'))
self.assertEquals(inst.a, 1)
self.assertEquals(inst.b, 1.1)
self.assertEquals(inst.c, 'test')
def test_model_load_into(self):
inst = _M()
_M.load_into(inst, dict(a=1, b=1.1, c='test'))
self.assertEquals(inst.a, 1)
self.assertEquals(inst.b, 1.1)
self.assertEquals(inst.c, 'test')
def test_model_loading_consume(self):
obj = dict(a=5, b=33.33, c='wtf')
inst = _M()
inst.load(obj, consume=True)
self.assertEquals(obj, {})
self.assertEquals(inst.a, 5)
self.assertEquals(inst.b, 33.33)
self.assertEquals(inst.c, 'wtf')
Loading…
Cancel
Save