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.14 2002-05-15 06:37:31 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'), 'the key1, the key2')
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_link_id(self):
238 self.assertEqual(self.tf.do_link('link', showid=1),
239 '<a href="other1" title="the key1">1</a>')
241 def testLink_multilink(self):
242 self.assertEqual(self.tf.do_link('multilink'),
243 '<a href="other1">the key1</a>, <a href="other2">the key2</a>')
245 def testLink_multilink_id(self):
246 self.assertEqual(self.tf.do_link('multilink', showid=1),
247 '<a href="other1" title="the key1">1</a>, <a href="other2" title="the key2">2</a>')
249 # def do_count(self, property, **args):
250 def testCount_nonlinks(self):
251 s = _('[Count: not a Multilink]')
252 self.assertEqual(self.tf.do_count('string'), s)
253 self.assertEqual(self.tf.do_count('date'), s)
254 self.assertEqual(self.tf.do_count('interval'), s)
255 self.assertEqual(self.tf.do_count('password'), s)
256 self.assertEqual(self.tf.do_count('link'), s)
258 def testCount_multilink(self):
259 self.assertEqual(self.tf.do_count('multilink'), '2')
261 # def do_reldate(self, property, pretty=0):
262 def testReldate_nondate(self):
263 s = _('[Reldate: not a Date]')
264 self.assertEqual(self.tf.do_reldate('string'), s)
265 self.assertEqual(self.tf.do_reldate('interval'), s)
266 self.assertEqual(self.tf.do_reldate('password'), s)
267 self.assertEqual(self.tf.do_reldate('link'), s)
268 self.assertEqual(self.tf.do_reldate('multilink'), s)
270 def testReldate_date(self):
271 self.assertEqual(self.tf.do_reldate('reldate'), '- 2y 1m')
272 interval = date.Interval('- 2y 1m')
273 self.assertEqual(self.tf.do_reldate('reldate', pretty=1),
274 interval.pretty())
276 # def do_download(self, property):
277 def testDownload_novalue(self):
278 self.assertEqual(self.tf.do_download('novalue'),
279 _('[no %(propname)s]')%{'propname':'novalue'.capitalize()})
281 def testDownload_string(self):
282 self.assertEqual(self.tf.do_download('string'),
283 '<a href="test_class1/Node 1: I am a string">Node 1: '
284 'I am a string</a>')
286 def testDownload_file(self):
287 self.assertEqual(self.tf.do_download('filename', is_download=1),
288 '<a href="test_class1/file.foo">file.foo</a>')
290 def testDownload_date(self):
291 self.assertEqual(self.tf.do_download('date'),
292 '<a href="test_class1/2000-01-01.00:00:00">2000-01-01.00:00:00</a>')
294 def testDownload_interval(self):
295 self.assertEqual(self.tf.do_download('interval'),
296 '<a href="test_class1/- 3d">- 3d</a>')
298 def testDownload_link(self):
299 self.assertEqual(self.tf.do_download('link'),
300 '<a href="other1/the key1">the key1</a>')
302 def testDownload_multilink(self):
303 self.assertEqual(self.tf.do_download('multilink'),
304 '<a href="other1/the key1">the key1</a>, '
305 '<a href="other2/the key2">the key2</a>')
307 # def do_checklist(self, property, reverse=0):
308 def testChecklink_nonlinks(self):
309 s = _('[Checklist: not a link]')
310 self.assertEqual(self.tf.do_checklist('string'), s)
311 self.assertEqual(self.tf.do_checklist('date'), s)
312 self.assertEqual(self.tf.do_checklist('interval'), s)
313 self.assertEqual(self.tf.do_checklist('password'), s)
315 def testChecklink_link(self):
316 self.assertEqual(self.tf.do_checklist('link'),
317 '''the key1:<input type="checkbox" checked name="link" value="the key1">
318 the key2:<input type="checkbox" name="link" value="the key2">
319 [unselected]:<input type="checkbox" name="link" value="-1">''')
321 def testChecklink_multilink(self):
322 self.assertEqual(self.tf.do_checklist('multilink'),
323 '''the key1:<input type="checkbox" checked name="multilink" value="the key1">
324 the key2:<input type="checkbox" checked name="multilink" value="the key2">''')
326 # def do_note(self, rows=5, cols=80):
327 def testNote(self):
328 self.assertEqual(self.tf.do_note(), '<textarea name="__note" '
329 'wrap="hard" rows=5 cols=80></textarea>')
331 # def do_list(self, property, reverse=0):
332 def testList_nonlinks(self):
333 s = _('[List: not a Multilink]')
334 self.assertEqual(self.tf.do_list('string'), s)
335 self.assertEqual(self.tf.do_list('date'), s)
336 self.assertEqual(self.tf.do_list('interval'), s)
337 self.assertEqual(self.tf.do_list('password'), s)
338 self.assertEqual(self.tf.do_list('link'), s)
340 def testList_multilink(self):
341 # TODO: test this (needs to have lots and lots of support!
342 #self.assertEqual(self.tf.do_list('multilink'),'')
343 pass
345 def testClasshelp(self):
346 self.assertEqual(self.tf.do_classhelp('theclass', 'prop1,prop2'),
347 '<a href="javascript:help_window(\'classhelp?classname=theclass'
348 '&properties=prop1,prop2\', \'400\', \'400\')"><b>(?)</b></a>')
350 def suite():
351 return unittest.makeSuite(NodeCase, 'test')
354 #
355 # $Log: not supported by cvs2svn $
356 # Revision 1.13 2002/04/03 05:54:31 richard
357 # Fixed serialisation problem by moving the serialisation step out of the
358 # hyperdb.Class (get, set) into the hyperdb.Database.
359 #
360 # Also fixed htmltemplate after the showid changes I made yesterday.
361 #
362 # Unit tests for all of the above written.
363 #
364 # Revision 1.12 2002/03/29 19:41:48 rochecompaan
365 # . Fixed display of mutlilink properties when using the template
366 # functions, menu and plain.
367 #
368 # Revision 1.11 2002/02/21 23:11:45 richard
369 # . fixed some problems in date calculations (calendar.py doesn't handle over-
370 # and under-flow). Also, hour/minute/second intervals may now be more than
371 # 99 each.
372 #
373 # Revision 1.10 2002/02/21 06:57:39 richard
374 # . Added popup help for classes using the classhelp html template function.
375 # - add <display call="classhelp('priority', 'id,name,description')">
376 # to an item page, and it generates a link to a popup window which displays
377 # the id, name and description for the priority class. The description
378 # field won't exist in most installations, but it will be added to the
379 # default templates.
380 #
381 # Revision 1.9 2002/02/15 07:08:45 richard
382 # . Alternate email addresses are now available for users. See the MIGRATION
383 # file for info on how to activate the feature.
384 #
385 # Revision 1.8 2002/02/06 03:47:16 richard
386 # . #511586 ] unittest FAIL: testReldate_date
387 #
388 # Revision 1.7 2002/01/23 20:09:41 jhermann
389 # Proper fix for failing test
390 #
391 # Revision 1.6 2002/01/23 05:47:57 richard
392 # more HTML template cleanup and unit tests
393 #
394 # Revision 1.5 2002/01/23 05:10:28 richard
395 # More HTML template cleanup and unit tests.
396 # - download() now implemented correctly, replacing link(is_download=1) [fixed in the
397 # templates, but link(is_download=1) will still work for existing templates]
398 #
399 # Revision 1.4 2002/01/22 22:46:22 richard
400 # more htmltemplate cleanups and unit tests
401 #
402 # Revision 1.3 2002/01/22 06:35:40 richard
403 # more htmltemplate tests and cleanup
404 #
405 # Revision 1.2 2002/01/22 00:12:07 richard
406 # Wrote more unit tests for htmltemplate, and while I was at it, I polished
407 # off the implementation of some of the functions so they behave sanely.
408 #
409 # Revision 1.1 2002/01/21 11:05:48 richard
410 # New tests for htmltemplate (well, it's a beginning)
411 #
412 #
413 #
414 # vim: set filetype=python ts=4 sw=4 et si