Skip to content

Instantly share code, notes, and snippets.

@lkraider
Last active December 17, 2015 13:19
Show Gist options
  • Save lkraider/5616500 to your computer and use it in GitHub Desktop.
Save lkraider/5616500 to your computer and use it in GitHub Desktop.
import unittest
import hashlib
from schematics.models import Model
from schematics.types import IntType, StringType, MD5Type
from schematics.exceptions import ValidationError
class TestTransformers(unittest.TestCase):
def test_transformers(self):
"""
Use transform functions to set data between fields.
"""
def gen_digest(value):
return hashlib.md5(value).hexdigest()
class Player(Model):
secret = MD5Type()
password = StringType(min_length=6, max_length=6)
transformers = {
password: (secret, gen_digest),
}
p1 = Player({'password': 'secret'})
p1.validate()
self.assertNotEqual(p1.password, 'secret')
self.assertTrue(p1.secret)
self.assertNotIn('secret', p1.serialize().values())
with self.assertRaises(ValidationError):
p2 = Player({'password': 'tiny'})
p2.validate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment