|
From: <sub...@co...> - 2007-10-21 00:34:57
|
Author: ianb
Date: 2007-10-20 18:34:57 -0600 (Sat, 20 Oct 2007)
New Revision: 3104
Modified:
FormEncode/trunk/docs/Design.txt
FormEncode/trunk/formencode/schema.py
FormEncode/trunk/tests/test_validators.py
Log:
tests from James Gardner; fix an issue where validating {} with a schema will always just return {}
Modified: FormEncode/trunk/docs/Design.txt
===================================================================
--- FormEncode/trunk/docs/Design.txt 2007-10-21 00:25:36 UTC (rev 3103)
+++ FormEncode/trunk/docs/Design.txt 2007-10-21 00:34:57 UTC (rev 3104)
@@ -95,7 +95,7 @@
secure way. The specific way we bridge this depends on the nature of
the user interface. An XML-RPC interface can make some assumptions
that a GUI cannot make. An HTML interface can typically make even
-less assumptions, including the basic integrity of the input data. It
+fewer assumptions, including the basic integrity of the input data. It
isn't reasonable that the object should know about all means of
inputs, and the varying UI requirements of those inputs; user
interfaces are volatile, and more art than science, but domain objects
Modified: FormEncode/trunk/formencode/schema.py
===================================================================
--- FormEncode/trunk/formencode/schema.py 2007-10-21 00:25:36 UTC (rev 3103)
+++ FormEncode/trunk/formencode/schema.py 2007-10-21 00:34:57 UTC (rev 3104)
@@ -306,6 +306,10 @@
result.extend(self.fields.values())
return result
+ def is_empty(self, value):
+ ## Generally nothing is empty for us
+ return False
+
def empty_value(self, value):
return {}
Modified: FormEncode/trunk/tests/test_validators.py
===================================================================
--- FormEncode/trunk/tests/test_validators.py 2007-10-21 00:25:36 UTC (rev 3103)
+++ FormEncode/trunk/tests/test_validators.py 2007-10-21 00:34:57 UTC (rev 3104)
@@ -1,4 +1,10 @@
from formencode.validators import String, UnicodeString, Invalid, Int
+from formencode.validators import DateConverter
+from formencode.variabledecode import NestedVariables
+from formencode.schema import Schema
+from formencode.foreach import ForEach
+import datetime
+from formencode.api import NoDefault
def validate(validator, value):
try:
@@ -80,5 +86,44 @@
assert validate(iv, "1") == messages('tooLow', None, min=5)
assert validate(iv, "15") == messages('tooHigh', None, max=10)
+def test_month_style():
+ date = DateConverter(month_style='dd/mm/yyy')
+ d = datetime.date(2007,12,20)
+ assert date.to_python('20/12/2007') == d
+ assert date.from_python(d) == '20/12/2007'
+def test_date():
+ date = DateConverter(month_style='dd/mm/yyy')
+ try:
+ date.to_python('20/12/150')
+ except Invalid, e:
+ assert 'Please enter a four-digit year after 1899' in str(e)
+
+def test_foreach_if_missing():
+
+ class SubSchema(Schema):
+ age = Int()
+ name = String(not_empty=True)
+
+ class MySchema(Schema):
+ pre_validators = [NestedVariables()]
+ people = ForEach(SubSchema(), if_missing=NoDefault, messages={'missing':'Please add a person'})
+
+ start_values = {}
+
+ class State:
+ pass
+
+ validator = MySchema()
+ assert validator.fields['people'].not_empty == False
+
+ state = State()
+
+ try:
+ values = validator.to_python(start_values, state)
+ except Invalid, e:
+ assert e.unpack_errors() == {'people': u'Please add a person'}
+ else:
+ raise Exception("Shouldn't be valid data", values, start_values)
+
|