• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / five.formlib / 12945524283

24 Jan 2025 07:55AM UTC coverage: 83.333% (-0.2%) from 83.493%
12945524283

push

github

icemac
Adapt tests to `multipart >= 1.x`.

49 of 92 branches covered (53.26%)

Branch coverage included in aggregate %.

12 of 13 new or added lines in 1 file covered. (92.31%)

391 of 436 relevant lines covered (89.68%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.5
/src/five/formlib/tests/test_formlib.py
1
##############################################################################
2
#
3
# Copyright (c) 2004, 2005 Zope Corporation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14

15
import typing
1✔
16
import unittest
1✔
17
import webtest
1✔
18
from doctest import DocTestSuite
1✔
19
from uuid import uuid4
1✔
20

21
from Testing.ZopeTestCase import FunctionalDocFileSuite
1✔
22
from Testing.ZopeTestCase.zopedoctest.functional import http
1✔
23

24
_TEST_APP_FOR_ENCODING = webtest.TestApp(None)
1✔
25

26
def test_get_widgets_for_schema_fields():
1✔
27
    """
28
    Test widget lookup for schema fields
29

30
    First, load the configuration files:
31

32
      >>> from Zope2.App import zcml
33
      >>> import Products.Five
34
      >>> zcml.load_config('configure.zcml', Products.Five)
35
      >>> import zope.app.form.browser
36
      >>> zcml.load_config('configure.zcml', zope.app.form.browser)
37

38
    Now for some actual testing...
39

40
      >>> from zope.schema import Choice, TextLine
41
      >>> salutation = Choice(title=u'Salutation',
42
      ...                     values=("Mr.", "Mrs.", "Captain", "Don"))
43
      >>> contactname = TextLine(title=u'Name')
44

45
      >>> from zope.publisher.browser import TestRequest
46
      >>> request = TestRequest()
47
      >>> salutation = salutation.bind(request)
48
      >>> contactname = contactname.bind(request)
49

50
      >>> from zope.component import getMultiAdapter
51
      >>> from zope.formlib.interfaces import IInputWidget
52
      >>> from zope.formlib.textwidgets import TextWidget
53
      >>> from zope.formlib.itemswidgets import DropdownWidget
54

55
      >>> view1 = getMultiAdapter((contactname, request), IInputWidget)
56
      >>> view1.__class__ == TextWidget
57
      True
58

59
      >>> view2 = getMultiAdapter((salutation, request), IInputWidget)
60
      >>> view2.__class__ == DropdownWidget
61
      True
62

63
    Clean up:
64

65
      >>> from zope.component.testing import tearDown
66
      >>> tearDown()
67
    """
68

69

70
def encodeMultipartFormdata(
1✔
71
        fields: typing.List[typing.Tuple[str, str]],
72
        files: typing.Optional[list] = None) -> typing.Tuple[str, str]:
73
    """Encode fields and files to be used in a multipart/form-data request.
74

75
    Returns a tuple of content-type and content.
76

77
    Copied over from `zope.app.wsgi.testlayer` and adapted to return `str` as
78
    `Testing.ZopeTestCase.zopedoctest.functional.http` expects so.
79
    """
80
    if files is None:
1!
81
        files = []
1✔
82
    content_type, content = _TEST_APP_FOR_ENCODING.encode_multipart(
1✔
83
        fields, files)
84
    return content_type, content.decode('utf-8')
1✔
85

86

87
def http_request(url, form_parts=None, body=None, auth=None):
1✔
88
    """perform HTTP request from given parameters.
89

90
    If given, *form_parts* must be an iterable yielding pairs *name*,*value*.
91
    *body* is then computed as a typical form response from it.
92
    Otherwise, if *body* is not `None`, it defines the request content.
93
    Otherwise, a `GET` request is created.
94
    """
95
    if form_parts is not None:
1!
96
        content_type, body = encodeMultipartFormdata(fields=form_parts)
1✔
97
    else:
NEW
98
        content_type = "application/x-www-form-urlencoded"
×
99
    headers = [f'{"GET" if body is None else "POST"} {url} HTTP/1.1']
1✔
100

101
    if auth:
1!
102
        headers.append(f"Authorization: {auth}")
1✔
103
    headers.append("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7")
1✔
104
    if body is not None:
1!
105
        if not body.endswith("\n"):
1!
106
            body += "\n"
×
107
        headers.append(f"Content-Type: {content_type}")
1✔
108
        body = "\n\n" + body
1✔
109
    return http("\n".join(headers) + (body or ''))
1✔
110

111

112
def test_suite():
1✔
113
    return unittest.TestSuite([
1✔
114
        DocTestSuite(),
115
        FunctionalDocFileSuite('forms.txt', package="five.formlib.tests",
116
                               globs=dict(http_request=http_request),
117
                               ),
118
        FunctionalDocFileSuite('formlib.txt', package='five.formlib.tests'),
119
    ])
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc