Package tkintertable :: Module Testing
[hide private]
[frames] | no frames]

Source Code for Module tkintertable.Testing

  1  #!/usr/bin/env python
 
  2  """
 
  3      Table Testing module.
 
  4      Created Oct 2008
 
  5      Copyright (C) Damien Farrell
 
  6  
 
  7      This program is free software; you can redistribute it and/or
 
  8      modify it under the terms of the GNU General Public License
 
  9      as published by the Free Software Foundation; either version 2
 
 10      of the License, or (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, write to the Free Software
 
 19      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 20  """ 
 21  
 
 22  import random, string 
 23  from Tkinter import * 
 24  from Tables import TableCanvas 
 25  from TableModels import TableModel 
 26  
 
 27  """Testing general functionality of tables""" 
 28  
 
29 -class App:
30 - def __init__(self, master):
31 self.main = Frame(master) 32 self.main.pack(fill=BOTH,expand=1) 33 master.geometry('600x400+200+100')
34
35 -def createRandomStrings(l,n):
36 """create list of l random strings, each of length n""" 37 names = [] 38 for i in range(l): 39 val = ''.join(random.choice(string.ascii_lowercase) for x in range(n)) 40 names.append(val) 41 return names
42
43 -def createData(rows=20, cols=5):
44 """Creare random dict for test data""" 45 46 data = {} 47 names = createRandomStrings(rows,16) 48 colnames = createRandomStrings(cols,5) 49 for n in names: 50 data[n]={} 51 data[n]['label'] = n 52 for c in range(0,cols): 53 colname=colnames[c] 54 vals = [round(random.normalvariate(100,50),2) for i in range(0,len(names))] 55 vals = sorted(vals) 56 i=0 57 for n in names: 58 data[n][colname] = vals[i] 59 i+=1 60 return data
61
62 -def createTable(model):
63 t=Toplevel() 64 app = App(t) 65 master = app.main 66 table = TableCanvas(master, model,rowheaderwidth=50) 67 table.createTableFrame() 68 return table
69
70 -def test1(root):
71 """Setup a table and populate it with data""" 72 app = App(root) 73 master = app.main 74 model = TableModel() 75 data = createData(40) 76 #import after model created 77 #print data 78 model.importDict(data) 79 table = TableCanvas(master, model, 80 cellwidth=60, cellbackgr='#e3f698', 81 thefont=('Arial',12),rowheight=18, rowheaderwidth=30, 82 rowselectedcolor='yellow', editable=True) 83 table.createTableFrame() 84 #table.sortTable(columnName='label') 85 #remove cols 86 model.deleteColumns([0]) 87 model.deleteRows(range(0,2)) 88 #table.redrawTable() 89 #add rows and cols 90 table.addRow(1,label='aaazzz') 91 table.addRow(label='bbb') 92 table.addRow(**{'label':'www'}) 93 table.addColumn('col6') 94 model.data[1]['col6']='TEST' 95 #table.redrawTable() 96 #change col labels 97 model.columnlabels['col6'] = 'new label' 98 #set and get selections 99 table.setSelectedRow(2) 100 table.setSelectedCol(1) 101 table.setSelectedCells(1,80,2,4) 102 #print table.getSelectionValues() 103 #table.plotSelected(graphtype='XY') 104 #save data 105 #table.addRows(50000) 106 model.save('test.table') 107 #load new data 108 table.load('test.table') 109 #root.after(2000, root.quit) 110 return
111
112 -def test2():
113 """Multuple tables in one window""" 114 t=Toplevel() 115 app = App(t) 116 master = app.main 117 c=0; r=1 118 for i in range(12): 119 model = TableModel() 120 data = createData(50) 121 model.importDict(data) 122 fr = Frame(master) 123 if c%3==0: c=0; r+=1 124 fr.grid(row=r,column=c,sticky='nws') 125 table = TableCanvas(fr, model, width=250,height=150,rowheaderwidth=0) 126 table.createTableFrame() 127 c+=1 128 return
129
130 -def test3():
131 """Drawing large tables""" 132 data = createData(10000) 133 model = TableModel() 134 model.importDict(data) 135 createTable(model) 136 return
137
138 -def test4():
139 """Filtering/searching""" 140 model = TableModel() 141 data = createData(100) 142 model.importDict(data) 143 model.addColumn('comment') 144 for i in model.reclist: 145 val = random.sample(['a','b','c'],1)[0] 146 model.data[i]['comment'] = val 147 #searchterms = [('label', 'aa', 'contains', 'AND'), 148 # ('label', 'bb', 'contains', 'OR')] 149 searchterms = [('comment', 'a', '!=', 'AND'), 150 ('comment', 'b', '!=', 'AND')] 151 vals = model.getColumnData(columnIndex=0, filters=searchterms) 152 #model.getColumns(model.columnNames, filters=searchterms) 153 #model.getDict(model.columnNames, filters=searchterms) 154 print '%s found' %len(vals) 155 #createTable(model) 156 return
157
158 -def test5():
159 """frame placement""" 160 import Pmw 161 t=Toplevel() 162 app = App(t) 163 master = app.main 164 w = Pmw.Group(master, 165 tag_text='Show/Hide') 166 w.pack(fill=BOTH) 167 f=Frame(master) 168 f.pack(fill=BOTH) 169 table = TableCanvas(w) 170 table.createTableFrame() 171 return
172
173 -def GUITests():
174 """Run standard tests""" 175 root = Tk() 176 test1(root) 177 test2() 178 test3() 179 test4() 180 #test5() 181 print 'GUI tests done' 182 return root
183
184 -def main():
185 root = GUITests() 186 root.mainloop()
187 #loadSaveTest() 188 189 if __name__ == '__main__': 190 main() 191