fix: add simple rendering logic for currency values and errors
This commit is contained in:
parent
78a9965c52
commit
9c1bfee97f
4 changed files with 111 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
|
||||
import datetime
|
||||
import decimal
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
|
@ -422,6 +423,31 @@ app_title = WuttaTest
|
|||
session = self.app.get_session(user)
|
||||
self.assertIs(session, mysession)
|
||||
|
||||
def test_render_currency(self):
|
||||
|
||||
# null
|
||||
self.assertEqual(self.app.render_currency(None), '')
|
||||
|
||||
# basic decimal example
|
||||
value = decimal.Decimal('42.00')
|
||||
self.assertEqual(self.app.render_currency(value), '$42.00')
|
||||
|
||||
# basic float example
|
||||
value = 42.00
|
||||
self.assertEqual(self.app.render_currency(value), '$42.00')
|
||||
|
||||
# decimal places will be rounded
|
||||
value = decimal.Decimal('42.12345')
|
||||
self.assertEqual(self.app.render_currency(value), '$42.12')
|
||||
|
||||
# but we can declare the scale
|
||||
value = decimal.Decimal('42.12345')
|
||||
self.assertEqual(self.app.render_currency(value, scale=4), '$42.1234')
|
||||
|
||||
# negative numbers get parens
|
||||
value = decimal.Decimal('-42.42')
|
||||
self.assertEqual(self.app.render_currency(value), '($42.42)')
|
||||
|
||||
def test_render_date(self):
|
||||
self.assertIsNone(self.app.render_date(None))
|
||||
|
||||
|
@ -434,6 +460,22 @@ app_title = WuttaTest
|
|||
dt = datetime.datetime(2024, 12, 11, 8, 30, tzinfo=datetime.timezone.utc)
|
||||
self.assertEqual(self.app.render_datetime(dt), '2024-12-11 08:30+0000')
|
||||
|
||||
def test_simple_error(self):
|
||||
|
||||
# with description
|
||||
try:
|
||||
raise RuntimeError("just testin")
|
||||
except Exception as error:
|
||||
result = self.app.render_error(error)
|
||||
self.assertEqual(result, "RuntimeError: just testin")
|
||||
|
||||
# without description
|
||||
try:
|
||||
raise RuntimeError
|
||||
except Exception as error:
|
||||
result = self.app.render_error(error)
|
||||
self.assertEqual(result, "RuntimeError")
|
||||
|
||||
def test_render_time_ago(self):
|
||||
with patch.object(mod, 'humanize') as humanize:
|
||||
humanize.naturaltime.return_value = 'now'
|
||||
|
|
|
@ -317,3 +317,20 @@ class TestResourcePath(TestCase):
|
|||
|
||||
# absolute path returned as-is
|
||||
self.assertEqual(mod.resource_path('/tmp/doesnotexist.txt'), '/tmp/doesnotexist.txt')
|
||||
|
||||
|
||||
class TestSimpleError(TestCase):
|
||||
|
||||
def test_with_description(self):
|
||||
try:
|
||||
raise RuntimeError("just testin")
|
||||
except Exception as error:
|
||||
result = mod.simple_error(error)
|
||||
self.assertEqual(result, "RuntimeError: just testin")
|
||||
|
||||
def test_without_description(self):
|
||||
try:
|
||||
raise RuntimeError
|
||||
except Exception as error:
|
||||
result = mod.simple_error(error)
|
||||
self.assertEqual(result, "RuntimeError")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue