Last active
December 17, 2015 13:19
-
-
Save lkraider/5616500 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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