Added app.utils.str2id() to convert IDs on URLs to ObjectId.

Raises a BadRequest exception when the ID is malformed.
This commit is contained in:
2016-07-07 14:56:30 +02:00
parent be1deb7eb6
commit 251f5ac86a
2 changed files with 51 additions and 1 deletions

31
tests/test_utils.py Normal file
View File

@@ -0,0 +1,31 @@
# -*- encoding: utf-8 -*-
from bson import ObjectId
from werkzeug.exceptions import BadRequest
from common_test_class import AbstractPillarTest
class Str2idTest(AbstractPillarTest):
def test_happy(self):
from application.utils import str2id
def happy(str_id):
self.assertEqual(ObjectId(str_id), str2id(str_id))
happy(24 * 'a')
happy(12 * 'a')
happy(u'577e23ad98377323f74c368c')
def test_unhappy(self):
from application.utils import str2id
def unhappy(str_id):
self.assertRaises(BadRequest, str2id, str_id)
unhappy(13 * 'a')
unhappy(u'577e23ad 8377323f74c368c')
unhappy(u'김치') # Kimchi
unhappy('')
unhappy(u'')
unhappy(None)