Skip to content

Commit

Permalink
Update tests to run with multipart >= 1.1+. (#17)
Browse files Browse the repository at this point in the history
Fix deprecation warning regarding `datetime.utcnow`.
  • Loading branch information
icemac authored Dec 2, 2024
1 parent 15f4a99 commit ffaf9b1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 62 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CHANGES
6.2 (unreleased)
================

- Nothing changed yet.
- Update tests to run with ``multipart >= 1.1+``.


6.1 (2024-10-21)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def read(*rnames):
'zope.app.appsetup',
'zope.testing',
'zope.testrunner',
'zope.app.wsgi >= 5.2',
'zope.app.wsgi >= 5.3',
'webtest',
]

Expand Down
13 changes: 11 additions & 2 deletions src/zope/app/form/browser/editview.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"""Edit View Classes"""
__docformat__ = 'restructuredtext'

from datetime import datetime
import datetime
import sys

import transaction
import zope.component
Expand All @@ -37,6 +38,9 @@
from zope.app.form.utility import setUpEditWidgets


PY310_OR_OLDER = sys.version_info < (3, 11)


class EditView(BrowserView):
"""Simple edit-view base class
Expand Down Expand Up @@ -112,9 +116,14 @@ def update(self):
self.changed()
formatter = self.request.locale.dates.getFormatter(
'dateTime', 'medium')
if PY310_OR_OLDER:
now = datetime.datetime.now(datetime.timezone.utc)
else:
now = datetime.datetime.now(datetime.UTC)

status = _("Updated on ${date_time}",
mapping={'date_time':
formatter.format(datetime.utcnow())})
formatter.format(now)})

self.update_status = status
return status
Expand Down
76 changes: 24 additions & 52 deletions src/zope/app/form/browser/i18n.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,27 @@ i18n messages in the schema.

Let's take this simple add form...

>>> print(http(r"""
>>> print(http(b"""
... GET /addfieldcontent.html HTTP/1.1
... """, handle_errors=False))
HTTP/1.1 200 Ok
...

with an error...

>>> print(http(r"""
>>> content_type, content = encodeMultipartFormdata([
... ('field.title', ''),
... ('field.description', ''),
... ('field.somenumber', '0'),
... ('UPDATE_SUBMIT', 'Hinzufxgen'),
... ('add_input_name', ''),
... ])
>>> print(http(b"""
... POST /addfieldcontent.html HTTP/1.1
... Content-Length: 670
... Content-Type: multipart/form-data; boundary=---------------------------19588947601368617292863650127
... Content-Type: %b
...
... -----------------------------19588947601368617292863650127
... Content-Disposition: form-data; name="field.title"
...
...
... -----------------------------19588947601368617292863650127
... Content-Disposition: form-data; name="field.description"
...
...
... -----------------------------19588947601368617292863650127
... Content-Disposition: form-data; name="field.somenumber"
...
... 0
... -----------------------------19588947601368617292863650127
... Content-Disposition: form-data; name="UPDATE_SUBMIT"
...
... Hinzufxgen
... -----------------------------19588947601368617292863650127
... Content-Disposition: form-data; name="add_input_name"
...
...
... -----------------------------19588947601368617292863650127--
... """, handle_errors=False))
... %b
... """ % (content_type, content), handle_errors=False))
HTTP/1.1 200 Ok
...
There are <strong>1</strong> input errors.
Expand All @@ -54,7 +40,7 @@ Translated

And now the add form in German:

>>> print(http(r"""
>>> print(http(b"""
... GET /addfieldcontent.html HTTP/1.1
... Accept-Language: de
... """, handle_errors=False))
Expand All @@ -67,34 +53,20 @@ And now the add form in German:

The same with an input error:

>>> print(http(r"""
>>> content_type, content = encodeMultipartFormdata([
... ('field.title', ''),
... ('field.description', ''),
... ('field.somenumber', '0'),
... ('UPDATE_SUBMIT', 'Hinzufxgen'),
... ('add_input_name', ''),
... ])
>>> print(http(b"""
... POST /addfieldcontent.html HTTP/1.1
... Accept-Language: de
... Content-Length: 670
... Content-Type: multipart/form-data; boundary=---------------------------19588947601368617292863650127
... Content-Type: %b
...
... -----------------------------19588947601368617292863650127
... Content-Disposition: form-data; name="field.title"
...
...
... -----------------------------19588947601368617292863650127
... Content-Disposition: form-data; name="field.description"
...
...
... -----------------------------19588947601368617292863650127
... Content-Disposition: form-data; name="field.somenumber"
...
... 0
... -----------------------------19588947601368617292863650127
... Content-Disposition: form-data; name="UPDATE_SUBMIT"
...
... Hinzufxgen
... -----------------------------19588947601368617292863650127
... Content-Disposition: form-data; name="add_input_name"
...
...
... -----------------------------19588947601368617292863650127--
... """, handle_errors=False))
... %b
... """ % (content_type, content), handle_errors=False))
HTTP/1.1 200 Ok
...Felderinhalt hinzuf...
...<strong>1</strong>...
Expand Down
9 changes: 3 additions & 6 deletions src/zope/app/form/browser/tests/test_functional_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import unittest

from persistent import Persistent
from zope.app.wsgi.testlayer import encodeMultipartFormdata
from zope.app.wsgi.testlayer import http
from zope.i18nmessageid import MessageFactory
from zope.interface import Interface
Expand Down Expand Up @@ -81,14 +82,10 @@ def setUp(test):
wsgi_app = AppFormLayer.make_wsgi_app()

def _http(query_str, *args, **kwargs):
# Strip leading \n
query_str = query_str.lstrip()
if not isinstance(query_str, bytes):
query_str = query_str.encode("ascii")
response = http(wsgi_app, query_str, *args, **kwargs)
return response
return http(wsgi_app, query_str, *args, **kwargs)

test.globs['http'] = _http
test.globs['encodeMultipartFormdata'] = encodeMultipartFormdata

i18n = doctest.DocFileSuite('../i18n.rst',
setUp=setUp,
Expand Down

0 comments on commit ffaf9b1

Please sign in to comment.