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

zopefoundation / five.formlib / 13005766883

28 Jan 2025 07:42AM UTC coverage: 83.302% (-0.2%) from 83.493%
13005766883

Pull #17

github

web-flow
Try again
Pull Request #17: 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%)

390 of 435 relevant lines covered (89.66%)

0.9 hits per line

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

82.05
/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
from doctest import DocTestSuite
1✔
18

19
import webtest
1✔
20

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

24

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

27

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

32
    First, load the configuration files:
33

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

40
    Now for some actual testing...
41

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

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

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

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

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

65
    Clean up:
66

67
      >>> from zope.component.testing import tearDown
68
      >>> tearDown()
69
    """
70

71

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

77
    Returns a tuple of content-type and content.
78

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

88

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

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

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

113

114
def test_suite():
1✔
115
    return unittest.TestSuite([
1✔
116
        DocTestSuite(),
117
        FunctionalDocFileSuite('forms.txt', package="five.formlib.tests",
118
                               globs=dict(http_request=http_request),
119
                               ),
120
        FunctionalDocFileSuite('formlib.txt', package='five.formlib.tests'),
121
    ])
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

© 2025 Coveralls, Inc