From 0a03f9a31ad7ab412e6ff5ab251aadb4d3296db7 Mon Sep 17 00:00:00 2001 From: Pau Aliagas Date: Tue, 4 Oct 2011 15:59:56 +0200 Subject: [PATCH] Add unit tests for UUIDField --- tests/fields.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/fields.py b/tests/fields.py index fd993167..6c183b49 100644 --- a/tests/fields.py +++ b/tests/fields.py @@ -1,9 +1,7 @@ import unittest import datetime from decimal import Decimal - -import pymongo -import gridfs +import uuid from mongoengine import * from mongoengine.connection import _get_db @@ -175,6 +173,26 @@ class FieldTest(unittest.TestCase): person.admin = 'Yes' self.assertRaises(ValidationError, person.validate) + def test_uuid_validation(self): + """Ensure that invalid values cannot be assigned to UUID fields. + """ + class Person(Document): + api_key = UUIDField() + + person = Person() + # any uuid type is valid + person.api_key = uuid.uuid4() + person.validate() + person.api_key = uuid.uuid1() + person.validate() + + # last g cannot belong to an hex number + person.api_key = '9d159858-549b-4975-9f98-dd2f987c113g' + self.assertRaises(ValidationError, person.validate) + # short strings don't validate + person.api_key = '9d159858-549b-4975-9f98-dd2f987c113' + self.assertRaises(ValidationError, person.validate) + def test_datetime_validation(self): """Ensure that invalid values cannot be assigned to datetime fields. """