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

zopefoundation / Zope / 3956162881

pending completion
3956162881

push

github

Michael Howitz
Update to deprecation warning free releases.

4401 of 7036 branches covered (62.55%)

Branch coverage included in aggregate %.

27161 of 31488 relevant lines covered (86.26%)

0.86 hits per line

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

90.98
/src/ZPublisher/tests/test_utils.py
1
##############################################################################
2
#
3
# Copyright (c) 2017 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

15
import unittest
1✔
16

17

18
class SafeUnicodeTests(unittest.TestCase):
1✔
19

20
    def _makeOne(self, value):
1✔
21
        from ZPublisher.utils import safe_unicode
1✔
22
        return safe_unicode(value)
1✔
23

24
    def test_ascii(self):
1✔
25
        self.assertEqual(self._makeOne('foo'), 'foo')
1✔
26
        self.assertEqual(self._makeOne(b'foo'), 'foo')
1✔
27

28
    def test_latin_1(self):
1✔
29
        self.assertEqual(self._makeOne(b'fo\xf6'), 'fo\ufffd')
1✔
30

31
    def test_unicode(self):
1✔
32
        self.assertEqual(self._makeOne('foö'), 'foö')
1✔
33

34
    def test_utf_8(self):
1✔
35
        self.assertEqual(self._makeOne('test\xc2\xae'), 'test\xc2\xae')
1✔
36
        self.assertEqual(self._makeOne(b'test\xc2\xae'), 'test\xae')
1✔
37

38

39
class NoUpdatePropertyManager:
1✔
40
    """PropertyManager without _updateProperty method.
41

42
    This is a simplified version of the original PropertyManager,
43
    with only the methods we need.
44
    """
45
    _properties = ()
1✔
46

47
    def _setPropValue(self, id, value):
1✔
48
        if type(value) == list:
1!
49
            value = tuple(value)
1✔
50
        setattr(self, id, value)
1✔
51

52
    def _setProperty(self, id, value, type='string'):
1✔
53
        self._properties = self._properties + ({'id': id, 'type': type},)
1✔
54
        self._setPropValue(id, value)
1✔
55

56
    def hasProperty(self, id):
1✔
57
        for p in self._properties:
1!
58
            if id == p['id']:
1!
59
                return 1
1✔
60
        return 0
×
61

62
    def getProperty(self, id, d=None):
1✔
63
        if self.hasProperty(id):
1!
64
            return getattr(self, id)
1✔
65
        return d
×
66

67
    def getPropertyType(self, id):
1✔
68
        for md in self._properties:
1!
69
            if md['id'] == id:
1!
70
                return md.get('type', 'string')
1✔
71
        return None
×
72

73
    def _propertyMap(self):
1✔
74
        return self._properties
1✔
75

76
    def propertyMap(self):
1✔
77
        return tuple(dict.copy() for dict in self._propertyMap())
1!
78

79

80
class NoPropertiesManager(NoUpdatePropertyManager):
1✔
81
    """PropertyManager with _updateProperty method but without _properties."""
82
    _properties = None
1✔
83

84
    def _updateProperty(self, id, value):
1✔
85
        self._setPropValue(id, value)
×
86

87

88
class FixPropertiesTests(unittest.TestCase):
1✔
89

90
    def _makeOne(self):
1✔
91
        from OFS.PropertyManager import PropertyManager
1✔
92

93
        return PropertyManager()
1✔
94

95
    def test_lines(self):
1✔
96
        from ZPublisher.utils import fix_properties
1✔
97

98
        obj = self._makeOne()
1✔
99
        obj._setProperty("mixed", ["text and", b"bytes"], "lines")
1✔
100
        self.assertEqual(obj.getProperty("mixed"), ("text and", b"bytes"))
1✔
101
        self.assertEqual(obj.getPropertyType("mixed"), "lines")
1✔
102

103
        fix_properties(obj)
1✔
104
        self.assertEqual(obj.getProperty("mixed"), ("text and", "bytes"))
