33Unit testing the variety of data types supported by tableformatter.
44"""
55from collections import OrderedDict
6- import numpy as np
7- import pandas as pd
86import tableformatter as tf
97
108
2624
2725iteralbe_of_iterables = [[1 , 2 , 3 , 4 ],
2826 [5 , 6 , 7 , 8 ]]
29- np_2d_array = np .array (iteralbe_of_iterables )
3027d = {'col1' : [1 , 5 ], 'col2' : [2 , 6 ], 'col3' : [3 , 7 ], 'col4' : [4 , 8 ]}
3128od = OrderedDict (sorted (d .items (), key = lambda t : t [0 ]))
32- df = pd .DataFrame (data = od )
3329
3430
3531def test_iterable_of_iterables ():
3632 table = tf .generate_table (iteralbe_of_iterables )
3733 assert table == EXPECTED_BASIC
3834
3935
40- def test_numpy_2d_array ():
41- table = tf .generate_table (np_2d_array )
42- assert table == EXPECTED_BASIC
43-
44-
4536def test_iterable_of_dicts ():
4637 d1 = {1 : 'a' , 2 : 'b' , 3 : 'c' , 4 : 'd' }
4738 d2 = {5 : 'e' , 6 : 'f' , 7 : 'g' , 8 : 'h' }
@@ -51,27 +42,6 @@ def test_iterable_of_dicts():
5142 assert table == EXPECTED_BASIC
5243
5344
54- def test_numpy_record_array ():
55- np_rec_array = np .rec .array ([(1 , 2. , 'Hello' ),
56- (2 , 3. , "World" )],
57- dtype = [('foo' , 'i4' ), ('bar' , 'f4' ), ('baz' , 'U10' )])
58- table = tf .generate_table (np_rec_array )
59- expected = '''
60- ╔═════╤═════╤═══════╗
61- ║ foo │ bar │ baz ║
62- ╠═════╪═════╪═══════╣
63- ║ 1 │ 2.0 │ Hello ║
64- ║ 2 │ 3.0 │ World ║
65- ╚═════╧═════╧═══════╝
66- ''' .lstrip ('\n ' )
67- assert table == expected
68-
69-
70- def test_pandas_dataframe ():
71- table = tf .generate_table (df )
72- assert table == EXPECTED_WITH_HEADERS
73-
74-
7545def test_dict_of_iterables ():
7646 table = tf .generate_table (od )
7747 assert table == EXPECTED_WITH_HEADERS
@@ -99,3 +69,41 @@ def test_iterable_of_non_iterable_objects():
9969 tf .Column ('col4' , attrib = 'field4' ))
10070 table = tf .generate_table (rows , columns )
10171 assert table == EXPECTED_WITH_HEADERS
72+
73+
74+ try :
75+ import numpy as np
76+ except ImportError :
77+ pass
78+ else :
79+ np_2d_array = np .array (iteralbe_of_iterables )
80+
81+ def test_numpy_2d_array ():
82+ table = tf .generate_table (np_2d_array )
83+ assert table == EXPECTED_BASIC
84+
85+ def test_numpy_record_array ():
86+ np_rec_array = np .rec .array ([(1 , 2. , 'Hello' ),
87+ (2 , 3. , "World" )],
88+ dtype = [('foo' , 'i4' ), ('bar' , 'f4' ), ('baz' , 'U10' )])
89+ table = tf .generate_table (np_rec_array )
90+ expected = '''
91+ ╔═════╤═════╤═══════╗
92+ ║ foo │ bar │ baz ║
93+ ╠═════╪═════╪═══════╣
94+ ║ 1 │ 2.0 │ Hello ║
95+ ║ 2 │ 3.0 │ World ║
96+ ╚═════╧═════╧═══════╝
97+ ''' .lstrip ('\n ' )
98+ assert table == expected
99+
100+ try :
101+ import pandas as pd
102+ except ImportError :
103+ pass
104+ else :
105+ df = pd .DataFrame (data = od )
106+
107+ def test_pandas_dataframe ():
108+ table = tf .generate_table (df )
109+ assert table == EXPECTED_WITH_HEADERS
0 commit comments