Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: appengine/monorail/framework/test/template_helpers_test.py

Issue 1868553004: Open Source Monorail (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is govered by a BSD-style
3 # license that can be found in the LICENSE file or at
4 # https://developers.google.com/open-source/licenses/bsd
5
6 """Unit tests for template_helpers module."""
7
8 import unittest
9
10 from framework import pbproxy_test_pb2
11 from framework import template_helpers
12
13
14 class HelpersUnitTest(unittest.TestCase):
15
16 def testDictionaryProxy(self):
17
18 # basic in 'n out test
19 item = template_helpers.EZTItem(label='foo', group_name='bar')
20
21 self.assertEquals('foo', item.label)
22 self.assertEquals('bar', item.group_name)
23
24 # be sure the __str__ returns the fields
25 self.assertEquals("EZTItem({'group_name': 'bar', 'label': 'foo'})",
26 str(item))
27
28 def testPBProxy(self):
29 """Checks that PBProxy wraps protobuf objects as expected."""
30 # check that protobuf fields are accessible in ".attribute" form
31 pbe = pbproxy_test_pb2.PBProxyExample()
32 pbe.foo = 'foo'
33 pbe.bar = False
34 pbep = template_helpers.PBProxy(pbe)
35 self.assertEqual(pbep.foo, 'foo')
36 # _bool suffix converts protobuf field 'bar' to None (EZT boolean false)
37 self.assertEqual(pbep.bar_bool, None)
38
39 # check that a new field can be added to the PBProxy
40 pbep.baz = 'bif'
41 self.assertEqual(pbep.baz, 'bif')
42
43 # check that a PBProxy-local field can hide a protobuf field
44 pbep.foo = 'local foo'
45 self.assertEqual(pbep.foo, 'local foo')
46
47 # check that a nested protobuf is recursively wrapped with a PBProxy
48 pbn = pbproxy_test_pb2.PBProxyNested()
49 pbn.nested = pbproxy_test_pb2.PBProxyExample()
50 pbn.nested.foo = 'bar'
51 pbn.nested.bar = True
52 pbnp = template_helpers.PBProxy(pbn)
53 self.assertEqual(pbnp.nested.foo, 'bar')
54 # _bool suffix converts protobuf field 'bar' to 'yes' (EZT boolean true)
55 self.assertEqual(pbnp.nested.bar_bool, 'yes')
56
57 # check that 'repeated' lists of items produce a list of strings
58 pbn.multiple_strings.append('1')
59 pbn.multiple_strings.append('2')
60 self.assertEqual(pbnp.multiple_strings, ['1', '2'])
61
62 # check that 'repeated' messages produce lists of PBProxy instances
63 pbe1 = pbproxy_test_pb2.PBProxyExample()
64 pbn.multiple_pbes.append(pbe1)
65 pbe1.foo = '1'
66 pbe1.bar = True
67 pbe2 = pbproxy_test_pb2.PBProxyExample()
68 pbn.multiple_pbes.append(pbe2)
69 pbe2.foo = '2'
70 pbe2.bar = False
71 self.assertEqual(pbnp.multiple_pbes[0].foo, '1')
72 self.assertEqual(pbnp.multiple_pbes[0].bar_bool, 'yes')
73 self.assertEqual(pbnp.multiple_pbes[1].foo, '2')
74 self.assertEqual(pbnp.multiple_pbes[1].bar_bool, None)
75
76 def testFitTextMethods(self):
77 """Tests both FitUnsafeText with an eye on i18n."""
78 # pylint: disable=anomalous-unicode-escape-in-string
79 test_data = (
80 u'This is a short string.',
81
82 u'This is a much longer string. '
83 u'This is a much longer string. '
84 u'This is a much longer string. '
85 u'This is a much longer string. '
86 u'This is a much longer string. '
87 u'This is a much longer string. '
88 u'This is a much longer string. '
89 u'This is a much longer string. '
90 u'This is a much longer string. '
91 u'This is a much longer string. ',
92
93 # This is a short escaped i18n string
94 '\xd5\xa1\xd5\xba\xd5\xa1\xd5\xaf\xd5\xab'.decode('utf-8'),
95
96 # This is a longer i18n string
97 '\xd5\xa1\xd5\xba\xd5\xa1\xd5\xaf\xd5\xab '
98 '\xe6\x88\x91\xe8\x83\xbd\xe5\x90\x9e '
99 '\xd5\xa1\xd5\xba\xd5\xa1\xd5\xaf\xd5\xab '
100 '\xe6\x88\x91\xe8\x83\xbd\xe5\x90\x9e '
101 '\xd5\xa1\xd5\xba\xd5\xa1\xd5\xaf\xd5\xab '
102 '\xe6\x88\x91\xe8\x83\xbd\xe5\x90\x9e '
103 '\xd5\xa1\xd5\xba\xd5\xa1\xd5\xaf\xd5\xab '
104 '\xe6\x88\x91\xe8\x83\xbd\xe5\x90\x9e '.decode('utf-8'),
105
106 # This is a longer i18n string that was causing trouble.
107 '\u041d\u0430 \u0431\u0435\u0440\u0435\u0433\u0443'
108 ' \u043f\u0443\u0441\u0442\u044b\u043d\u043d\u044b\u0445'
109 ' \u0432\u043e\u043b\u043d \u0421\u0442\u043e\u044f\u043b'
110 ' \u043e\u043d, \u0434\u0443\u043c'
111 ' \u0432\u0435\u043b\u0438\u043a\u0438\u0445'
112 ' \u043f\u043e\u043b\u043d, \u0418'
113 ' \u0432\u0434\u0430\u043b\u044c'
114 ' \u0433\u043b\u044f\u0434\u0435\u043b.'
115 ' \u041f\u0440\u0435\u0434 \u043d\u0438\u043c'
116 ' \u0448\u0438\u0440\u043e\u043a\u043e'
117 ' \u0420\u0435\u043a\u0430'
118 ' \u043d\u0435\u0441\u043b\u0430\u0441\u044f;'
119 ' \u0431\u0435\u0434\u043d\u044b\u0439'
120 ' \u0447\u0451\u043b\u043d \u041f\u043e'
121 ' \u043d\u0435\u0439'
122 ' \u0441\u0442\u0440\u0435\u043c\u0438\u043b\u0441\u044f'
123 ' \u043e\u0434\u0438\u043d\u043e\u043a\u043e.'
124 ' \u041f\u043e \u043c\u0448\u0438\u0441\u0442\u044b\u043c,'
125 ' \u0442\u043e\u043f\u043a\u0438\u043c'
126 ' \u0431\u0435\u0440\u0435\u0433\u0430\u043c'
127 ' \u0427\u0435\u0440\u043d\u0435\u043b\u0438'
128 ' \u0438\u0437\u0431\u044b \u0437\u0434\u0435\u0441\u044c'
129 ' \u0438 \u0442\u0430\u043c, \u041f\u0440\u0438\u044e\u0442'
130 ' \u0443\u0431\u043e\u0433\u043e\u0433\u043e'
131 ' \u0447\u0443\u0445\u043e\u043d\u0446\u0430;'
132 ' \u0418 \u043b\u0435\u0441,'
133 ' \u043d\u0435\u0432\u0435\u0434\u043e\u043c\u044b\u0439'
134 ' \u043b\u0443\u0447\u0430\u043c \u0412'
135 ' \u0442\u0443\u043c\u0430\u043d\u0435'
136 ' \u0441\u043f\u0440\u044f\u0442\u0430\u043d\u043d\u043e'
137 '\u0433\u043e \u0441\u043e\u043b\u043d\u0446\u0430,'
138 ' \u041a\u0440\u0443\u0433\u043e\u043c'
139 ' \u0448\u0443\u043c\u0435\u043b.'.decode('utf-8'))
140
141 for unicode_s in test_data:
142 # Get the length in characters, not bytes.
143 length = len(unicode_s)
144
145 # Test the FitUnsafeText method at the length boundary.
146 fitted_unsafe_text = template_helpers.FitUnsafeText(unicode_s, length)
147 self.assertEqual(fitted_unsafe_text, unicode_s)
148
149 # Set some values that test FitString well.
150 available_space = length / 2
151 max_trailing = length / 4
152 # Break the string at various places - symmetric range around 0
153 for i in range(1-max_trailing, max_trailing):
154 # Test the FitUnsafeText method.
155 fitted_unsafe_text = template_helpers.FitUnsafeText(
156 unicode_s, available_space - i)
157 self.assertEqual(fitted_unsafe_text[:available_space - i],
158 unicode_s[:available_space - i])
159
160 # Test a string that is already unicode
161 u_string = u'This is already unicode'
162 fitted_unsafe_text = template_helpers.FitUnsafeText(u_string, 100)
163 self.assertEqual(u_string, fitted_unsafe_text)
164
165 # Test a string that is already unicode, and has non-ascii in it.
166 u_string = u'This is already unicode este\\u0301tico'
167 fitted_unsafe_text = template_helpers.FitUnsafeText(u_string, 100)
168 self.assertEqual(u_string, fitted_unsafe_text)
169
170 def testEZTError(self):
171 errors = template_helpers.EZTError()
172 self.assertFalse(errors.AnyErrors())
173
174 errors.error_a = 'A'
175 self.assertTrue(errors.AnyErrors())
176 self.assertEquals('A', errors.error_a)
177
178 errors.SetError('error_b', 'B')
179 self.assertTrue(errors.AnyErrors())
180 self.assertEquals('A', errors.error_a)
181 self.assertEquals('B', errors.error_b)
182
183 def testBytesKbOrMb(self):
184 self.assertEqual('1023 bytes', template_helpers.BytesKbOrMb(1023))
185 self.assertEqual('1.0 KB', template_helpers.BytesKbOrMb(1024))
186 self.assertEqual('1023 KB', template_helpers.BytesKbOrMb(1024 * 1023))
187 self.assertEqual('1.0 MB', template_helpers.BytesKbOrMb(1024 * 1024))
188 self.assertEqual('98.0 MB', template_helpers.BytesKbOrMb(98 * 1024 * 1024))
189 self.assertEqual('99 MB', template_helpers.BytesKbOrMb(99 * 1024 * 1024))
190
191
192 if __name__ == '__main__':
193 unittest.main()
OLDNEW
« no previous file with comments | « appengine/monorail/framework/test/table_view_helpers_test.py ('k') | appengine/monorail/framework/test/timestr_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698