From 83e5f93958ede3ed9210ad270c12d9f7010d6397 Mon Sep 17 00:00:00 2001 From: andrei Date: Fri, 23 Jun 2017 16:28:30 -0700 Subject: [PATCH] [tests] add more typing tests --- tests/test_types.py | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/tests/test_types.py b/tests/test_types.py index 0f96560..a69774d 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -1,5 +1,7 @@ from unittest import TestCase -from disco.types.base import Model, Field + +from holster.enum import Enum +from disco.types.base import Model, Field, enum, snowflake, ConversionError class _A(Model): @@ -57,3 +59,43 @@ class TestModel(TestCase): self.assertEquals(inst.b.c, '1') self.assertEquals(obj, {'a': {'d': 'wow'}, 'b': {'z': 'wtf'}, 'g': 'lmao'}) + + def test_model_field_enum(self): + en = Enum( + A=1, + B=2, + C=3 + ) + + class _M(Model): + field = Field(enum(en)) + + self.assertEquals(_M(field=en.A).field, en.A) + self.assertEquals(_M(field=2).field, en.B) + self.assertEquals(_M(field='3').field, None) + self.assertEquals(_M(field='a').field, en.A) + self.assertEquals(_M(field='A').field, en.A) + + def test_model_field_snowflake(self): + class _M(Model): + field = Field(snowflake) + + self.assertEquals(_M(field=327936274851954688).field, 327936274851954688) + self.assertEquals(_M(field='327936274851954688').field, 327936274851954688) + + with self.assertRaises(ConversionError): + _M(field='asdf') + + def test_model_field_cast(self): + class Object(object): + def __init__(self, v): + self.v = v + + def __unicode__(self): + return self.v + + class _M(Model): + field = Field(Object, cast=unicode) + + inst = _M(field=u'wowza') + self.assertEquals(inst.to_dict(), {'field': u'wowza'})