File: test_print_table.py

package info (click to toggle)
python-agate 1.13.0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,008 kB
  • sloc: python: 8,578; makefile: 126
file content (133 lines) | stat: -rw-r--r-- 4,582 bytes parent folder | download | duplicates (2)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from io import StringIO

from babel.numbers import get_decimal_symbol

from agate import Table
from agate.data_types import Number, Text
from agate.testcase import AgateTestCase


class TestPrintTable(AgateTestCase):
    def setUp(self):
        self.rows = (
            ('1.7', 2000, 2000, 'a'),
            ('11.18', None, None, None),
            ('0', 1, 1, 'c')
        )

        self.number_type = Number()
        self.american_number_type = Number(locale='en_US')
        self.german_number_type = Number(locale='de_DE.UTF-8')
        self.text_type = Text()

        self.column_names = ['one', 'two', 'three', 'four']
        self.column_types = [
            self.number_type,
            self.american_number_type,
            self.german_number_type,
            self.text_type
        ]

    def test_print_table(self):
        table = Table(self.rows, self.column_names, self.column_types)

        output = StringIO()
        table.print_table(output=output)
        lines = output.getvalue().split('\n')

        self.assertEqual(len(lines), 6)
        self.assertEqual(len(lines[0]), 32)

    def test_print_table_max_rows(self):
        table = Table(self.rows, self.column_names, self.column_types)

        output = StringIO()
        table.print_table(max_rows=2, output=output)
        lines = output.getvalue().split('\n')

        self.assertEqual(len(lines), 6)
        self.assertEqual(len(lines[0]), 32)

    def test_print_table_max_columns(self):
        table = Table(self.rows, self.column_names, self.column_types)

        output = StringIO()
        table.print_table(max_columns=2, output=output)
        lines = output.getvalue().split('\n')

        self.assertEqual(len(lines), 6)
        self.assertEqual(len(lines[0]), 23)

    def test_print_table_max_precision(self):
        rows = (
            ('1.745', 1.745, 1.72),
            ('11.123456', 11.123456, 5.10),
            ('0', 0, 0.10)
        )

        column_names = ['text_number', 'real_long_number', 'real_short_number']
        column_types = [
            self.text_type,
            self.number_type,
            self.number_type
        ]
        table = Table(rows, column_names, column_types)

        output = StringIO()
        table.print_table(output=output, max_precision=2)
        lines = output.getvalue().split('\n')

        # Text shouldn't be affected
        self.assertIn(' 1.745 ', lines[2])
        self.assertIn(' 11.123456 ', lines[3])
        self.assertIn(' 0 ', lines[4])
        # Test real precision above max
        self.assertIn(' 1' + get_decimal_symbol() + '74… ', lines[2])
        self.assertIn(' 11' + get_decimal_symbol() + '12… ', lines[3])
        self.assertIn(' 0' + get_decimal_symbol() + '00… ', lines[4])
        # Test real precision below max
        self.assertIn(' 1' + get_decimal_symbol() + '72 ', lines[2])
        self.assertIn(' 5' + get_decimal_symbol() + '10 ', lines[3])
        self.assertIn(' 0' + get_decimal_symbol() + '10 ', lines[4])

    def test_print_table_max_column_width(self):
        rows = (
            ('1.7', 2, 2, 'this is long'),
            ('11.18', None, None, None),
            ('0', 1, 1, 'nope')
        )

        column_names = ['one', 'two', 'three', 'also, this is long']
        table = Table(rows, column_names, self.column_types)

        output = StringIO()
        table.print_table(output=output, max_column_width=7)
        lines = output.getvalue().split('\n')

        self.assertIn(' also... ', lines[0])
        self.assertIn(' this... ', lines[2])
        self.assertIn(' nope ', lines[4])

    def test_print_table_locale_american(self):
        """
        Verify that the locale of the german number is correctly
        controlling the format of how it is printed.
        """
        table = Table(self.rows, self.column_names, self.column_types)

        output = StringIO()
        table.print_table(max_columns=2, output=output, locale='en_US')
        # If it's working, 2000 should appear as the english '2,000'
        self.assertTrue("2,000" in output.getvalue())

    def test_print_table_locale_german(self):
        """
        Verify that the locale of the german number is correctly
        controlling the format of how it is printed.
        """
        table = Table(self.rows, self.column_names, self.column_types)

        output = StringIO()
        table.print_table(max_columns=2, output=output, locale='de_DE.UTF-8')
        # If it's working, the english '2,000' should appear as '2.000'
        self.assertTrue("2.000" in output.getvalue())