Package test_suite :: Package unit_tests :: Package _lib :: Package _text :: Module test_table
[hide private]
[frames] | no frames]

Source Code for Module test_suite.unit_tests._lib._text.test_table

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2013 Edward d'Auvergne                                        # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Python module imports. 
 23  from unittest import TestCase 
 24   
 25  # relax module imports. 
 26  from lib.text.table import format_table, MULTI_COL 
 27   
 28   
29 -class Test_table(TestCase):
30 """Unit tests for the lib.text.table relax module.""" 31
32 - def test_format_table1(self):
33 """Test 1 of the lib.text.table.format_table() function.""" 34 35 # The table data. 36 headings = [ 37 ['Column 1', 'Column 2'] 38 ] 39 contents = [ 40 ['A', '2'], 41 ['B', '2'] 42 ] 43 44 # Create the table. 45 table = format_table(headings=headings, contents=contents) 46 table_lines = table.split('\n') 47 48 # The true table. 49 true_table = [ 50 " _____________________ ", 51 " ", 52 " Column 1 Column 2 ", 53 " _____________________ ", 54 " ", 55 " A 2 ", 56 " B 2 ", 57 " _____________________ ", 58 " ", 59 "" # This is because split combined with a final \n character. 60 ] 61 62 # Printout. 63 print("The formatted table:") 64 for i in range(len(table_lines)): 65 print("'%s'" % table_lines[i]) 66 print("\nWhat the table should look like:") 67 for i in range(len(true_table)): 68 print("'%s'" % true_table[i]) 69 70 # Check the table. 71 self.assertEqual(len(true_table), len(table_lines)) 72 for i in range(len(table_lines)): 73 self.assertEqual(true_table[i], table_lines[i])
74 75
76 - def test_format_table2(self):
77 """Test 2 of the lib.text.table.format_table() function.""" 78 79 # The table data. 80 headings = [ 81 ['Column 1', 'Column 2'] 82 ] 83 contents = [ 84 ['A', '2'], 85 ['B', '2'] 86 ] 87 88 # Create the table. 89 table = format_table(headings=headings, contents=contents, max_width=30, spacing=True, debug=True) 90 table_lines = table.split('\n') 91 92 # The true table. 93 true_table = [ 94 " _____________________ ", 95 " ", 96 " Column 1 Column 2 ", 97 " _____________________ ", 98 " ", 99 " A 2 ", 100 " ", 101 " B 2 ", 102 " _____________________ ", 103 " ", 104 "" # This is because split combined with a final \n character. 105 ] 106 107 # Printout. 108 print("The formatted table:") 109 for i in range(len(table_lines)): 110 print("'%s'" % table_lines[i]) 111 print("\nWhat the table should look like:") 112 for i in range(len(true_table)): 113 print("'%s'" % true_table[i]) 114 115 # Check the table. 116 self.assertEqual(len(true_table), len(table_lines)) 117 for i in range(len(table_lines)): 118 self.assertEqual(true_table[i], table_lines[i])
119 120
121 - def test_format_table3(self):
122 """Test 3 of the lib.text.table.format_table() function.""" 123 124 # The table data. 125 headings = [ 126 ['', 'Long text span test', MULTI_COL], 127 ['Column 1', 'Column 2', 'Column 3'] 128 ] 129 contents = [ 130 ['A', '2', '3.456'], 131 ['B', '2', '4.567'] 132 ] 133 134 # Create the table. 135 table = format_table(headings=headings, contents=contents, spacing=True) 136 table_lines = table.split('\n') 137 138 # The true table. 139 true_table = [ 140 " ________________________________ ", 141 " ", 142 " Long text span test ", 143 " ", 144 " Column 1 Column 2 Column 3 ", 145 " ________________________________ ", 146 " ", 147 " A 2 3.456 ", 148 " ", 149 " B 2 4.567 ", 150 " ________________________________ ", 151 " ", 152 "" # This is because split combined with a final \n character. 153 ] 154 155 # Printout. 156 print("The formatted table:") 157 for i in range(len(table_lines)): 158 print("'%s'" % table_lines[i]) 159 print("\nWhat the table should look like:") 160 for i in range(len(true_table)): 161 print("'%s'" % true_table[i]) 162 163 # Check the table. 164 self.assertEqual(len(true_table), len(table_lines)) 165 for i in range(len(table_lines)): 166 self.assertEqual(true_table[i], table_lines[i])
167 168
169 - def test_format_table4(self):
170 """Test 4 of the lib.text.table.format_table() function.""" 171 172 # The table data. 173 headings = [ 174 [None, 'Long text span test', MULTI_COL, MULTI_COL], 175 ['Column 1', 'Column 2', 'Column 3', 'Column 4'] 176 ] 177 contents = [ 178 ['A', 2, 3.4561234124, [1, 2.0]], 179 ['B', 2, 4.567745674, 1e-6] 180 ] 181 182 # Create the table. 183 table = format_table(headings=headings, contents=contents, spacing=True, custom_format=[None, None, '%.3f', None]) 184 table_lines = table.split('\n') 185 186 # The true table. 187 true_table = [ 188 " ___________________________________________ ", 189 " ", 190 " Long text span test ", 191 " ", 192 " Column 1 Column 2 Column 3 Column 4 ", 193 " ___________________________________________ ", 194 " ", 195 " A 2 3.456 [1, 2.0] ", 196 " ", 197 " B 2 4.568 1e-06 ", 198 " ___________________________________________ ", 199 " ", 200 "" # This is because split combined with a final \n character. 201 ] 202 203 # Printout. 204 print("The formatted table:") 205 for i in range(len(table_lines)): 206 print("'%s'" % table_lines[i]) 207 print("\nWhat the table should look like:") 208 for i in range(len(true_table)): 209 print("'%s'" % true_table[i]) 210 211 # Check the table. 212 self.assertEqual(len(true_table), len(table_lines)) 213 for i in range(len(table_lines)): 214 self.assertEqual(true_table[i], table_lines[i])
215 216
217 - def test_format_table5(self):
218 """Test 5 of the lib.text.table.format_table() function - no headings.""" 219 220 # The table data. 221 contents = [ 222 ['A', 2], 223 ['B', True] 224 ] 225 226 # Create the table. 227 table = format_table(contents=contents) 228 table_lines = table.split('\n') 229 230 # The true table. 231 true_table = [ 232 " __________ ", 233 " ", 234 " A 2 ", 235 " B True ", 236 " __________ ", 237 " ", 238 "" # This is because split combined with a final \n character. 239 ] 240 241 # Printout. 242 print("The formatted table:") 243 for i in range(len(table_lines)): 244 print("'%s'" % table_lines[i]) 245 print("\nWhat the table should look like:") 246 for i in range(len(true_table)): 247 print("'%s'" % true_table[i]) 248 249 # Check the table. 250 self.assertEqual(len(true_table), len(table_lines)) 251 for i in range(len(table_lines)): 252 self.assertEqual(true_table[i], table_lines[i])
253 254
255 - def test_format_table6(self):
256 """Test 6 of the lib.text.table.format_table() function - no headings.""" 257 258 # The table data. 259 headings = [['Model', 'k', 'chi2', 'AIC', 'Average position', MULTI_COL, MULTI_COL, 'Motional eigenframe', MULTI_COL, MULTI_COL, 'Order parameters (deg)', MULTI_COL, MULTI_COL], [None, None, None, None, 'a', 'b', 'g', 'a', 'b/th', 'g/ph', 'thx', 'thy', 'smax']] 260 contents = [['Rigid', 6, 1611.0583844357488, 1623.0583844357488, 2.7928187044509789, 6.241673451655573, 3.3350126302921255, None, None, None, None, None, None], ['Rotor', 9, 1393.0628812874404, 1411.0628812874404, 2.3720778521835015, 6.2511294411496241, 3.7347870727084764, None, 1.3782156252713658, 5.5998324326753401, None, None, 36.651271107544183], ['Iso cone, torsionless', 9, 1407.9014811061686, 1425.9014811061686, 2.2550248034078395, 6.2368882019396619, 3.891108977360032, None, 0.25090427716293384, 1.590485101074278, 21.287274572663485, None, None], ['Iso cone', 10, 1400.3558737738815, 1420.3558737738815, 2.8146957276396858, 6.2597080483925627, 3.2956149488567879, None, 1.3956123975976844, 5.5817149266639987, 10.300677006193942, None, 32.387495822632452], ['Pseudo ellipse, torsionless', 11, 1386.6214759007082, 1408.6214759007082, 2.6253119819082835, 6.2528446735668872, 3.4989380500907097, 2.692632830571366, 0.43833843941243616, 1.3038063115520346, 33.512494725673051, 15.888178532164503, None], ['Pseudo ellipse', 12, 1378.8893702060313, 1402.8893702060313, 2.7403158840045716, 6.259192518336242, 3.3759530521363121, 6.1651101516049849, 1.3600775439064279, 5.5851511636460813, 13.646328409458231, 0.74265383200964785, 31.027675419200627]] 261 262 # Create the table. 263 table = format_table(headings=headings, contents=contents, custom_format=[None, None, "%.2f", "%.2f", "%.3f", "%.3f", "%.3f", "%.3f", "%.3f", "%.3f", "%.2f", "%.2f", "%.2f"]) 264 table_lines = table.split('\n') 265 266 # The true table. 267 true_table = [ 268 " _______________________________________________________________________________________________________________________________ ", 269 " ", 270 " Model k chi2 AIC Average position Motional eigenframe Order parameters (deg) ", 271 " a b g a b/th g/ph thx thy smax ", 272 " _______________________________________________________________________________________________________________________________ ", 273 " ", 274 " Rigid 6 1611.06 1623.06 2.793 6.242 3.335 ", 275 " Rotor 9 1393.06 1411.06 2.372 6.251 3.735 1.378 5.600 36.65 ", 276 " Iso cone, torsionless 9 1407.90 1425.90 2.255 6.237 3.891 0.251 1.590 21.29 ", 277 " Iso cone 10 1400.36 1420.36 2.815 6.260 3.296 1.396 5.582 10.30 32.39 ", 278 " Pseudo ellipse, torsionless 11 1386.62 1408.62 2.625 6.253 3.499 2.693 0.438 1.304 33.51 15.89 ", 279 " Pseudo ellipse 12 1378.89 1402.89 2.740 6.259 3.376 6.165 1.360 5.585 13.65 0.74 31.03 ", 280 " _______________________________________________________________________________________________________________________________ ", 281 " ", 282 "" # This is because split combined with a final \n character. 283 ] 284 285 # Printout. 286 print("The formatted table:") 287 for i in range(len(table_lines)): 288 print("'%s'" % table_lines[i]) 289 print("\nWhat the table should look like:") 290 for i in range(len(true_table)): 291 print("'%s'" % true_table[i]) 292 293 # Check the table. 294 self.assertEqual(len(true_table), len(table_lines)) 295 for i in range(len(table_lines)): 296 self.assertEqual(true_table[i], table_lines[i])
297