From 359795e410b8a496243ccac7e2a59a4e57c3a9b0 Mon Sep 17 00:00:00 2001 From: andrei Date: Thu, 22 Jun 2017 15:36:09 -0700 Subject: [PATCH] [types] make load_into a classmethod, add testing for types --- disco/types/base.py | 7 ++++--- tests/test_types.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 tests/test_types.py diff --git a/disco/types/base.py b/disco/types/base.py index 54cbcfd..883ba38 100644 --- a/disco/types/base.py +++ b/disco/types/base.py @@ -292,8 +292,9 @@ class Model(six.with_metaclass(ModelMeta, Chainable)): 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: @@ -312,7 +313,7 @@ class Model(six.with_metaclass(ModelMeta, Chainable)): 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): diff --git a/tests/test_types.py b/tests/test_types.py new file mode 100644 index 0000000..c5df25c --- /dev/null +++ b/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')