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

zopefoundation / five.formlib / 16399733854

15 Feb 2025 02:34PM UTC coverage: 83.302%. Remained the same
16399733854

push

github

icemac
Back to development: 5.1

49 of 92 branches covered (53.26%)

Branch coverage included in aggregate %.

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
from Testing.ZopeTestCase import FunctionalDocFileSuite
1✔
21
from Testing.ZopeTestCase.zopedoctest.functional import http
1✔
22

23

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

26

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

31
    First, load the configuration files:
32

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

39
    Now for some actual testing...
40

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

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

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

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

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

64
    Clean up:
65

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

70

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

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

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

87

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

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

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

112

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