Skip to content

Instantly share code, notes, and snippets.

@kennethlove
Created April 2, 2014 20:58
Show Gist options
  • Save kennethlove/9943052 to your computer and use it in GitHub Desktop.
Save kennethlove/9943052 to your computer and use it in GitHub Desktop.
Traceback (most recent call last):
File "/Users/martin/Projects/md5checker/playground/md5checker/tests.py", line 15, in test_new_md5hash_derived_field
self.assertEqual(m.md5, md5)
AssertionError: '' != '9a618248b64db62d15b300a07b00580b'
+ 9a618248b64db62d15b300a07b00580b
import hashlib
from django.db import models
class Hash(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=True)
class Meta(object):
abstract = True
class MD5HashManager(models.Manager):
def create(self, clear):
obj = self.create(clear=clear)
obj.md5 = hashlib.md5(clear).hexdigest()
return obj
class MD5Hash(Hash):
clear = models.CharField(max_length=128)
md5 = models.CharField(max_length=32)
objects = MD5HashManager()
def __unicode__(self):
return '<clear:{}> <md5:{}>'.format(
self.clear,
self.md5,
)
def __str__(self):
return self.__unicode__()
import hashlib
from django.test import TestCase
from md5checker.models import MD5Hash
class TestMD5Hash(TestCase):
def test_new_md5hash_derived_field(self):
clear = b'supersecret'
md5 = hashlib.md5(clear).hexdigest()
m = MD5Hash(clear=clear)
self.assertEqual(m.clear, clear)
self.assertEqual(m.md5, md5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment