Skip to content
This repository has been archived by the owner on Aug 1, 2021. It is now read-only.

Commit

Permalink
[types] make load_into a classmethod, add testing for types
Browse files Browse the repository at this point in the history
  • Loading branch information
b1naryth1ef committed Jun 22, 2017
1 parent 25f1b2d commit 359795e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
7 changes: 4 additions & 3 deletions disco/types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,9 @@ def _fields(self):
def load(self, obj, consume=False, skip=None):
return self.load_into(self, obj, consume, skip)

def load_into(self, inst, obj, consume=False, skip=None):
for name, field in six.iteritems(self._fields):
@classmethod
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

if consume and not should_skip:
Expand All @@ -312,7 +313,7 @@ def load_into(self, inst, obj, consume=False, skip=None):
setattr(inst, field.dst_name, raw)
continue

value = field.try_convert(raw, self.client)
value = field.try_convert(raw, inst.client)
setattr(inst, field.dst_name, value)

def update(self, other, ignored=None):
Expand Down
33 changes: 33 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -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')

0 comments on commit 359795e

Please sign in to comment.