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

zopefoundation / z3c.form / 5168953570

pending completion
5168953570

push

github

web-flow
Config with pure python template (#114)

* Bumped version for breaking release.

* Drop support for Python 2.7, 3.5, 3.6.

* Add support for Python 3.11.

1316 of 1408 branches covered (93.47%)

Branch coverage included in aggregate %.

420 of 420 new or added lines in 39 files covered. (100.0%)

3716 of 3838 relevant lines covered (96.82%)

0.97 hits per line

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

97.37
/src/z3c/form/tests/test_bugfix.py
1
##############################################################################
2
#
3
# Copyright (c) 2007 Zope Foundation 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
"""Unittests for bug fixes."""
1✔
15

16
import unittest
1✔
17

18

19
class TestApplyChangesDictDatamanager(unittest.TestCase):
1✔
20
    # z3c.form.form.applyChanges could not write a value into an empty
21
    # content dict it got an AttributeError while accessing
22
    # datamanger.get(). This test makes sure that a dictionary content
23
    # does not need to be initialized with all keys which might be
24
    # written on it later on. (This was the behavior before
25
    # datamanger.DictionaryField.get() raised an AttributeError on not
26
    # existing keys.
27

28
    def setUp(self):
1✔
29
        import zope.component
1✔
30
        import zope.interface
1✔
31

32
        import z3c.form.datamanager
1✔
33

34
        zope.component.provideAdapter(
1✔
35
            z3c.form.datamanager.DictionaryField,
36
            (dict, zope.interface.Interface))
37

38
    def tearDown(self):
1✔
39
        import zope.component.globalregistry
1✔
40

41
        import z3c.form.datamanager
1✔
42

43
        zope.component.globalregistry.getGlobalSiteManager().unregisterAdapter(
1✔
44
            z3c.form.datamanager.DictionaryField,
45
            (dict, zope.interface.Interface))
46

47
    def test_applyChanges(self):
1✔
48
        import zope.interface
1✔
49
        import zope.schema
1✔
50

51
        import z3c.form.field
1✔
52
        import z3c.form.form
1✔
53

54
        class TestInterface(zope.interface.Interface):
1✔
55
            text = zope.schema.TextLine(title='text')
1✔
56

57
        class TestForm(z3c.form.form.BaseForm):
1✔
58
            fields = z3c.form.field.Fields(TestInterface)
1✔
59

60
        # content is an empty dict, the `text` key does not yet exist
61
        content = dict()
1✔
62
        form = TestForm(content, request=None)
1✔
63
        data = dict(text='a')
1✔
64
        changes = z3c.form.form.applyChanges(form, content, data)
1✔
65
        self.assertEqual({TestInterface: ['text']}, changes)
1✔
66
        self.assertEqual({'text': 'a'}, content)
1✔
67

68

69
class Mock:
1✔
70
    pass
1✔
71

72

73
class MockNumberFormatter:
1✔
74
    def format(self, value):
1✔
75
        if value is None:
1!
76
            # execution should never get here
77
            raise ValueError('Cannot format None')
×
78
        return str(value)
1✔
79

80

81
class MockLocale:
1✔
82
    def getFormatter(self, category):
1✔
83
        return MockNumberFormatter()
1✔
84

85

86
class OurNone:
1✔
87
    def __eq__(self, other):
1✔
88
        return isinstance(other, (type(None), OurNone))
1✔
89

90

91
OUR_NONE = OurNone()
1✔
92
# OUR_NONE == None
93
# but
94
# OUR_NONE is not None
95

96

97
class ConverterFixTests(unittest.TestCase):
1✔
98
    # most of the time `==` and `is` works the same
99
    # unless you have some custom or mutable values
100

101
    def test_BaseDataConverter_toWidgetValue(self):
1✔
102
        from z3c.form.converter import BaseDataConverter
1✔
103

104
        field = Mock()
1✔
105
        field.missing_value = None
1✔
106
        bdc = BaseDataConverter(field, None)
1✔
107
        self.assertEqual(bdc.toWidgetValue(''), '')
1✔
108
        self.assertEqual(bdc.toWidgetValue(None), '')
1✔
109
        self.assertEqual(bdc.toWidgetValue([]), '[]')
1✔
110

111
        field.missing_value = []
1✔
112
        self.assertEqual(bdc.toWidgetValue(''), '')
1✔
113
        self.assertEqual(bdc.toWidgetValue(None), 'None')
1✔
114
        self.assertEqual(bdc.toWidgetValue([]), '')
1✔
115

116
    def test_NumberDataConverter_toWidgetValue(self):
1✔
117
        from z3c.form.converter import NumberDataConverter
1✔
118

119
        field = Mock()
1✔
120
        field.missing_value = None
1✔
121
        widget = Mock()
1✔
122
        widget.request = Mock()
1✔
123
        widget.request.locale = Mock()
1✔
124
        widget.request.locale.numbers = MockLocale()
1✔
125
        ndc = NumberDataConverter(field, widget)
1✔
126
        self.assertEqual(ndc.toWidgetValue(''), '')
1✔
127
        self.assertEqual(ndc.toWidgetValue(None), '')
1✔
128
        # here is the real deal, OUR_NONE should be considered as None
129
        self.assertEqual(ndc.toWidgetValue(OUR_NONE), '')
1✔
130
        self.assertEqual(ndc.toWidgetValue([]), '[]')
1✔
131

132
        field.missing_value = OUR_NONE
1✔
133
        self.assertEqual(ndc.toWidgetValue(''), '')
1✔
134
        self.assertEqual(ndc.toWidgetValue(None), '')
1✔
135
        self.assertEqual(ndc.toWidgetValue(OUR_NONE), '')
1✔
136
        self.assertEqual(ndc.toWidgetValue([]), '[]')
1✔
137

138

139
def test_suite():
1✔
140
    return unittest.defaultTestLoader.loadTestsFromName(__name__)
1✔
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