1✔
105
        self.assertEqual(obj.getPropertyType("mixed"), "lines")
1✔
106

107
    def test_ulines(self):
1✔
108
        from ZPublisher.utils import fix_properties
1✔
109

110
        obj = self._makeOne()
1✔
111
        obj._setProperty("mixed", ["text and", b"bytes"], "ulines")
1✔
112
        self.assertEqual(obj.getProperty("mixed"), ("text and", b"bytes"))
1✔
113
        self.assertEqual(obj.getPropertyType("mixed"), "ulines")
1✔
114

115
        fix_properties(obj)
1✔
116
        self.assertEqual(obj.getProperty("mixed"), ("text and", "bytes"))
1✔
117
        self.assertEqual(obj.getPropertyType("mixed"), "lines")
1✔
118

119
    def test_utokens(self):
1✔
120
        from ZPublisher.utils import fix_properties
1✔
121

122
        obj = self._makeOne()
1✔
123
        obj._setProperty("mixed", ["text", "and", b"bytes"], "utokens")
1✔
124
        self.assertEqual(obj.getProperty("mixed"), ("text", "and", b"bytes"))
1✔
125
        self.assertEqual(obj.getPropertyType("mixed"), "utokens")
1✔
126

127
        fix_properties(obj)
1✔
128
        self.assertEqual(obj.getProperty("mixed"), ("text", "and", "bytes"))
1✔
129
        self.assertEqual(obj.getPropertyType("mixed"), "tokens")
1✔
130

131
    def test_utext(self):
1✔
132
        from ZPublisher.utils import fix_properties
1✔
133

134
        obj = self._makeOne()
1✔
135
        obj._setProperty("prop1", "multiple\nlines", "utext")
1✔
136
        self.assertEqual(obj.getProperty("prop1"), "multiple\nlines")
1✔
137
        self.assertEqual(obj.getPropertyType("prop1"), "utext")
1✔
138

139
        fix_properties(obj)
1✔
140
        self.assertEqual(obj.getProperty("prop1"), "multiple\nlines")
1✔
141
        self.assertEqual(obj.getPropertyType("prop1"), "text")
1✔
142

143
    def test_ustring(self):
1✔
144
        from ZPublisher.utils import fix_properties
1✔
145

146
        obj = self._makeOne()
1✔
147
        obj._setProperty("prop1", "single line", "ustring")
1✔
148
        self.assertEqual(obj.getProperty("prop1"), "single line")
1✔
149
        self.assertEqual(obj.getPropertyType("prop1"), "ustring")
1✔
150

151
        fix_properties(obj)
1✔
152
        self.assertEqual(obj.getProperty("prop1"), "single line")
1✔
153
        self.assertEqual(obj.getPropertyType("prop1"), "string")
1✔
154

155
    def test_no_update(self):
1✔
156
        # Test that an object without _updateProperty method does not trip up
157
        # our code.
158
        from ZPublisher.utils import fix_properties
1✔
159

160
        obj = NoUpdatePropertyManager()
1✔
161
        obj._setProperty("mixed", ["text and", b"bytes"], "lines")
1✔
162
        self.assertEqual(obj.getProperty("mixed"), ("text and", b"bytes"))
1✔
163
        self.assertEqual(obj.getPropertyType("mixed"), "lines")
1✔
164

165
        # This should not raise an error.
166
        fix_properties(obj)
1✔
167
        # The properties should have remained the same.
168
        self.assertEqual(obj.getProperty("mixed"), ("text and", b"bytes"))
1✔
169
        self.assertEqual(obj.getPropertyType("mixed"), "lines")
1✔
170

171
    def test_no_properties(self):
1✔
172
        # Test that an object with a failing propertyMap method,
173
        # due to _properties=None, does not trip up our code.
174
        from ZPublisher.utils import fix_properties
1✔
175

176
        obj = NoPropertiesManager()
1✔
177
        # This should not raise an error.
178
        fix_properties(obj)
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