[Zope3-checkins] SVN: zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/ Port another test from zope.app.form.

Martijn Faassen faassen at startifact.com
Thu Dec 31 02:52:52 EST 2009


Log message for revision 107436:
  Port another test from zope.app.form.
  

Changed:
  U   zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/functionalsupport.py
  U   zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_functional_datetimewidget.py
  A   zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_functional_filewidget.py

-=-
Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/functionalsupport.py
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/functionalsupport.py	2009-12-31 07:41:20 UTC (rev 107435)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/functionalsupport.py	2009-12-31 07:52:51 UTC (rev 107436)
@@ -36,10 +36,6 @@
 
     result = []
 
-    #if self.errors:
-    #    for error in self.errors:
-    #        result.append("%s: %s" % (error.__class__.__name__, error))
-
     for w in self.widgets:
         result.append(w())
         error = w.error()

Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_functional_datetimewidget.py
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_functional_datetimewidget.py	2009-12-31 07:41:20 UTC (rev 107435)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_functional_datetimewidget.py	2009-12-31 07:52:51 UTC (rev 107436)
@@ -108,8 +108,6 @@
         request.form['form.actions.apply'] = u''
 
         Form(foo, request)()
-        
-        #    env={"HTTP_ACCEPT_LANGUAGE": "en"})
 
         self.assertEqual(foo.d1, datetime(2003, 2, 1, tzinfo=tzinfo(0)))
         self.assertEqual(foo.d2, datetime(2003, 2, 2, tzinfo=tzinfo(0)))

Copied: zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_functional_filewidget.py (from rev 107371, zope.app.form/branches/faassen-zaf/src/zope/app/form/browser/tests/test_functional_filewidget.py)
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_functional_filewidget.py	                        (rev 0)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_functional_filewidget.py	2009-12-31 07:52:51 UTC (rev 107436)
@@ -0,0 +1,156 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""File Widget Tests
+
+$Id$
+"""
+import unittest
+
+import re
+from datetime import datetime
+
+from StringIO import StringIO
+from zope.interface import Interface, implements
+from zope.schema import Datetime, Choice, Field
+from zope.schema.interfaces import IField
+from zope.formlib import form
+from zope.publisher.browser import TestRequest
+from zope.formlib.tests.support import patternExists
+from zope.formlib.widgets import FileWidget
+from zope.formlib.tests.functionalsupport import FunctionalWidgetTestCase
+import zope.schema.interfaces
+from zope.datetime import parseDatetimetz, tzinfo
+
+class IFileField(IField):
+    """Field for representing a file that can be edited by FileWidget."""
+
+class FileField(Field):
+    implements(IFileField)
+
+class IFileTest(Interface):
+    f1 = FileField(required=True)
+    f2 = FileField(required=False)
+
+class FileTest(object):
+    implements(IFileTest)
+
+    def __init__(self):
+        self.f1 = None
+        self.f2 = 'foo'
+
+class Form(form.EditForm):
+    form_fields = form.fields(IFileTest)
+    form_fields['f1'].custom_widget = FileWidget
+    form_fields['f2'].custom_widget = FileWidget
+    
+class SampleTextFile(StringIO):
+    def __init__(self, buf, filename=''):
+        StringIO.__init__(self, buf)
+        self.filename = filename
+
+class Test(FunctionalWidgetTestCase):
+    
+    sampleText = "The quick brown fox\njumped over the lazy dog."
+    sampleTextFile = SampleTextFile(sampleText)
+
+    emptyFileName = 'empty.txt'
+    emptyFile = SampleTextFile('', emptyFileName)
+
+    def test_display_editform(self):
+        foo = FileTest()
+        request = TestRequest()
+
+        # display edit view
+        html = Form(foo, request)()
+
+        # field should be displayed in a file input element
+        self.assert_(patternExists(
+            '<input .* name="form.f1".* type="file".*>', html))
+        self.assert_(patternExists(
+            '<input .* name="form.f2".* type="file".*>', html))
+
+    def test_submit_text(self):
+        foo = FileTest()
+        request = TestRequest()
+
+        self.assert_(foo.f1 is None)
+        self.assertEqual(foo.f2, 'foo')
+
+        # submit a sample text file
+        request.form['form.f1'] = self.sampleTextFile
+        request.form['form.f2'] = self.sampleTextFile
+        request.form['form.f1.used'] = ''
+        request.form['form.f2.used'] = ''
+        request.form['form.actions.apply'] = u''
+
+        Form(foo, request)()
+                
+        # check new values in object
+        self.assertEqual(foo.f1, self.sampleText)
+        self.assertEqual(foo.f2, self.sampleText)
+
+    def test_invalid_value(self):
+        foo = FileTest()
+        request = TestRequest()
+
+        # submit an invalid file value
+        request.form['form.f1'] = 'not a file - same as missing input'
+        request.form['form.f1.used'] = ''
+        request.form['form.f2.used'] = ''
+        request.form['form.actions.apply'] = u''
+
+        html = Form(foo, request)()
+
+        self.assert_('Form input is not a file object', html)
+
+    def test_required_validation(self):
+        foo = FileTest()
+        request = TestRequest()
+
+        # submit missing value for required field f1
+        request.form['form.f1.used'] = ''
+        request.form['form.f2.used'] = ''
+        request.form['form.actions.apply'] = u''
+
+        html = Form(foo, request)()
+
+        # confirm error msgs
+        f1_index = html.find('form.f1')
+        f2_index = html.find('form.f2')
+        missing_index = html.find('Required input is missing')
+        self.assert_(missing_index > f1_index)
+        self.assert_(html.find('Required input is missing', f2_index) == -1)
+
+    def test_empty_file(self):
+        foo = FileTest()
+        request = TestRequest()
+
+        # submit missing value for required field f1
+        request.form['form.f2'] = self.emptyFile
+        request.form['form.f2.used'] = ''
+        request.form['form.actions.apply'] = u''
+        # we don't let f1 know that it was rendered
+        # or else it will complain (see test_required_validation) and the
+        # change will not succeed.
+        
+        html = Form(foo, request)()
+
+        # new value for f1 should be field.missing_value (i.e, None)
+        self.assert_(foo.f1 is None)
+
+def test_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(Test))
+    return suite
+



More information about the Zope3-Checkins mailing list