Code

Proper fix for failing test
[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.7 2002-01-23 20:09:41 jhermann 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 == 'interval':
29             return date.Interval('-3d')
30         elif attribute == 'link':
31             return '1'
32         elif attribute == 'multilink':
33             return ['1', '2']
34         elif attribute == 'password':
35             return password.Password('sekrit')
36         elif attribute == 'key':
37             return 'the key'+nodeid
38         elif attribute == 'html':
39             return '<html>hello, I am HTML</html>'
40     def list(self):
41         return ['1', '2']
42     def getprops(self):
43         return {'string': String(), 'date': Date(), 'interval': Interval(),
44             'link': Link('other'), 'multilink': Multilink('other'),
45             'password': Password(), 'html': String(), 'key': String(),
46             'novalue': String(), 'filename': String()}
47     def labelprop(self):
48         return 'key'
50 class Database:
51     classes = {'other': Class()}
52     def getclass(self, name):
53         return Class()
54     def __getattr(self, name):
55         return Class()
57 class Client:
58     write = None
60 class NodeCase(unittest.TestCase):
61     def setUp(self):
62         ''' Set up the harness for calling the individual tests
63         '''
64         self.tf = tf = TemplateFunctions()
65         tf.nodeid = '1'
66         tf.cl = Class()
67         tf.classname = 'test_class'
68         tf.properties = tf.cl.getprops()
69         tf.db = Database()
71 #    def do_plain(self, property, escape=0):
72     def testPlain_string(self):
73         s = 'Node 1: I am a string'
74         self.assertEqual(self.tf.do_plain('string'), s)
76     def testPlain_password(self):
77         self.assertEqual(self.tf.do_plain('password'), '*encrypted*')
79     def testPlain_html(self):
80         s = '<html>hello, I am HTML</html>'
81         self.assertEqual(self.tf.do_plain('html', escape=0), s)
82         s = cgi.escape(s)
83         self.assertEqual(self.tf.do_plain('html', escape=1), s)
85     def testPlain_date(self):
86         self.assertEqual(self.tf.do_plain('date'), '2000-01-01.00:00:00')
88     def testPlain_interval(self):
89         self.assertEqual(self.tf.do_plain('interval'), '- 3d')
91     def testPlain_link(self):
92         self.assertEqual(self.tf.do_plain('link'), 'the key1')
94     def testPlain_multilink(self):
95         self.assertEqual(self.tf.do_plain('multilink'), '1, 2')
98 #    def do_field(self, property, size=None, showid=0):
99     def testField_string(self):
100         self.assertEqual(self.tf.do_field('string'),
101             '<input name="string" value="Node 1: I am a string" size="30">')
102         self.assertEqual(self.tf.do_field('string', size=10),
103             '<input name="string" value="Node 1: I am a string" size="10">')
105     def testField_password(self):
106         self.assertEqual(self.tf.do_field('password'),
107             '<input type="password" name="password" size="30">')
108         self.assertEqual(self.tf.do_field('password', size=10),
109             '<input type="password" name="password" size="10">')
111     def testField_html(self):
112         self.assertEqual(self.tf.do_field('html'), '<input name="html" '
113             'value="&lt;html&gt;hello, I am HTML&lt;/html&gt;" size="30">')
114         self.assertEqual(self.tf.do_field('html', size=10),
115             '<input name="html" value="&lt;html&gt;hello, I am '
116             'HTML&lt;/html&gt;" size="10">')
118     def testField_date(self):
119         self.assertEqual(self.tf.do_field('date'),
120             '<input name="date" value="2000-01-01.00:00:00" size="30">')
121         self.assertEqual(self.tf.do_field('date', size=10),
122             '<input name="date" value="2000-01-01.00:00:00" size="10">')
124     def testField_interval(self):
125         self.assertEqual(self.tf.do_field('interval'),
126             '<input name="interval" value="- 3d" size="30">')
127         self.assertEqual(self.tf.do_field('interval', size=10),
128             '<input name="interval" value="- 3d" size="10">')
130     def testField_link(self):
131         self.assertEqual(self.tf.do_field('link'), '''<select name="link">
132 <option value="-1">- no selection -</option>
133 <option selected value="1">the key1</option>
134 <option value="2">the key2</option>
135 </select>''')
137     def testField_multilink(self):
138         self.assertEqual(self.tf.do_field('multilink'),
139             '<input name="multilink" size="30" value="the key1,the key2">')
140         self.assertEqual(self.tf.do_field('multilink', size=10),
141             '<input name="multilink" size="10" value="the key1,the key2">')
143 #    def do_menu(self, property, size=None, height=None, showid=0):
144     def testMenu_nonlinks(self):
145         s = _('[Menu: not a link]')
146         self.assertEqual(self.tf.do_menu('string'), s)
147         self.assertEqual(self.tf.do_menu('date'), s)
148         self.assertEqual(self.tf.do_menu('interval'), s)
149         self.assertEqual(self.tf.do_menu('password'), s)
151     def testMenu_link(self):
152         self.assertEqual(self.tf.do_menu('link'), '''<select name="link">
153 <option value="-1">- no selection -</option>
154 <option selected value="1">the key1</option>
155 <option value="2">the key2</option>
156 </select>''')
157         self.assertEqual(self.tf.do_menu('link', size=6),
158             '''<select name="link">
159 <option value="-1">- no selection -</option>
160 <option selected value="1">the...</option>
161 <option value="2">the...</option>
162 </select>''')
163         self.assertEqual(self.tf.do_menu('link', showid=1),
164             '''<select name="link">
165 <option value="-1">- no selection -</option>
166 <option selected value="1">other1: the key1</option>
167 <option value="2">other2: the key2</option>
168 </select>''')
170     def testMenu_multilink(self):
171         self.assertEqual(self.tf.do_menu('multilink', height=10),
172             '''<select multiple name="multilink" size="10">
173 <option selected value="1">the key1</option>
174 <option selected value="2">the key2</option>
175 </select>''')
176         self.assertEqual(self.tf.do_menu('multilink', size=6, height=10),
177             '''<select multiple name="multilink" size="10">
178 <option selected value="1">the...</option>
179 <option selected value="2">the...</option>
180 </select>''')
181         self.assertEqual(self.tf.do_menu('multilink', showid=1),
182             '''<select multiple name="multilink" size="2">
183 <option selected value="1">other1: the key1</option>
184 <option selected value="2">other2: the key2</option>
185 </select>''')
187 #    def do_link(self, property=None, is_download=0):
188     def testLink_novalue(self):
189         self.assertEqual(self.tf.do_link('novalue'),
190             _('[no %(propname)s]')%{'propname':'novalue'.capitalize()})
192     def testLink_string(self):
193         self.assertEqual(self.tf.do_link('string'),
194             '<a href="test_class1">Node 1: I am a string</a>')
196     def testLink_file(self):
197         self.assertEqual(self.tf.do_link('filename', is_download=1),
198             '<a href="test_class1/file.foo">file.foo</a>')
200     def testLink_date(self):
201         self.assertEqual(self.tf.do_link('date'),
202             '<a href="test_class1">2000-01-01.00:00:00</a>')
204     def testLink_interval(self):
205         self.assertEqual(self.tf.do_link('interval'),
206             '<a href="test_class1">- 3d</a>')
208     def testLink_link(self):
209         self.assertEqual(self.tf.do_link('link'),
210             '<a href="other1">the key1</a>')
212     def testLink_multilink(self):
213         self.assertEqual(self.tf.do_link('multilink'),
214             '<a href="other1">the key1</a>, <a href="other2">the key2</a>')
216 #    def do_count(self, property, **args):
217     def testCount_nonlinks(self):
218         s = _('[Count: not a Multilink]')
219         self.assertEqual(self.tf.do_count('string'), s)
220         self.assertEqual(self.tf.do_count('date'), s)
221         self.assertEqual(self.tf.do_count('interval'), s)
222         self.assertEqual(self.tf.do_count('password'), s)
223         self.assertEqual(self.tf.do_count('link'), s)
225     def testCount_multilink(self):
226         self.assertEqual(self.tf.do_count('multilink'), '2')
228 #    def do_reldate(self, property, pretty=0):
229     def testReldate_nondate(self):
230         s = _('[Reldate: not a Date]')
231         self.assertEqual(self.tf.do_reldate('string'), s)
232         self.assertEqual(self.tf.do_reldate('interval'), s)
233         self.assertEqual(self.tf.do_reldate('password'), s)
234         self.assertEqual(self.tf.do_reldate('link'), s)
235         self.assertEqual(self.tf.do_reldate('multilink'), s)
237     def testReldate_date(self):
238         self.assertEqual(self.tf.do_reldate('date'), '- 2y 1m')
239         self.assertEqual(self.tf.do_reldate('date', pretty=1),
240             ' 1 %s 2000' % time.strftime('%B', time.localtime(0)))
242 #    def do_download(self, property):
243     def testDownload_novalue(self):
244         self.assertEqual(self.tf.do_download('novalue'),
245             _('[no %(propname)s]')%{'propname':'novalue'.capitalize()})
247     def testDownload_string(self):
248         self.assertEqual(self.tf.do_download('string'),
249             '<a href="test_class1/Node 1: I am a string">Node 1: '
250             'I am a string</a>')
252     def testDownload_file(self):
253         self.assertEqual(self.tf.do_download('filename', is_download=1),
254             '<a href="test_class1/file.foo">file.foo</a>')
256     def testDownload_date(self):
257         self.assertEqual(self.tf.do_download('date'),
258             '<a href="test_class1/2000-01-01.00:00:00">2000-01-01.00:00:00</a>')
260     def testDownload_interval(self):
261         self.assertEqual(self.tf.do_download('interval'),
262             '<a href="test_class1/- 3d">- 3d</a>')
264     def testDownload_link(self):
265         self.assertEqual(self.tf.do_download('link'),
266             '<a href="other1/the key1">the key1</a>')
268     def testDownload_multilink(self):
269         self.assertEqual(self.tf.do_download('multilink'),
270             '<a href="other1/the key1">the key1</a>, '
271             '<a href="other2/the key2">the key2</a>')
273 #    def do_checklist(self, property, reverse=0):
274     def testChecklink_nonlinks(self):
275         s = _('[Checklist: not a link]')
276         self.assertEqual(self.tf.do_checklist('string'), s)
277         self.assertEqual(self.tf.do_checklist('date'), s)
278         self.assertEqual(self.tf.do_checklist('interval'), s)
279         self.assertEqual(self.tf.do_checklist('password'), s)
281     def testChecklink_link(self):
282         self.assertEqual(self.tf.do_checklist('link'),
283             '''the key1:<input type="checkbox" checked name="link" value="the key1">
284 the key2:<input type="checkbox"  name="link" value="the key2">
285 [unselected]:<input type="checkbox"  name="link" value="-1">''')
287     def testChecklink_multilink(self):
288         self.assertEqual(self.tf.do_checklist('multilink'),
289             '''the key1:<input type="checkbox" checked name="multilink" value="the key1">
290 the key2:<input type="checkbox" checked name="multilink" value="the key2">''')
292 #    def do_note(self, rows=5, cols=80):
293     def testNote(self):
294         self.assertEqual(self.tf.do_note(), '<textarea name="__note" '
295             'wrap="hard" rows=5 cols=80></textarea>')
297 #    def do_list(self, property, reverse=0):
298     def testList_nonlinks(self):
299         s = _('[List: not a Multilink]')
300         self.assertEqual(self.tf.do_list('string'), s)
301         self.assertEqual(self.tf.do_list('date'), s)
302         self.assertEqual(self.tf.do_list('interval'), s)
303         self.assertEqual(self.tf.do_list('password'), s)
304         self.assertEqual(self.tf.do_list('link'), s)
306     def testList_multilink(self):
307         # TODO: test this (needs to have lots and lots of support!
308         #self.assertEqual(self.tf.do_list('multilink'),'')
309         pass
311 def suite():
312    return unittest.makeSuite(NodeCase, 'test')
316 # $Log: not supported by cvs2svn $
317 # Revision 1.6  2002/01/23 05:47:57  richard
318 # more HTML template cleanup and unit tests
320 # Revision 1.5  2002/01/23 05:10:28  richard
321 # More HTML template cleanup and unit tests.
322 #  - download() now implemented correctly, replacing link(is_download=1) [fixed in the
323 #    templates, but link(is_download=1) will still work for existing templates]
325 # Revision 1.4  2002/01/22 22:46:22  richard
326 # more htmltemplate cleanups and unit tests
328 # Revision 1.3  2002/01/22 06:35:40  richard
329 # more htmltemplate tests and cleanup
331 # Revision 1.2  2002/01/22 00:12:07  richard
332 # Wrote more unit tests for htmltemplate, and while I was at it, I polished
333 # off the implementation of some of the functions so they behave sanely.
335 # Revision 1.1  2002/01/21 11:05:48  richard
336 # New tests for htmltemplate (well, it's a beginning)
340 # vim: set filetype=python ts=4 sw=4 et si