Skip to content

Commit 1b53f9f

Browse files
Add unit test
Tests ability to disable error message interpolation. Added parameter to turn on/off error message interpolation.
1 parent b21157e commit 1b53f9f

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

flask_validator/validator.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class FlaskValidator(object):
1919
throw_exception = False
2020
message = None
2121

22-
def __init__(self, field, allow_null, throw_exception, message, parent):
22+
def __init__(self, field, allow_null, throw_exception, message,
23+
interpolate_message, parent):
2324
""" Initialize a Validator object.
2425
2526
:type throw_exception: Throw a ValidateError exception
@@ -30,6 +31,7 @@ def __init__(self, field, allow_null, throw_exception, message, parent):
3031
self.allow_null = allow_null
3132
self.throw_exception = throw_exception
3233
self.message = message
34+
self.interpolate_message = interpolate_message
3335
self.__create_event()
3436

3537
def __validate(self, target, value, oldvalue, initiator):
@@ -93,14 +95,16 @@ def start(self):
9395
class Validator(FlaskValidator):
9496
""" Main validotr class """
9597

96-
def __init__(self, field, allow_null=True, throw_exception=False, message=None):
98+
def __init__(self, field, allow_null=True, throw_exception=False,
99+
message=None, interpolate_message=True):
97100
"""
98101
Validator Interface initialization
99102
100103
:param field: Flask Column to validate
101104
"""
102105

103-
FlaskValidator.__init__(self, field, allow_null, throw_exception, message, self)
106+
FlaskValidator.__init__(self, field, allow_null, throw_exception,
107+
message, interpolate_message, self)
104108

105109
@abc.abstractmethod
106110
def check_value(self, value):

test/test_message.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
""" FlaskValidator tests """
2+
3+
import unittest
4+
5+
from flask import Flask
6+
from flask_sqlalchemy import SQLAlchemy
7+
8+
from flask_validator import Validator
9+
10+
11+
class MessageTest(unittest.TestCase):
12+
def setUp(self):
13+
app = Flask(__name__)
14+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
15+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
16+
db = SQLAlchemy(app)
17+
18+
class DummyModel(db.Model):
19+
""" SQLAlchemy Dummy Object """
20+
21+
id = db.Column(db.Integer, primary_key=True)
22+
integer = db.Column(db.Integer)
23+
24+
db.create_all()
25+
26+
self.DummyModel = DummyModel
27+
self.define_validators()
28+
29+
self.dummy = DummyModel()
30+
31+
self.app = app
32+
self.db = db
33+
34+
def simple_validate(self, field, value):
35+
"""
36+
Simple Validation
37+
38+
"""
39+
setattr(self.dummy, field, value)
40+
41+
def define_validators(self):
42+
"""
43+
Define Validators
44+
45+
"""
46+
class FailValidator(Validator):
47+
def check_value(self, value):
48+
return False
49+
50+
FailValidator(self.DummyModel.integer, throw_exception=True,
51+
message="{do} {not} {interpolate} {this}",
52+
interpolate_message=False)
53+
54+
def test_disable_message_interpolation(self):
55+
"""
56+
Testing FlaskValidator.interpolate_message
57+
58+
"""
59+
with self.assertRaises(Exception) as context:
60+
self.simple_validate('integer', 1)
61+
62+
self.assertEqual("{do} {not} {interpolate} {this}",
63+
str(context.exception))
64+
65+
66+
def suite():
67+
""" Test Suite """
68+
suite = unittest.TestSuite()
69+
suite.addTest(unittest.makeSuite(MessageTest))
70+
return suite
71+
72+
73+
if __name__ == '__main__':
74+
unittest.main(defaultTest='suite')

0 commit comments

Comments
 (0)