1 #
2 # Copyright (c) 2001 Richard Jones
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # This module is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 #
11 # $Id: test_htmltemplate.py,v 1.11 2002-02-21 23:11:45 richard Exp $
13 import unittest, cgi, time
15 from roundup import date, password
16 from roundup.htmltemplate import TemplateFunctions
17 from roundup.i18n import _
18 from roundup.hyperdb import String, Password, Date, Interval, Link, Multilink
20 class Class:
21 def get(self, nodeid, attribute, default=None):
22 if attribute == 'string':
23 return 'Node %s: I am a string'%nodeid
24 elif attribute == 'filename':
25 return 'file.foo'
26 elif attribute == 'date':
27 return date.Date('2000-01-01')
28 elif attribute == 'reldate':
29 return date.Date() + date.Interval('- 2y 1m')
30 elif attribute == 'interval':
31 return date.Interval('-3d')
32 elif attribute == 'link':
33 return '1'
34 elif attribute == 'multilink':
35 return ['1', '2']
36 elif attribute == 'password':
37 return password.Password('sekrit')
38 elif attribute == 'key':
39 return 'the key'+nodeid
40 elif attribute == 'html':
41 return '<html>hello, I am HTML</html>'
42 elif attribute == 'multiline':
43 return 'hello\nworld'
44 def list(self):
45 return ['1', '2']
46 def getprops(self):
47 return {'string': String(), 'date': Date(), 'interval': Interval(),
48 'link': Link('other'), 'multilink': Multilink('other'),
49 'password': Password(), 'html': String(), 'key': String(),
50 'novalue': String(), 'filename': String(), 'multiline': String(),
51 'reldate': Date()}
52 def labelprop(self):
53 return 'key'
55 class Database:
56 classes = {'other': Class()}
57 def getclass(self, name):
58 return Class()
59 def __getattr(self, name):
60 return Class()
62 class Client:
63 write = None
65 class NodeCase(unittest.TestCase):
66 def setUp(self):
67 ''' Set up the harness for calling the individual tests
68 '''
69 self.tf = tf = TemplateFunctions()
70 tf.nodeid = '1'
71 tf.cl = Class()
72 tf.classname = 'test_class'
73 tf.properties = tf.cl.getprops()
74 tf.db = Database()
76 # def do_plain(self, property, escape=0):
77 def testPlain_string(self):
78 s = 'Node 1: I am a string'
79 self.assertEqual(self.tf.do_plain('string'), s)
81 def testPlain_password(self):
82 self.assertEqual(self.tf.do_plain('password'), '*encrypted*')
84 def testPlain_html(self):
85 s = '<html>hello, I am HTML</html>'
86 self.assertEqual(self.tf.do_plain('html', escape=0), s)
87 s = cgi.escape(s)
88 self.assertEqual(self.tf.do_plain('html', escape=1), s)
90 def testPlain_date(self):
91 self.assertEqual(self.tf.do_plain('date'), '2000-01-01.00:00:00')
93 def testPlain_interval(self):
94 self.assertEqual(self.tf.do_plain('interval'), '- 3d')
96 def testPlain_link(self):
97 self.assertEqual(self.tf.do_plain('link'), 'the key1')
99 def testPlain_multilink(self):
100 self.assertEqual(self.tf.do_plain('multilink'), '1, 2')
103 # def do_field(self, property, size=None, showid=0):
104 def testField_string(self):
105 self.assertEqual(self.tf.do_field('string'),
106 '<input name="string" value="Node 1: I am a string" size="30">')
107 self.assertEqual(self.tf.do_field('string', size=10),
108 '<input name="string" value="Node 1: I am a string" size="10">')
110 def testField_password(self):
111 self.assertEqual(self.tf.do_field('password'),
112 '<input type="password" name="password" size="30">')
113 self.assertEqual(self.tf.do_field('password', size=10),
114 '<input type="password" name="password" size="10">')
116 def testField_html(self):
117 self.assertEqual(self.tf.do_field('html'), '<input name="html" '
118 'value="<html>hello, I am HTML</html>" size="30">')
119 self.assertEqual(self.tf.do_field('html', size=10),
120 '<input name="html" value="<html>hello, I am '
121 'HTML</html>" size="10">')
123 def testField_date(self):
124 self.assertEqual(self.tf.do_field('date'),
125 '<input name="date" value="2000-01-01.00:00:00" size="30">')
126 self.assertEqual(self.tf.do_field('date', size=10),
127 '<input name="date" value="2000-01-01.00:00:00" size="10">')
129 def testField_interval(self):
130 self.assertEqual(self.tf.do_field('interval'),
131 '<input name="interval" value="- 3d" size="30">')
132 self.assertEqual(self.tf.do_field('interval', size=10),
133 '<input name="interval" value="- 3d" size="10">')
135 def testField_link(self):
136 self.assertEqual(self.tf.do_field('link'), '''<select name="link">
137 <option value="-1">- no selection -</option>
138 <option selected value="1">the key1</option>
139 <option value="2">the key2</option>
140 </select>''')
142 def testField_multilink(self):
143 self.assertEqual(self.tf.do_field('multilink'),
144 '<input name="multilink" size="30" value="the key1,the key2">')
145 self.assertEqual(self.tf.do_field('multilink', size=10),
146 '<input name="multilink" size="10" value="the key1,the key2">')
148 # def do_multiline(self, property, rows=5, cols=40)
149 def testMultiline_string(self):
150 self.assertEqual(self.tf.do_multiline('multiline'),
151 '<textarea name="multiline" rows="5" cols="40">'
152 'hello\nworld</textarea>')
153 self.assertEqual(self.tf.do_multiline('multiline', rows=10),
154 '<textarea name="multiline" rows="10" cols="40">'
155 'hello\nworld</textarea>')
156 self.assertEqual(self.tf.do_multiline('multiline', cols=10),
157 '<textarea name="multiline" rows="5" cols="10">'
158 'hello\nworld</textarea>')
160 def testMultiline_nonstring(self):
161 s = _('[Multiline: not a string]')
162 self.assertEqual(self.tf.do_multiline('date'), s)
163 self.assertEqual(self.tf.do_multiline('interval'), s)
164 self.assertEqual(self.tf.do_multiline('password'), s)
165 self.assertEqual(self.tf.do_multiline('link'), s)
166 self.assertEqual(self.tf.do_multiline('multilink'), s)
168 # def do_menu(self, property, size=None, height=None, showid=0):
169 def testMenu_nonlinks(self):
170 s = _('[Menu: not a link]')
171 self.assertEqual(self.tf.do_menu('string'), s)
172 self.assertEqual(self.tf.do_menu('date'), s)
173 self.assertEqual(self.tf.do_menu('interval'), s)
174 self.assertEqual(self.tf.do_menu('password'), s)
176 def testMenu_link(self):
177 self.assertEqual(self.tf.do_menu('link'), '''<select name="link">
178 <option value="-1">- no selection -</option>
179 <option selected value="1">the key1</option>
180 <option value="2">the key2</option>
181 </select>''')
182 self.assertEqual(self.tf.do_menu('link', size=6),
183 '''<select name="link">
184 <option value="-1">- no selection -</option>
185 <option selected value="1">the...</option>
186 <option value="2">the...</option>
187 </select>''')
188 self.assertEqual(self.tf.do_menu('link', showid=1),
189 '''<select name="link">
190 <option value="-1">- no selection -</option>
191 <option selected value="1">other1: the key1</option>
192 <option value="2">other2: the key2</option>
193 </select>''')
195 def testMenu_multilink(self):
196 self.assertEqual(self.tf.do_menu('multilink', height=10),
197 '''<select multiple name="multilink" size="10">
198 <option selected value="1">the key1</option>
199 <option selected value="2">the key2</option>
200 </select>''')
201 self.assertEqual(self.tf.do_menu('multilink', size=6, height=10),
202 '''<select multiple name="multilink" size="10">
203 <option selected value="1">the...</option>
204 <option selected value="2">the...</option>
205 </select>''')
206 self.assertEqual(self.tf.do_menu('multilink', showid=1),
207 '''<select multiple name="multilink" size="2">
208 <option selected value="1">other1: the key1</option>
209 <option selected value="2">other2: the key2</option>
210 </select>''')
212 # def do_link(self, property=None, is_download=0):
213 def testLink_novalue(self):
214 self.assertEqual(self.tf.do_link('novalue'),
215 _('[no %(propname)s]')%{'propname':'novalue'.capitalize()})
217 def testLink_string(self):
218 self.assertEqual(self.tf.do_link('string'),
219 '<a href="test_class1">Node 1: I am a string</a>')
221 def testLink_file(self):
222 self.assertEqual(self.tf.do_link('filename', is_download=1),
223 '<a href="test_class1/file.foo">file.foo</a>')
225 def testLink_date(self):
226 self.assertEqual(self.tf.do_link('date'),
227 '<a href="test_class1">2000-01-01.00:00:00</a>')
229 def testLink_interval(self):
230 self.assertEqual(self.tf.do_link('interval'),
231 '<a href="test_class1">- 3d</a>')
233 def testLink_link(self):
234 self.assertEqual(self.tf.do_link('link'),
235 '<a href="other1">the key1</a>')
237 def testLink_multilink(self):
238 self.assertEqual(self.tf.do_link('multilink'),
239 '<a href="other1">the key1</a>, <a href="other2">the key2</a>')
241 # def do_count(self, property, **args):
242 def testCount_nonlinks(self):
243 s = _('[Count: not a Multilink]')
244 self.assertEqual(self.tf.do_count('string'), s)
245 self.assertEqual(self.tf.do_count('date'), s)
246 self.assertEqual(self.tf.do_count('interval'), s)
247 self.assertEqual(self.tf.do_count('password'), s)
248 self.assertEqual(self.tf.do_count('link'), s)
250 def testCount_multilink(self):
251 self.assertEqual(self.tf.do_count('multilink'), '2')
253 # def do_reldate(self, property, pretty=0):
254 def testReldate_nondate(self):
255 s = _('[Reldate: not a Date]')
256 self.assertEqual(self.tf.do_reldate('string'), s)
257 self.assertEqual(self.tf.do_reldate('interval'), s)
258 self.assertEqual(self.tf.do_reldate('password'), s)
259 self.assertEqual(self.tf.do_reldate('link'), s)
260 self.assertEqual(self.tf.do_reldate('multilink'), s)
262 def testReldate_date(self):
263 self.assertEqual(self.tf.do_reldate('reldate'), '- 2y 1m')
264 date = self.tf.cl.get('1', 'reldate')
265 self.assertEqual(self.tf.do_reldate('reldate', pretty=1), date.pretty())
267 # def do_download(self, property):
268 def testDownload_novalue(self):
269 self.assertEqual(self.tf.do_download('novalue'),
270 _('[no %(propname)s]')%{'propname':'novalue'.capitalize()})
272 def testDownload_string(self):
273 self.assertEqual(self.tf.do_download('string'),
274 '<a href="test_class1/Node 1: I am a string">Node 1: '
275 'I am a string</a>')
277 def testDownload_file(self):
278 self.assertEqual(self.tf.do_download('filename', is_download=1),
279 '<a href="test_class1/file.foo">file.foo</a>')
281 def testDownload_date(self):
282 self.assertEqual(self.tf.do_download('date'),
283 '<a href="test_class1/2000-01-01.00:00:00">2000-01-01.00:00:00</a>')
285 def testDownload_interval(self):
286 self.assertEqual(self.tf.do_download('interval'),
287 '<a href="test_class1/- 3d">- 3d</a>')
289 def testDownload_link(self):
290 self.assertEqual(self.tf.do_download('link'),
291 '<a href="other1/the key1">the key1</a>')
293 def testDownload_multilink(self):
294 self.assertEqual(self.tf.do_download('multilink'),
295 '<a href="other1/the key1">the key1</a>, '
296 '<a href="other2/the key2">the key2</a>')
298 # def do_checklist(self, property, reverse=0):
299 def testChecklink_nonlinks(self):
300 s = _('[Checklist: not a link]')
301 self.assertEqual(self.tf.do_checklist('string'), s)
302 self.assertEqual(self.tf.do_checklist('date'), s)
303 self.assertEqual(self.tf.do_checklist('interval'), s)
304 self.assertEqual(self.tf.do_checklist('password'), s)
306 def testChecklink_link(self):
307 self.assertEqual(self.tf.do_checklist('link'),
308 '''the key1:<input type="checkbox" checked name="link" value="the key1">
309 the key2:<input type="checkbox" name="link" value="the key2">
310 [unselected]:<input type="checkbox" name="link" value="-1">''')
312 def testChecklink_multilink(self):
313 self.assertEqual(self.tf.do_checklist('multilink'),
314 '''the key1:<input type="checkbox" checked name="multilink" value="the key1">
315 the key2:<input type="checkbox" checked name="multilink" value="the key2">''')
317 # def do_note(self, rows=5, cols=80):
318 def testNote(self):
319 self.assertEqual(self.tf.do_note(), '<textarea name="__note" '
320 'wrap="hard" rows=5 cols=80></textarea>')
322 # def do_list(self, property, reverse=0):
323 def testList_nonlinks(self):
324 s = _('[List: not a Multilink]')
325 self.assertEqual(self.tf.do_list('string'), s)
326 self.assertEqual(self.tf.do_list('date'), s)
327 self.assertEqual(self.tf.do_list('interval'), s)
328 self.assertEqual(self.tf.do_list('password'), s)
329 self.assertEqual(self.tf.do_list('link'), s)
331 def testList_multilink(self):
332 # TODO: test this (needs to have lots and lots of support!
333 #self.assertEqual(self.tf.do_list('multilink'),'')
334 pass
336 def testClasshelp(self):
337 self.assertEqual(self.tf.do_classhelp('theclass', 'prop1,prop2'),
338 '<a href="javascript:help_window(\'classhelp?classname=theclass'
339 '&properties=prop1,prop2\', \'400\', \'400\')"><b>(?)</b></a>')
341 def suite():
342 return unittest.makeSuite(NodeCase, 'test')
345 #
346 # $Log: not supported by cvs2svn $
347 # Revision 1.10 2002/02/21 06:57:39 richard
348 # . Added popup help for classes using the classhelp html template function.
349 # - add <display call="classhelp('priority', 'id,name,description')">
350 # to an item page, and it generates a link to a popup window which displays
351 # the id, name and description for the priority class. The description
352 # field won't exist in most installations, but it will be added to the
353 # default templates.
354 #
355 # Revision 1.9 2002/02/15 07:08:45 richard
356 # . Alternate email addresses are now available for users. See the MIGRATION
357 # file for info on how to activate the feature.
358 #
359 # Revision 1.8 2002/02/06 03:47:16 richard
360 # . #511586 ] unittest FAIL: testReldate_date
361 #
362 # Revision 1.7 2002/01/23 20:09:41 jhermann
363 # Proper fix for failing test
364 #
365 # Revision 1.6 2002/01/23 05:47:57 richard
366 # more HTML template cleanup and unit tests
367 #
368 # Revision 1.5 2002/01/23 05:10:28 richard
369 # More HTML template cleanup and unit tests.
370 # - download() now implemented correctly, replacing link(is_download=1) [fixed in the
371 # templates, but link(is_download=1) will still work for existing templates]
372 #
373 # Revision 1.4 2002/01/22 22:46:22 richard
374 # more htmltemplate cleanups and unit tests
375 #
376 # Revision 1.3 2002/01/22 06:35:40 richard
377 # more htmltemplate tests and cleanup
378 #
379 # Revision 1.2 2002/01/22 00:12:07 richard
380 # Wrote more unit tests for htmltemplate, and while I was at it, I polished
381 # off the implementation of some of the functions so they behave sanely.
382 #
383 # Revision 1.1 2002/01/21 11:05:48 richard
384 # New tests for htmltemplate (well, it's a beginning)
385 #
386 #
387 #
388 # vim: set filetype=python ts=4 sw=4 et si