Debian Codegraph @

tests/test_models_report_table.py

raw symbols SCIP
1 import logging
2 import tempfile
3
4 import pytest
5
6 from pbcommand.models.report import Table, Column, PbReportError
7
8 log = logging.getLogger(__name__)
9
10
11 class TestEmptyTable:
12
13 """Basic Smoke tests"""
14
15 def setup_method(self, method):
16 self.columns = [Column('one', header="One"),
17 Column('two', header="Two"),
18 Column('three', header="Three")]
19
20 self.table = Table('my_table', columns=self.columns)
21
22 def test_str(self):
23 """Smoke test for conversion to str"""
24 log.info(str(self.table))
25 assert str(self.table) is not None
26
27 def test_columns(self):
28 """Test Columns"""
29 assert len(self.table.columns) == 3
30
31 def test_column_values(self):
32 """Basic check for column values"""
33 for column in self.table.columns:
34 assert len(column.values) == 0
35
36 def test_to_dict(self):
37 """Conversion to dictionary"""
38 assert isinstance(self.table.to_dict(), dict)
39 log.info(self.table.to_dict())
40
41
42 class TestBasicTable:
43
44 """Basic Smoke tests"""
45
46 def setup_method(self, method):
47 self.columns = [Column('one', header="One"),
48 Column('two', header="Two"),
49 Column('three', header="Three"),
50 Column('four', header="Four")]
51 self.table = Table('my_table_with_values', columns=self.columns)
52 datum = [
53 ('one', list(range(3))),
54 ('two', list('abc')),
55 ('three', 'file1 file2 file3'.split()),
56 ('four', [3.14159, 2.72, 9.87654321])
57 ]
58 for k, values in datum:
59 for value in values:
60 self.table.add_data_by_column_id(k, value)
61
62 def test_str(self):
63 """Smoke test for conversion to str"""
64 log.info(str(self.table))
65 assert str(self.table) is not None
66
67 def test_columns(self):
68 """Test Columns"""
69 assert len(self.table.columns) == 4
70
71 def test_column_values(self):
72 """Basic check for column values"""
73 for column in self.table.columns:
74 assert len(column.values) == 3
75
76 def test_to_dict(self):
77 """Conversion to dictionary"""
78 assert isinstance(self.table.to_dict(), dict)
79 log.info(self.table.to_dict())
80
81 def test_to_csv(self):
82 f = tempfile.NamedTemporaryFile(suffix=".csv").name
83 self.table.to_csv(f)
84 with open(f) as csv_out:
85 assert csv_out.read() == "One,Two,Three,Four\n0,a,file1,3.14159\n1,b,file2,2.72\n2,c,file3,9.87654321\n"
86 self.table.to_csv(f, float_format="%.4f")
87 with open(f) as csv_out:
88 assert csv_out.read() == "One,Two,Three,Four\n0,a,file1,3.1416\n1,b,file2,2.7200\n2,c,file3,9.8765\n"
89 self.table.to_csv(f, float_format="{:.2f}")
90 with open(f) as csv_out:
91 assert csv_out.read() == "One,Two,Three,Four\n0,a,file1,3.14\n1,b,file2,2.72\n2,c,file3,9.88\n"
92
93
94 class TestTable:
95
96 def test_table(self):
97 """Can't create an Table without an id."""
98 def none_table():
99 t = Table(None)
100 with pytest.raises(Exception):
101 none_table()
102
103 def test_add_column(self):
104 """Cannot add column with duplicate id."""
105 cs = [Column('1'), Column('2')]
106 t = Table('foo', columns=cs)
107
108 def add_dupe():
109 t.add_column(Column('2'))
110
111 assert cs == t.columns
112
113 with pytest.raises(PbReportError):
114 add_dupe()
115
116 def test_append_data(self):
117 """Append data to columns by index."""
118
119 cs = [Column('1'), Column('2')]
120 t = Table('foo', columns=cs)
121
122 t.append_data(0, 'whatev')
123 t.append_data(0, 'huh')
124 t.append_data(1, 'ernie')
125 t.append_data(1, 'bert')
126
127 assert ['whatev', 'huh'] == t.columns[0].values
128 assert ['ernie', 'bert'] == t.columns[1].values
129
130 def test_add_data_by_column_id(self):
131 """Added data values by column identifier."""
132
133 columns = [Column('one'), Column('two')]
134 table = Table('mytable', columns=columns)
135
136 datum = {'one': 12.0, 'two': 1234.0}
137
138 for k, v in datum.items():
139 table.add_data_by_column_id(k, v)
140
141 assert 12.0 in table.columns[0].values
142 assert 1234.0 in table.columns[1].values