Use debian 2.7 only
[fpbd-bostik.git] / pyfpdb / TestSummaryImport.py
blob2daa139e8cf38c63f81ca0543f9ce75ede22c623
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Copyright 2010-2011, Carl Gherardi
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ########################################################################
21 import sys
22 import os
23 import codecs
24 import pprint
25 import Configuration
26 import Database
27 import Options
28 import SQL
29 from GuiTourneyImport import SummaryImporter
32 class FpdbError:
33 def __init__(self, sitename):
34 self.site = sitename
35 self.errorcount = 0
36 self.histogram = {}
37 self.statcount = {}
39 def error_report(self, filename, hand, stat, ghash, testhash, player):
40 print "Regression Test Error:"
41 print "\tFile: %s" % filename
42 print "\tStat: %s" % stat
43 print "\tPlayer: %s" % player
44 if filename in self.histogram:
45 self.histogram[filename] += 1
46 else:
47 self.histogram[filename] = 1
49 if stat in self.statcount:
50 self.statcount[stat] += 1
51 else:
52 self.statcount[stat] = 1
53 self.errorcount += 1
55 def print_histogram(self):
56 print "%s:" % self.site
57 for f in self.histogram:
58 idx = f.find('regression')
59 print "(%3d) : %s" %(self.histogram[f], f[idx:])
61 def compare(leaf, importer, errors, site):
62 filename = leaf
63 #print "DEBUG: fileanme: %s" % filename
65 if filename.endswith('.txt'):
66 # test if there is a .hp version of the file
67 importer.addImportFileOrDir(filename, site = site)
68 (stored, errs) = importer.runImport()
70 # if os.path.isfile(filename + '.hp') and errs < 1:
71 # # Compare them
72 # hashfilename = filename + '.hp'
74 # in_fh = codecs.open(hashfilename, 'r', 'utf8')
75 # whole_file = in_fh.read()
76 # in_fh.close()
78 # testhash = eval(whole_file)
80 # hhc = importer.getCachedHHC()
81 # handlist = hhc.getProcessedHands()
82 # #We _really_ only want to deal with a single hand here.
83 # for hand in handlist:
84 # ghash = hand.stats.getHandsPlayers()
85 # for p in ghash:
86 # #print "DEBUG: player: '%s'" % p
87 # pstat = ghash[p]
88 # teststat = testhash[p]
90 # for stat in pstat:
91 # #print "pstat[%s][%s]: %s == %s" % (p, stat, pstat[stat], teststat[stat])
92 # try:
93 # if pstat[stat] == teststat[stat]:
94 # # The stats match - continue
95 # pass
96 # else:
97 # # Stats don't match - Doh!
98 # errors.error_report(filename, hand, stat, ghash, testhash, p)
99 # except KeyError, e:
100 # errors.error_report(filename, False, "KeyError: '%s'" % stat, False, False, p)
101 if errs > 0:
102 errors.error_report(filename, False, "Parse", False, False, False)
104 importer.clearFileList()
108 def walk_testfiles(dir, function, importer, errors, site):
109 """Walks a directory, and executes a callback on each file """
110 dir = os.path.abspath(dir)
111 for file in [file for file in os.listdir(dir) if not file in [".",".."]]:
112 nfile = os.path.join(dir,file)
113 if os.path.isdir(nfile):
114 walk_testfiles(nfile, compare, importer, errors, site)
115 else:
116 print "***********************************"
117 compare(nfile, importer, errors, site)
118 print "***********************************"
120 def main(argv=None):
121 if argv is None:
122 argv = sys.argv[1:]
124 Configuration.set_logfile("fpdb-log.txt")
125 (options, argv) = Options.fpdb_options()
127 test_all_sites = True
129 if options.usage == True:
130 usage()
132 single_file_test = False
134 if options.sitename:
135 options.sitename = Options.site_alias(options.sitename)
136 if options.sitename == False:
137 usage()
138 if options.filename:
139 print "Testing single hand: '%s'" % options.filename
140 single_file_test = True
141 else:
142 print "Only regression testing '%s' files" % (options.sitename)
143 test_all_sites = False
145 config = Configuration.Config(file = "HUD_config.test.xml")
146 db = Database.Database(config)
147 sql = SQL.Sql(db_server = 'sqlite')
148 db.recreate_tables()
150 importer = SummaryImporter(config, sql, None)
152 PokerStarsErrors = FpdbError('PokerStars')
153 FTPErrors = FpdbError('Full Tilt Poker')
154 #PartyPokerErrors = FpdbError('Party Poker')
155 #BetfairErrors = FpdbError('Betfair')
156 #OnGameErrors = FpdbError('OnGame')
157 #AbsoluteErrors = FpdbError('Absolute Poker')
158 #UltimateBetErrors = FpdbError('Ultimate Bet')
159 #EverleafErrors = FpdbError('Everleaf Poker')
160 MergeErrors = FpdbError('Merge')
161 #PKRErrors = FpdbError('PKR')
162 iPokerErrors = FpdbError('iPoker')
163 #WinamaxErrors = FpdbError('Winamax')
165 ErrorsList = [
166 PokerStarsErrors, FTPErrors, #WinamaxErrors,
167 MergeErrors, iPokerErrors,
168 #PartyPokerErrors,
169 #BetfairErrors, OnGameErrors, AbsoluteErrors,
170 #EverleafErrors, PKRErrors,
171 #UltimateBetErrors,
174 sites = {
175 'PokerStars' : False,
176 'Full Tilt Poker' : False,
177 #'PartyPoker' : True,
178 #'Betfair' : True,
179 #'OnGame' : True,
180 #'Absolute' : True,
181 #'UltimateBet' : True,
182 #'Everleaf' : True,
183 'Merge' : False,
184 #'PKR' : False,
185 'iPoker' : False,
186 # 'Winamax' : False,
189 if test_all_sites == True:
190 for s in sites:
191 sites[s] = True
192 else:
193 sites[options.sitename] = True
195 if sites['PokerStars'] == True and not single_file_test:
196 walk_testfiles("regression-test-files/summaries/Stars/", compare, importer, PokerStarsErrors, "PokerStars")
197 elif sites['PokerStars'] == True and single_file_test:
198 walk_testfiles(options.filename, compare, importer, PokerStarsErrors, "PokerStars")
199 if sites['Full Tilt Poker'] == True and not single_file_test:
200 walk_testfiles("regression-test-files/summaries/FTP/", compare, importer, FTPErrors, "Full Tilt Poker")
201 elif sites['Full Tilt Poker'] == True and single_file_test:
202 walk_testfiles(options.filename, compare, importer, FTPErrors, "Full Tilt Poker")
203 #if sites['PartyPoker'] == True:
204 # walk_testfiles("regression-test-files/cash/PartyPoker/", compare, importer, PartyPokerErrors, "PartyPoker")
205 # walk_testfiles("regression-test-files/tour/PartyPoker/", compare, importer, PartyPokerErrors, "PartyPoker")
206 #if sites['Betfair'] == True:
207 # walk_testfiles("regression-test-files/cash/Betfair/", compare, importer, BetfairErrors, "Betfair")
208 #if sites['OnGame'] == True:
209 # walk_testfiles("regression-test-files/cash/OnGame/", compare, importer, OnGameErrors, "OnGame")
210 #if sites['Absolute'] == True:
211 # walk_testfiles("regression-test-files/cash/Absolute/", compare, importer, AbsoluteErrors, "Absolute")
212 #if sites['UltimateBet'] == True:
213 # walk_testfiles("regression-test-files/cash/UltimateBet/", compare, importer, UltimateBetErrors, "Absolute")
214 #if sites['Everleaf'] == True:
215 # walk_testfiles("regression-test-files/cash/Everleaf/", compare, importer, EverleafErrors, "Everleaf")
216 if sites['Merge'] == True:
217 walk_testfiles("regression-test-files/cash/Merge/", compare, importer, MergeErrors, "Merge")
218 #if sites['PKR'] == True:
219 # walk_testfiles("regression-test-files/cash/PKR/", compare, importer, PKRErrors, "PKR")
220 if sites['iPoker'] == True:
221 walk_testfiles("regression-test-files/tour/iPoker/", compare, importer, iPokerErrors, "iPoker")
222 #if sites['Winamax'] == True:
223 # walk_testfiles("regression-test-files/summaries/Winamax/", compare, importer, WinamaxErrors, "Winamax")
225 totalerrors = 0
227 for i, site in enumerate(ErrorsList):
228 totalerrors += ErrorsList[i].errorcount
230 print "---------------------"
231 print "Total Errors: %d" % totalerrors
232 print "---------------------"
233 for i, site in enumerate(ErrorsList):
234 ErrorsList[i].print_histogram()
236 # Merge the dicts of stats from the various error objects
237 statdict = {}
238 for i, site in enumerate(ErrorsList):
239 tmp = ErrorsList[i].statcount
240 for stat in tmp:
241 if stat in statdict:
242 statdict[stat] += tmp[stat]
243 else:
244 statdict[stat] = tmp[stat]
246 print "\n"
247 print "---------------------"
248 print "Errors by stat:"
249 print "---------------------"
250 #for stat in statdict:
251 # print "(%3d) : %s" %(statdict[stat], stat)
253 sortedstats = sorted([(value,key) for (key,value) in statdict.items()])
254 for num, stat in sortedstats:
255 print "(%3d) : %s" %(num, stat)
258 if __name__ == '__main__':
259 sys.exit(main())