Code

Unit tests and a few fixes.
[roundup.git] / test / test_htmltemplate.py
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.17 2002-07-18 23:07:07 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, \
19     Multilink, Boolean, Number
21 class Class:
22     def get(self, nodeid, attribute, default=None):
23         if attribute == 'string':
24             return 'Node %s: I am a string'%nodeid
25         elif attribute == 'filename':
26             return 'file.foo'
27         elif attribute == 'date':
28             return date.Date('2000-01-01')
29         elif attribute == 'boolean':
30             return 0
31         elif attribute == 'number':
32             return 1234
33         elif attribute == 'reldate':
34             return date.Date() + date.Interval('- 2y 1m')
35         elif attribute == 'interval':
36             return date.Interval('-3d')
37         elif attribute == 'link':
38             return '1'
39         elif attribute == 'multilink':
40             return ['1', '2']
41         elif attribute == 'password':
42             return password.Password('sekrit')
43         elif attribute == 'key':
44             return 'the key'+nodeid
45         elif attribute == 'html':
46             return '<html>hello, I am HTML</html>'
47         elif attribute == 'multiline':
48             return 'hello\nworld'
49         elif attribute == 'email':
50             return 'test@foo.domain.example'
51     def list(self):
52         return ['1', '2']
53     def filter(self, search_matches, filterspec, sort, group):
54         return ['1', '2']
55     def getprops(self):
56         return {'string': String(), 'date': Date(), 'interval': Interval(),
57             'link': Link('other'), 'multilink': Multilink('other'),
58             'password': Password(), 'html': String(), 'key': String(),
59             'novalue': String(), 'filename': String(), 'multiline': String(),
60             'reldate': Date(), 'email': String(), 'boolean': Boolean(),
61             'number': Number()}
62     def labelprop(self, default_to_id=0):
63         return 'key'
65 class Database:
66     classes = {'other': Class()}
67     def getclass(self, name):
68         return Class()
69     def __getattr(self, name):
70         return Class()
72 class Client:
73     write = None
75 class NodeCase(unittest.TestCase):
76     def setUp(self):
77         ''' Set up the harness for calling the individual tests
78         '''
79         self.tf = tf = TemplateFunctions()
80         tf.nodeid = '1'
81         tf.cl = Class()
82         tf.classname = 'test_class'
83         tf.properties = tf.cl.getprops()
84         tf.db = Database()
86 #    def do_plain(self, property, escape=0):
87     def testPlain_string(self):
88         s = 'Node 1: I am a string'
89         self.assertEqual(self.tf.do_plain('string'), s)
91     def testPlain_password(self):
92         self.assertEqual(self.tf.do_plain('password'), '*encrypted*')
94     def testPlain_html(self):
95         s = '<html>hello, I am HTML</html>'
96         self.assertEqual(self.tf.do_plain('html', escape=0), s)
97         s = cgi.escape(s)
98         self.assertEqual(self.tf.do_plain('html', escape=1), s)
100     def testPlain_date(self):
101         self.assertEqual(self.tf.do_plain('date'), '2000-01-01.00:00:00')
103     def testPlain_interval(self):
104         self.assertEqual(self.tf.do_plain('interval'), '- 3d')
106     def testPlain_link(self):
107         self.assertEqual(self.tf.do_plain('link'), 'the key1')
109     def testPlain_multilink(self):
110         self.assertEqual(self.tf.do_plain('multilink'), 'the key1, the key2')
112     def testPlain_boolean(self):
113         self.assertEqual(self.tf.do_plain('boolean'), 'No')
115     def testPlain_number(self):
116         self.assertEqual(self.tf.do_plain('number'), '1234')
118 #    def do_field(self, property, size=None, showid=0):
119     def testField_string(self):
120         self.assertEqual(self.tf.do_field('string'),
121             '<input name="string" value="Node 1: I am a string" size="30">')
122         self.assertEqual(self.tf.do_field('string', size=10),
123             '<input name="string" value="Node 1: I am a string" size="10">')
125     def testField_password(self):
126         self.assertEqual(self.tf.do_field('password'),
127             '<input type="password" name="password" size="30">')
128         self.assertEqual(self.tf.do_field('password', size=10),
129             '<input type="password" name="password" size="10">')
131     def testField_html(self):
132         self.assertEqual(self.tf.do_field('html'), '<input name="html" '
133             'value="&lt;html&gt;hello, I am HTML&lt;/html&gt;" size="30">')
134         self.assertEqual(self.tf.do_field('html', size=10),
135             '<input name="html" value="&lt;html&gt;hello, I am '
136             'HTML&lt;/html&gt;" size="10">')
138     def testField_date(self):
139         self.assertEqual(self.tf.do_field('date'),
140             '<input name="date" value="2000-01-01.00:00:00" size="30">')
141         self.assertEqual(self.tf.do_field('date', size=10),
142             '<input name="date" value="2000-01-01.00:00:00" size="10">')
144     def testField_interval(self):
145         self.assertEqual(self.tf.do_field('interval'),
146             '<input name="interval" value="- 3d" size="30">')
147         self.assertEqual(self.tf.do_field('interval', size=10),
148             '<input name="interval" value="- 3d" size="10">')
150     def testField_link(self):
151         self.assertEqual(self.tf.do_field('link'), '''<select name="link">
152 <option value="-1">- no selection -</option>
153 <option selected value="1">the key1</option>
154 <option value="2">the key2</option>
155 </select>''')
157     def testField_multilink(self):
158         self.assertEqual(self.tf.do_field('multilink'),
159             '<input name="multilink" size="30" value="the key1,the key2">')
160         self.assertEqual(self.tf.do_field('multilink', size=10),
161             '<input name="multilink" size="10" value="the key1,the key2">')
163     def testField_boolean(self):
164         self.assertEqual(self.tf.do_field('boolean'),
165             '<input type="checkbox" name="boolean" >')
167     def testField_number(self):
168         self.assertEqual(self.tf.do_field('number'),
169             '<input name="number" value="1234" size="30">')
170         self.assertEqual(self.tf.do_field('number', size=10),
171             '<input name="number" value="1234" size="10">')
173 #    def do_multiline(self, property, rows=5, cols=40)
174     def testMultiline_string(self):
175         self.assertEqual(self.tf.do_multiline('multiline'),
176             '<textarea name="multiline" rows="5" cols="40">'
177             'hello\nworld</textarea>')
178         self.assertEqual(self.tf.do_multiline('multiline', rows=10),
179             '<textarea name="multiline" rows="10" cols="40">'
180             'hello\nworld</textarea>')
181         self.assertEqual(self.tf.do_multiline('multiline', cols=10),
182             '<textarea name="multiline" rows="5" cols="10">'
183             'hello\nworld</textarea>')
185     def testMultiline_nonstring(self):
186         s = _('[Multiline: not a string]')
187         self.assertEqual(self.tf.do_multiline('date'), s)
188         self.assertEqual(self.tf.do_multiline('interval'), s)
189         self.assertEqual(self.tf.do_multiline('password'), s)
190         self.assertEqual(self.tf.do_multiline('link'), s)
191         self.assertEqual(self.tf.do_multiline('multilink'), s)
192         self.assertEqual(self.tf.do_multiline('boolean'), s)
193         self.assertEqual(self.tf.do_multiline('number'), s)
195 #    def do_menu(self, property, size=None, height=None, showid=0):
196     def testMenu_nonlinks(self):
197         s = _('[Menu: not a link]')
198         self.assertEqual(self.tf.do_menu('string'), s)
199         self.assertEqual(self.tf.do_menu('date'), s)
200         self.assertEqual(self.tf.do_menu('interval'), s)
201         self.assertEqual(self.tf.do_menu('password'), s)
202         self.assertEqual(self.tf.do_menu('boolean'), s)
203         self.assertEqual(self.tf.do_menu('number'), s)
205     def testMenu_link(self):
206         self.assertEqual(self.tf.do_menu('link'), '''<select name="link">
207 <option value="-1">- no selection -</option>
208 <option selected value="1">the key1</option>
209 <option value="2">the key2</option>
210 </select>''')
211         self.assertEqual(self.tf.do_menu('link', size=6),
212             '''<select name="link">
213 <option value="-1">- no selection -</option>
214 <option selected value="1">the...</option>
215 <option value="2">the...</option>
216 </select>''')
217         self.assertEqual(self.tf.do_menu('link', showid=1),
218             '''<select name="link">
219 <option value="-1">- no selection -</option>
220 <option selected value="1">other1: the key1</option>
221 <option value="2">other2: the key2</option>
222 </select>''')
224     def testMenu_multilink(self):
225         self.assertEqual(self.tf.do_menu('multilink', height=10),
226             '''<select multiple name="multilink" size="10">
227 <option selected value="1">the key1</option>
228 <option selected value="2">the key2</option>
229 </select>''')
230         self.assertEqual(self.tf.do_menu('multilink', size=6, height=10),
231             '''<select multiple name="multilink" size="10">
232 <option selected value="1">the...</option>
233 <option selected value="2">the...</option>
234 </select>''')
235         self.assertEqual(self.tf.do_menu('multilink', showid=1),
236             '''<select multiple name="multilink" size="2">
237 <option selected value="1">other1: the key1</option>
238 <option selected value="2">other2: the key2</option>
239 </select>''')
241 #    def do_link(self, property=None, is_download=0):
242     def testLink_novalue(self):
243         self.assertEqual(self.tf.do_link('novalue'),
244             _('[no %(propname)s]')%{'propname':'novalue'.capitalize()})
246     def testLink_string(self):
247         self.assertEqual(self.tf.do_link('string'),
248             '<a href="test_class1">Node 1: I am a string</a>')
250     def testLink_file(self):
251         self.assertEqual(self.tf.do_link('filename', is_download=1),
252             '<a href="test_class1/file.foo">file.foo</a>')
254     def testLink_date(self):
255         self.assertEqual(self.tf.do_link('date'),
256             '<a href="test_class1">2000-01-01.00:00:00</a>')
258     def testLink_interval(self):
259         self.assertEqual(self.tf.do_link('interval'),
260             '<a href="test_class1">- 3d</a>')
262     def testLink_link(self):
263         self.assertEqual(self.tf.do_link('link'),
264             '<a href="other1">the key1</a>')
266     def testLink_link_id(self):
267         self.assertEqual(self.tf.do_link('link', showid=1),
268             '<a href="other1" title="the key1">1</a>')
270     def testLink_multilink(self):
271         self.assertEqual(self.tf.do_link('multilink'),
272             '<a href="other1">the key1</a>, <a href="other2">the key2</a>')
274     def testLink_multilink_id(self):
275         self.assertEqual(self.tf.do_link('multilink', showid=1),
276             '<a href="other1" title="the key1">1</a>, <a href="other2" title="the key2">2</a>')
278     def testLink_boolean(self):
279         self.assertEqual(self.tf.do_link('boolean'),
280             '<a href="test_class1">No</a>')
282     def testLink_number(self):
283         self.assertEqual(self.tf.do_link('number'),
284             '<a href="test_class1">1234</a>')
286 #    def do_count(self, property, **args):
287     def testCount_nonlinks(self):
288         s = _('[Count: not a Multilink]')
289         self.assertEqual(self.tf.do_count('string'), s)
290         self.assertEqual(self.tf.do_count('date'), s)
291         self.assertEqual(self.tf.do_count('interval'), s)
292         self.assertEqual(self.tf.do_count('password'), s)
293         self.assertEqual(self.tf.do_count('link'), s)
294         self.assertEqual(self.tf.do_count('boolean'), s)
295         self.assertEqual(self.tf.do_count('number'), s)
297     def testCount_multilink(self):
298         self.assertEqual(self.tf.do_count('multilink'), '2')
300 #    def do_reldate(self, property, pretty=0):
301     def testReldate_nondate(self):
302         s = _('[Reldate: not a Date]')
303         self.assertEqual(self.tf.do_reldate('string'), s)
304         self.assertEqual(self.tf.do_reldate('interval'), s)
305         self.assertEqual(self.tf.do_reldate('password'), s)
306         self.assertEqual(self.tf.do_reldate('link'), s)
307         self.assertEqual(self.tf.do_reldate('multilink'), s)
308         self.assertEqual(self.tf.do_reldate('boolean'), s)
309         self.assertEqual(self.tf.do_reldate('number'), s)
311     def testReldate_date(self):
312         self.assertEqual(self.tf.do_reldate('reldate'), '- 2y 1m')
313         interval = date.Interval('- 2y 1m')
314         self.assertEqual(self.tf.do_reldate('reldate', pretty=1),
315             interval.pretty())
317 #    def do_download(self, property):
318     def testDownload_novalue(self):
319         self.assertEqual(self.tf.do_download('novalue'),
320             _('[no %(propname)s]')%{'propname':'novalue'.capitalize()})
322     def testDownload_string(self):
323         self.assertEqual(self.tf.do_download('string'),
324             '<a href="test_class1/Node 1: I am a string">Node 1: '
325             'I am a string</a>')
327     def testDownload_file(self):
328         self.assertEqual(self.tf.do_download('filename', is_download=1),
329             '<a href="test_class1/file.foo">file.foo</a>')
331     def testDownload_date(self):
332         self.assertEqual(self.tf.do_download('date'),
333             '<a href="test_class1/2000-01-01.00:00:00">2000-01-01.00:00:00</a>')
335     def testDownload_interval(self):
336         self.assertEqual(self.tf.do_download('interval'),
337             '<a href="test_class1/- 3d">- 3d</a>')
339     def testDownload_link(self):
340         self.assertEqual(self.tf.do_download('link'),
341             '<a href="other1/the key1">the key1</a>')
343     def testDownload_multilink(self):
344         self.assertEqual(self.tf.do_download('multilink'),
345             '<a href="other1/the key1">the key1</a>, '
346             '<a href="other2/the key2">the key2</a>')
348     def testDownload_boolean(self):
349         self.assertEqual(self.tf.do_download('boolean'),
350             '<a href="test_class1/No">No</a>')
352     def testDownload_number(self):
353         self.assertEqual(self.tf.do_download('number'),
354             '<a href="test_class1/1234">1234</a>')
356 #    def do_checklist(self, property, reverse=0):
357     def testChecklist_nonlinks(self):
358         s = _('[Checklist: not a link]')
359         self.assertEqual(self.tf.do_checklist('string'), s)
360         self.assertEqual(self.tf.do_checklist('date'), s)
361         self.assertEqual(self.tf.do_checklist('interval'), s)
362         self.assertEqual(self.tf.do_checklist('password'), s)
363         self.assertEqual(self.tf.do_checklist('boolean'), s)
364         self.assertEqual(self.tf.do_checklist('number'), s)
366     def testChecklstk_link(self):
367         self.assertEqual(self.tf.do_checklist('link'),
368             '''the key1:<input type="checkbox" checked name="link" value="the key1">
369 the key2:<input type="checkbox"  name="link" value="the key2">
370 [unselected]:<input type="checkbox"  name="link" value="-1">''')
372     def testChecklink_multilink(self):
373         self.assertEqual(self.tf.do_checklist('multilink'),
374             '''the key1:<input type="checkbox" checked name="multilink" value="the key1">
375 the key2:<input type="checkbox" checked name="multilink" value="the key2">''')
377 #    def do_note(self, rows=5, cols=80):
378     def testNote(self):
379         self.assertEqual(self.tf.do_note(), '<textarea name="__note" '
380             'wrap="hard" rows=5 cols=80></textarea>')
382 #    def do_list(self, property, reverse=0):
383     def testList_nonlinks(self):
384         s = _('[List: not a Multilink]')
385         self.assertEqual(self.tf.do_list('string'), s)
386         self.assertEqual(self.tf.do_list('date'), s)
387         self.assertEqual(self.tf.do_list('interval'), s)
388         self.assertEqual(self.tf.do_list('password'), s)
389         self.assertEqual(self.tf.do_list('link'), s)
390         self.assertEqual(self.tf.do_list('boolean'), s)
391         self.assertEqual(self.tf.do_list('number'), s)
393     def testList_multilink(self):
394         # TODO: test this (needs to have lots and lots of support!
395         #self.assertEqual(self.tf.do_list('multilink'),'')
396         pass
398     def testClasshelp(self):
399         self.assertEqual(self.tf.do_classhelp('theclass', 'prop1,prop2'),
400             '<a href="javascript:help_window(\'classhelp?classname=theclass'
401             '&properties=prop1,prop2\', \'400\', \'400\')"><b>(?)</b></a>')
403 #    def do_multiline(self, property, rows=5, cols=40)
404     def testEmail_string(self):
405         self.assertEqual(self.tf.do_email('email'), 'test at foo domain example')
407     def testEmail_nonstring(self):
408         s = _('[Email: not a string]')
409         self.assertEqual(self.tf.do_email('date'), s)
410         self.assertEqual(self.tf.do_email('interval'), s)
411         self.assertEqual(self.tf.do_email('password'), s)
412         self.assertEqual(self.tf.do_email('link'), s)
413         self.assertEqual(self.tf.do_email('multilink'), s)
414         self.assertEqual(self.tf.do_email('boolean'), s)
415         self.assertEqual(self.tf.do_email('number'), s)
417 def suite():
418    return unittest.makeSuite(NodeCase, 'test')
422 # $Log: not supported by cvs2svn $
423 # Revision 1.16  2002/07/09 05:20:09  richard
424 #  . added email display function - mangles email addrs so they're not so easily
425 #    scraped from the web
427 # Revision 1.15  2002/07/08 06:39:00  richard
428 # Fixed unit test support class so the tests ran again.
430 # Revision 1.14  2002/05/15 06:37:31  richard
431 # ehem and the unit test
433 # Revision 1.13  2002/04/03 05:54:31  richard
434 # Fixed serialisation problem by moving the serialisation step out of the
435 # hyperdb.Class (get, set) into the hyperdb.Database.
437 # Also fixed htmltemplate after the showid changes I made yesterday.
439 # Unit tests for all of the above written.
441 # Revision 1.12  2002/03/29 19:41:48  rochecompaan
442 #  . Fixed display of mutlilink properties when using the template
443 #    functions, menu and plain.
445 # Revision 1.11  2002/02/21 23:11:45  richard
446 #  . fixed some problems in date calculations (calendar.py doesn't handle over-
447 #    and under-flow). Also, hour/minute/second intervals may now be more than
448 #    99 each.
450 # Revision 1.10  2002/02/21 06:57:39  richard
451 #  . Added popup help for classes using the classhelp html template function.
452 #    - add <display call="classhelp('priority', 'id,name,description')">
453 #      to an item page, and it generates a link to a popup window which displays
454 #      the id, name and description for the priority class. The description
455 #      field won't exist in most installations, but it will be added to the
456 #      default templates.
458 # Revision 1.9  2002/02/15 07:08:45  richard
459 #  . Alternate email addresses are now available for users. See the MIGRATION
460 #    file for info on how to activate the feature.
462 # Revision 1.8  2002/02/06 03:47:16  richard
463 #  . #511586 ] unittest FAIL: testReldate_date
465 # Revision 1.7  2002/01/23 20:09:41  jhermann
466 # Proper fix for failing test
468 # Revision 1.6  2002/01/23 05:47:57  richard
469 # more HTML template cleanup and unit tests
471 # Revision 1.5  2002/01/23 05:10:28  richard
472 # More HTML template cleanup and unit tests.
473 #  - download() now implemented correctly, replacing link(is_download=1) [fixed in the
474 #    templates, but link(is_download=1) will still work for existing templates]
476 # Revision 1.4  2002/01/22 22:46:22  richard
477 # more htmltemplate cleanups and unit tests
479 # Revision 1.3  2002/01/22 06:35:40  richard
480 # more htmltemplate tests and cleanup
482 # Revision 1.2  2002/01/22 00:12:07  richard
483 # Wrote more unit tests for htmltemplate, and while I was at it, I polished
484 # off the implementation of some of the functions so they behave sanely.
486 # Revision 1.1  2002/01/21 11:05:48  richard
487 # New tests for htmltemplate (well, it's a beginning)
491 # vim: set filetype=python ts=4 sw=4 et si