Switch core module to Python3

The core module (the relational algebra, not the interface) is now
using Python3

Tests are passing, so it should be ok.
This commit is contained in:
Salvo 'LtWorf' Tomaselli
2015-02-19 12:42:41 +01:00
parent 4019fbadb0
commit a355762fc4
9 changed files with 115 additions and 120 deletions

123
driver.py
View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# coding=UTF-8
# Relational
@@ -31,7 +31,7 @@ COLOR_GREEN = 0x00ff00
COLOR_MAGENTA = 0xff00ff
COLOR_CYAN = 0x00ffff
print relation
print(relation)
rels = {}
examples_path = 'samples/'
@@ -49,17 +49,17 @@ def readfile(fname):
def load_relations():
'''Loads all the relations present in the directory indicated in the
examples_path variable and stores them in the rels dictionary'''
print "Loading relations"
print("Loading relations")
for i in os.listdir(examples_path):
if i.endswith('.csv'): # It's a relation, loading it
# Naming the relation
relname = i[:-4]
print ("Loading relation %s with name %s..." % (i, relname)),
print ("Loading relation %s with name %s..." % (i, relname))
rels[relname] = relation.relation('%s%s' % (examples_path, i))
print 'done'
print('done')
def execute_tests():
@@ -93,49 +93,50 @@ def execute_tests():
ex_good += 1
else:
ex_bad += 1
print colorize("Resume of the results", COLOR_CYAN)
print (colorize("Resume of the results", COLOR_CYAN))
print colorize("Query tests", COLOR_MAGENTA)
print "Total test count: %d" % q_tot
print "Passed tests: %d" % q_good
print (colorize("Query tests", COLOR_MAGENTA))
print ("Total test count: %d" % q_tot)
print ("Passed tests: %d" % q_good)
if q_bad > 0:
print colorize("Failed tests count: %d" % q_bad, COLOR_RED)
print (colorize("Failed tests count: %d" % q_bad, COLOR_RED))
print colorize("Python tests", COLOR_MAGENTA)
print "Total test count: %d" % py_tot
print "Passed tests: %d" % py_good
print (colorize("Python tests", COLOR_MAGENTA))
print ("Total test count: %d" % py_tot)
print ("Passed tests: %d" % py_good)
if py_bad > 0:
print colorize("Failed tests count: %d" % py_bad, COLOR_RED)
print (colorize("Failed tests count: %d" % py_bad, COLOR_RED))
print colorize("Execute Python tests", COLOR_MAGENTA)
print "Total test count: %d" % ex_tot
print "Passed tests: %d" % ex_good
print (colorize("Execute Python tests", COLOR_MAGENTA))
print ("Total test count: %d" % ex_tot)
print ("Passed tests: %d" % ex_good)
if ex_bad > 0:
print colorize("Failed tests count: %d" % ex_bad, COLOR_RED)
print (colorize("Failed tests count: %d" % ex_bad, COLOR_RED))
print colorize("Total results", COLOR_CYAN)
print (colorize("Total results", COLOR_CYAN))
if q_bad + py_bad + ex_bad == 0:
print colorize("No failed tests", COLOR_GREEN)
print (colorize("No failed tests", COLOR_GREEN))
return 0
else:
print colorize("There are %d failed tests" % (py_bad + q_bad + ex_bad), COLOR_RED)
print (colorize("There are %d failed tests" %
(py_bad + q_bad + ex_bad), COLOR_RED))
return 1
def run_exec_test(testname):
'''Runs a python test, which executes code directly rather than queries'''
print "Running python test: " + colorize(testname, COLOR_MAGENTA)
print ("Running python test: " + colorize(testname, COLOR_MAGENTA))
glob = rels.copy()
exp_result = {}
try:
expr = readfile('%s%s.exec' % (tests_path, testname))
exec(expr, glob) # Evaluating the expression
expr = readfile('%s%s.exec' % (tests_path, testname))
exec(expr, glob) # Evaluating the expression
try:
exec(expr, glob) # Evaluating the expression
except Exception as e:
print (e)
raise Exception("")
expr = readfile('%s%s.result' % (tests_path, testname))
exp_result = eval(expr, rels) # Evaluating the expression
@@ -147,21 +148,22 @@ def run_exec_test(testname):
fields_ok = fields_ok and glob[i] == exp_result[i]
if fields_ok:
print colorize('Test passed', COLOR_GREEN)
print (colorize('Test passed', COLOR_GREEN))
return True
except:
pass
print colorize('ERROR', COLOR_RED)
print colorize('=====================================', COLOR_RED)
print "Expected %s" % exp_result
# print "Got %s" % result
print colorize('=====================================', COLOR_RED)
print (colorize('ERROR', COLOR_RED))
print (colorize('=====================================', COLOR_RED))
print ("Expected %s" % exp_result)
# print ("Got %s" % glob)
print (colorize('=====================================', COLOR_RED))
return False
def run_py_test(testname):
'''Runs a python test, which evaluates expressions directly rather than queries'''
print "Running expression python test: " + colorize(testname, COLOR_MAGENTA)
print ("Running expression python test: " +
colorize(testname, COLOR_MAGENTA))
try:
@@ -172,16 +174,16 @@ def run_py_test(testname):
exp_result = eval(expr, rels) # Evaluating the expression
if result == exp_result:
print colorize('Test passed', COLOR_GREEN)
print (colorize('Test passed', COLOR_GREEN))
return True
except:
pass
print colorize('ERROR', COLOR_RED)
print colorize('=====================================', COLOR_RED)
print "Expected %s" % exp_result
print "Got %s" % result
print colorize('=====================================', COLOR_RED)
print (colorize('ERROR', COLOR_RED))
print (colorize('=====================================', COLOR_RED))
print ("Expected %s" % exp_result)
print ("Got %s" % result)
print (colorize('=====================================', COLOR_RED))
return False
@@ -192,7 +194,7 @@ def run_test(testname):
testname.result
The query will be executed both unoptimized and
optimized'''
print "Running test: " + colorize(testname, COLOR_MAGENTA)
print ("Running test: " + colorize(testname, COLOR_MAGENTA))
query = None
expr = None
@@ -205,8 +207,7 @@ def run_test(testname):
try:
result_rel = relation.relation('%s%s.result' % (tests_path, testname))
query = unicode(
readfile('%s%s.query' % (tests_path, testname)).strip(), 'utf8')
query = readfile('%s%s.query' % (tests_path, testname)).strip()
o_query = optimizer.optimize_all(query, rels)
expr = parser.parse(query) # Converting expression to python string
@@ -220,29 +221,31 @@ def run_test(testname):
c_result = eval(c_expr, rels)
if (o_result == result_rel) and (result == result_rel) and (c_result == result_rel):
print colorize('Test passed', COLOR_GREEN)
print (colorize('Test passed', COLOR_GREEN))
return True
except Exception as inst:
print inst
print (inst)
pass
print colorize('ERROR', COLOR_RED)
print "Query: %s -> %s" % (query, expr)
print "Optimized query: %s -> %s" % (o_query, o_expr)
print colorize('=====================================', COLOR_RED)
print colorize("Expected result", COLOR_GREEN)
print result_rel
print colorize("Result", COLOR_RED)
print result
print colorize("Optimized result", COLOR_RED)
print o_result
print colorize("optimized result match %s" % str(result_rel == o_result), COLOR_MAGENTA)
print colorize("result match %s" % str(result == result_rel), COLOR_MAGENTA)
print colorize('=====================================', COLOR_RED)
print (colorize('ERROR', COLOR_RED))
print ("Query: %s -> %s" % (query, expr))
print ("Optimized query: %s -> %s" % (o_query, o_expr))
print (colorize('=====================================', COLOR_RED))
print (colorize("Expected result", COLOR_GREEN))
print (result_rel)
print (colorize("Result", COLOR_RED))
print (result)
print (colorize("Optimized result", COLOR_RED))
print (o_result)
print (colorize("optimized result match %s" %
str(result_rel == o_result), COLOR_MAGENTA))
print (colorize("result match %s" %
str(result == result_rel), COLOR_MAGENTA))
print (colorize('=====================================', COLOR_RED))
return False
if __name__ == '__main__':
print "-> Starting testsuite for relational"
print ("-> Starting testsuite for relational")
load_relations()
print "-> Starting tests"
print ("-> Starting tests")
exit(execute_tests())