1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
from io import StringIO
from babel.numbers import format_decimal
from agate import Table
from agate.data_types import Number, Text
from agate.exceptions import DataTypeError
from agate.testcase import AgateTestCase
class TestPrintBars(AgateTestCase):
def setUp(self):
self.rows = (
('1.7', 2000, 'a'),
('11.18', None, None),
('0', 1, 'c')
)
self.number_type = Number()
self.international_number_type = Number(locale='de_DE.UTF-8')
self.text_type = Text()
self.column_names = ['one', 'two', 'three']
self.column_types = [
self.number_type,
self.international_number_type,
self.text_type
]
def test_print_bars(self):
table = Table(self.rows, self.column_names, self.column_types)
output = StringIO()
table.print_bars('three', 'one', output=output)
output.getvalue().split('\n')
def test_print_bars_width(self):
table = Table(self.rows, self.column_names, self.column_types)
output = StringIO()
table.print_bars('three', 'one', width=40, output=output)
lines = output.getvalue().split('\n')
self.assertEqual(max([len(line) for line in lines]), 40)
def test_print_bars_width_overlap(self):
table = Table(self.rows, self.column_names, self.column_types)
output = StringIO()
table.print_bars('three', 'one', width=20, output=output)
lines = output.getvalue().split('\n')
self.assertEqual(max([len(line) for line in lines]), 20)
def test_print_bars_domain(self):
table = Table(self.rows, self.column_names, self.column_types)
table.print_bars('three', 'one', domain=(0, 300))
def test_print_bars_domain_invalid(self):
table = Table(self.rows, self.column_names, self.column_types)
with self.assertRaises(ValueError):
table.print_bars('three', 'one', domain=(5, 0))
def test_print_bars_negative(self):
rows = (
('-1.7', 2, 'a'),
('-11.18', None, None),
('0', 1, 'c')
)
table = Table(rows, self.column_names, self.column_types)
table.print_bars('three', 'one')
def test_print_bars_mixed_signs(self):
rows = (
('-1.7', 2, 'a'),
('11.18', None, None),
('0', 1, 'c')
)
table = Table(rows, self.column_names, self.column_types)
table.print_bars('three', 'one')
def test_print_bars_invalid_values(self):
table = Table(self.rows, self.column_names, self.column_types)
with self.assertRaises(DataTypeError):
table.print_bars('one', 'three')
def test_print_bars_with_nulls(self):
table = Table(self.rows, self.column_names, self.column_types)
output = StringIO()
table.print_bars('three', 'two', width=20, printable=True,
output=output)
self.assertEqual(output.getvalue(), "three two\n"
"a " + format_decimal(2000, format='#,##0') + " |:::::::\n"
"None - | \n"
"c 1 | \n"
" +------+\n"
" 0 " + format_decimal(2000, format='#,##0') + "\n")
|