comparison test/test_templating.py @ 6832:234fefd7568a

issue2550559 - Pretty printing / formatting for Number types. add pretty(format='%0.3f') method to NumberHTMLProperty.
author John Rouillard <rouilj@ieee.org>
date Tue, 16 Aug 2022 22:39:04 -0400
parents 28b906a237d8
children 3085aac22f3a
comparison
equal deleted inserted replaced
6831:e0b29e3fe995 6832:234fefd7568a
302 # the call to anti_csrf_nonce and the time.time() call 302 # the call to anti_csrf_nonce and the time.time() call
303 # that assigns ts above. I declare that difference 303 # that assigns ts above. I declare that difference
304 # to be less than 1 second for this to pass. 304 # to be less than 1 second for this to pass.
305 self.assertEqual(True, 305 self.assertEqual(True,
306 greater_than <= now - timestamp < (greater_than + 1) ) 306 greater_than <= now - timestamp < (greater_than + 1) )
307
308 def test_number__int__(self):
309 # test with number
310 p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
311 2345678.2345678)
312 self.assertEqual(p.__int__(), 2345678)
313
314 property = MockNull(get_default_value = lambda: None)
315 p = NumberHTMLProperty(self.client, 'testnum', '1', property,
316 'test', None)
317 with self.assertRaises(TypeError) as e:
318 p.__int__()
319
320 def test_number__float__(self):
321 # test with number
322 p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
323 2345678.2345678)
324 self.assertEqual(p.__float__(), 2345678.2345678)
325
326 property = MockNull(get_default_value = lambda: None)
327 p = NumberHTMLProperty(self.client, 'testnum', '1', property,
328 'test', None)
329 with self.assertRaises(TypeError) as e:
330 p.__float__()
331
332 def test_number_field(self):
333 import sys
334
335 _py3 = sys.version_info[0] > 2
336
337 # python2 truncates while python3 rounds. Sigh.
338 if _py3:
339 expected_val = 2345678.2345678
340 else:
341 expected_val = 2345678.23457
342
343 # test with number
344 p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
345 2345678.2345678)
346 self.assertEqual(p.field(),
347 ('<input name="testnum1@test" size="30" type="text" '
348 'value="%s">')%expected_val)
349 self.assertEqual(p.field(size=10),
350 ('<input name="testnum1@test" size="10" type="text" '
351 'value="%s">')%expected_val)
352 self.assertEqual(p.field(size=10, dataprop="foo", dataprop2=5),
353 ('<input dataprop="foo" dataprop2="5" '
354 'name="testnum1@test" size="10" type="text" '
355 'value="%s">'%expected_val))
356
357 self.assertEqual(p.field(size=10, klass="class1",
358 **{ "class": "class2 class3",
359 "data-prop": "foo",
360 "data-prop2": 5}),
361 ('<input class="class2 class3" data-prop="foo" '
362 'data-prop2="5" klass="class1" '
363 'name="testnum1@test" size="10" type="text" '
364 'value="%s">')%expected_val)
365
366 # get plain representation if user can't edit
367 p.is_edit_ok = lambda: False
368 self.assertEqual(p.field(), p.plain())
369
370 # test with string which is wrong type
371 p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
372 "2345678.2345678")
373 self.assertEqual(p.field(),
374 ('<input name="testnum1@test" size="30" type="text" '
375 'value="2345678.2345678">'))
376
377 # test with None value, pretend property.__default_value = Null which
378 # is the default. It would be returned by get_default_value
379 # which I mock.
380 property = MockNull(get_default_value = lambda: None)
381 p = NumberHTMLProperty(self.client, 'testnum', '1', property,
382 'test', None)
383 self.assertEqual(p.field(),
384 ('<input name="testnum1@test" size="30" type="text" '
385 'value="">'))
386
387 def test_number_plain(self):
388 import sys
389
390 _py3 = sys.version_info[0] > 2
391
392 # python2 truncates while python3 rounds. Sigh.
393 if _py3:
394 expected_val = 2345678.2345678
395 else:
396 expected_val = 2345678.23457
397
398 p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
399 2345678.2345678)
400
401 self.assertEqual(p.plain(), "%s"%expected_val)
402
403 def test_number_pretty(self):
404 # test with number
405 p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
406 2345678.2345678)
407 self.assertEqual(p.pretty(), "2345678.235")
408
409 # test with string which is wrong type
410 p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
411 "2345678.2345678")
412 self.assertEqual(p.pretty(), "2345678.2345678")
413
414 # test with boolean
415 p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
416 True)
417 self.assertEqual(p.pretty(), "1.000")
418
419 # test with None value, pretend property.__default_value = Null which
420 # is the default. It would be returned by get_default_value
421 # which I mock.
422 property = MockNull(get_default_value = lambda: None)
423 p = NumberHTMLProperty(self.client, 'testnum', '1', property,
424 'test', None)
425 self.assertEqual(p.pretty(), '')
426
427 with self.assertRaises(ValueError) as e:
428 p.pretty('%0.3')
307 429
308 def test_string_url_quote(self): 430 def test_string_url_quote(self):
309 ''' test that urlquote quotes the string ''' 431 ''' test that urlquote quotes the string '''
310 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', 'test string< foo@bar') 432 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', 'test string< foo@bar')
311 self.assertEqual(p.url_quote(), 'test%20string%3C%20foo%40bar') 433 self.assertEqual(p.url_quote(), 'test%20string%3C%20foo%40bar')

Roundup Issue Tracker: http://roundup-tracker.org/