def run_fail_test(testname): '''Runs a test, which executes a query that is supposed to fail''' print ("Running fail test: " + colorize(testname, COLOR_MAGENTA)) query = readfile('%s%s.fail' % (tests_path, testname)).strip() test_succeed = True try: expr = parser.parse(query) expr(rels) test_succeed = False except: pass try: o_query = optimizer.optimize_all(query, rels) o_expr = parser.parse(o_query) o_expr(rels) test_succeed = False except: pass try: c_expr = parser.tree(query).toCode() eval(c_expr, rels) test_succeed = False except: pass if test_succeed: print (colorize('Test passed', COLOR_GREEN)) else: print (colorize('Test failed (by not raising any exception)', COLOR_RED)) return test_succeed
def run_fail_test(testname): '''Runs a test, which executes a query that is supposed to fail''' print("Running fail test: " + colorize(testname, COLOR_MAGENTA)) query = readfile('%s%s.fail' % (tests_path, testname)).strip() test_succeed = True try: expr = parser.parse(query) expr(rels) test_succeed = False except: pass try: o_query = optimizer.optimize_all(query, rels) o_expr = parser.parse(o_query) o_expr(rels) test_succeed = False except: pass try: c_expr = parser.tree(query).toCode() eval(c_expr, rels) test_succeed = False except: pass if test_succeed: print(colorize('Test passed', COLOR_GREEN)) else: print(colorize('Test failed (by not raising any exception)', COLOR_RED)) return test_succeed
def _getrels(self): rels, tags, dev_dict = get_relations() context = {'devices': rels, 'tags': tags} try: expr_on = parse(self.onquery) except Exception as e: syslog(LOG_ERR, 'Unable to parse %s %s' % (self.onquery, e)) raise try: expr_off = parse(self.offquery) except Exception as e: syslog(LOG_ERR, 'Unable to parse %s %s' % (self.offquery, e)) raise try: rel_devs_on = expr_on(context) except Exception as e: syslog(LOG_ERR, 'Error in running query: %s %s' % (self.onquery, e)) raise try: rel_devs_off = expr_off(context) except Exception as e: syslog(LOG_ERR, 'Error in running query: %s %s' % (self.offquery, e)) raise # Allow overlapping queries if len(rel_devs_off.intersection(rel_devs_on)): syslog(LOG_WARNING, 'Relations intersect!') rel_devs_off = rel_devs_off.difference(rel_devs_on) return dev_dict, rel_devs_on, rel_devs_off
def run_test(testname): '''Runs a specific test executing the file testname.query and comparing the result with testname.result The query will be executed both unoptimized and optimized''' print("Running test: " + colorize(testname, COLOR_MAGENTA)) query = None expr = None o_query = None o_expr = None result_rel = None result = None o_result = None try: result_rel = relation.Relation.load('%s%s.result' % (tests_path, testname)) query = readfile('%s%s.query' % (tests_path, testname)).strip() o_query = optimizer.optimize_all(query, rels) expr = parser.parse(query) result = expr(rels) o_expr = parser.parse(o_query) o_result = o_expr(rels) c_expr = parser.tree(query).toCode() 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)) return True except Exception as inst: traceback.print_exc(file=sys.stdout) 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.pretty_string(tty=True)) print(colorize("Result", COLOR_RED)) print(result.pretty_string(tty=True)) print(colorize("Optimized result", COLOR_RED)) print(o_result.pretty_string(tty=True)) 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
def run_test(testname): '''Runs a specific test executing the file testname.query and comparing the result with testname.result The query will be executed both unoptimized and optimized''' print "Running test: " + colorize(testname, COLOR_MAGENTA) query = None expr = None o_query = None o_expr = None result_rel = None result = None o_result = None try: result_rel = relation.relation('%s%s.result' % (tests_path, testname)) query = unicode( readfile('%s%s.query' % (tests_path, testname)).strip(), 'utf8') o_query = optimizer.optimize_all(query, rels) expr = parser.parse(query) # Converting expression to python string result = eval(expr, rels) # Evaluating the expression o_expr = parser.parse( o_query) # Converting expression to python string o_result = eval(o_expr, rels) # Evaluating the expression c_expr = parser.tree(query).toCode() # Converting to python code 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) return True except Exception as 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) return False
def run_test(testname): '''Runs a specific test executing the file testname.query and comparing the result with testname.result The query will be executed both unoptimized and optimized''' print ("Running test: " + colorize(testname, COLOR_MAGENTA)) query = None expr = None o_query = None o_expr = None result_rel = None result = None o_result = None try: result_rel = relation.relation('%s%s.result' % (tests_path, testname)) 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 result = eval(expr, rels) # Evaluating the expression o_expr = parser.parse( o_query) # Converting expression to python string o_result = eval(o_expr, rels) # Evaluating the expression c_expr = parser.tree(query).toCode() # Converting to python code 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)) return True except Exception as 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)) return False
def run_test(testname): '''Runs a specific test executing the file testname.query and comparing the result with testname.result The query will be executed both unoptimized and optimized''' print "Running test: " + colored(testname, 'magenta') query = None expr = None o_query = None o_expr = None result_rel = None result = None o_result = None try: result_rel = relation.relation('%s%s.result' % (tests_path, testname)) query = readfile('%s%s.query' % (tests_path, testname)).strip() query = unicode(query, 'utf-8') expr = parser.parse(query) #Converting expression to python code o_query = optimizer.optimize_all(query, rels) result = eval(expr, rels) #Evaluating the expression o_expr = parser.parse(o_query) #Converting expression to python code o_result = eval(o_expr, rels) #Evaluating the expression if (o_result == result_rel) and (result == result_rel): print colored('Test passed', 'green') return True except Exception as inst: print inst, "------" pass print colored('ERROR', 'red') print "Query: %s -> %s" % (query, expr) print "Optimized query: %s -> %s" % (o_query, o_expr) print colored('=====================================', 'red') print "\033[33;1mExpected result\033[0m" print result_rel print "\033[33;1mResult\033[0m" print result print "\033[33;1mOptimized result\033[0m" print o_result print "\033[33;1moptimized result match\033[0m", result_rel == o_result print "\033[33;1mresult match \033[0m", result == result_rel print colored('=====================================', 'red') return False
def execute(self): '''Executes the query''' query = compatibility.get_py_str(self.ui.txtQuery.text()) res_rel = compatibility.get_py_str( self.ui.txtResult.text()) # result relation's name if not rtypes.is_valid_relation_name(res_rel): QtGui.QMessageBox.information(self, QtGui.QApplication.translate( "Form", "Error"), QtGui.QApplication.translate("Form", "Wrong name for destination relation.")) return try: # Converting string to utf8 and then from qstring to normal string expr = parser.parse(query) # Converting expression to python code print query, "-->", expr # Printing debug result = eval(expr, self.relations) # Evaluating the expression self.relations[ res_rel] = result # Add the relation to the dictionary self.updateRelations() # update the list self.selectedRelation = result self.showRelation(self.selectedRelation) # Show the result in the table except Exception, e: #print e.__unicode__() QtGui.QMessageBox.information(None, QtGui.QApplication.translate("Form", "Error"), u"%s\n%s" % (QtGui.QApplication.translate("Form", "Check your query!"), e.__unicode__())) return
def exec_query(command): '''This function executes a query and prints the result on the screen if the command terminates with ";" the result will not be printed ''' # If it terminates with ; doesn't print the result if command.endswith(';'): command = command[:-1] printrel = False else: printrel = True # Performs replacements for weird operators command = replacements(command) # Finds the name in where to save the query parts = command.split('=', 1) relname,query = maintenance.UserInterface.split_query(command) # Execute query try: pyquery = parser.parse(query) result = pyquery(relations) printtty(colorize("-> query: %s" % pyquery, 0x00ff00)) if printrel: print () print (result) relations[relname] = result completer.add_completion(relname) except Exception as e: print (colorize(str(e), ERROR_COLOR))
def _run_multiline(self): query = self.ui.txtMultiQuery.toPlainText() self.settings.setValue('multiline/query', query) queries = query.split('\n') for query in queries: if query.strip() == '': continue parts = query.split('=', 1) parts[0] = parts[0].strip() if len(parts) > 1 and rtypes.is_valid_relation_name(parts[0].strip()): relname = parts[0].strip() query = parts[1] else: relname = 'last_' try: expr = parser.parse(query) print ('%s <- %s' % (relname, expr)) result = eval(expr, self.relations) self.relations[relname] = result except Exception as e: print(str(e)) QtWidgets.QMessageBox.information(None, QtWidgets.QApplication.translate("Form", "Error"), u"%s\n%s" % (QtWidgets.QApplication.translate("Form", "Check your query!"), str(e))) break self.updateRelations() # update the list self.selectedRelation = result self.showRelation(self.selectedRelation)
def execute(self, query: str, relname: str = 'last_') -> Relation: '''Executes a query, returns the result and if relname is not None, adds the result to the dictionary, with the name given in relname.''' if not is_valid_relation_name(relname): raise Exception(_('Invalid name for destination relation')) expr = parser.parse(query) result = expr(self.relations) self.relations[relname] = result return result
def execute(self, query, relname='last_'): '''Executes a query, returns the result and if relname is not None, adds the result to the dictionary, with the name given in relname.''' if not is_valid_relation_name(relname): raise Exception('Invalid name for destination relation') expr = parser.parse(query) result = expr(self.relations) self.relations[relname] = result return result
def exec_query(command): '''This function executes a query and prints the result on the screen if the command terminates with ";" the result will not be printed ''' #If it terminates with ; doesn't print the result if command.endswith(';'): command = command[:-1] printrel = False else: printrel = True # RZ avisamos que estamos enviando un utf-8 para que no se complique command = command.encode('utf-8') #Performs replacements for weird operators command = replacements(command) #Finds the name in where to save the query parts = command.split('=', 1) if len(parts) > 1 and rtypes.is_valid_relation_name(parts[0]): relname = parts[0] query = parts[1] else: relname = 'last_' query = command query = unicode(query, 'utf-8') #Execute query try: pyquery = parser.parse(query) result = eval(pyquery, relations) out = "" #out = colored("-> query: %s" % pyquery, 'green') if printrel: out += "" out += str(result) relations[relname] = result completer.add_completion(relname) return out except Exception, e: print e return colored(e, 'red')
def execute(self): '''Executes the query''' if self.multiline: return self._run_multiline() #Single line query query = self.ui.txtQuery.text() res_rel = self.ui.txtResult.text() # result relation's name if not rtypes.is_valid_relation_name(res_rel): QtWidgets.QMessageBox.information(self, QtWidgets.QApplication.translate( "Form", "Error"), QtWidgets.QApplication.translate("Form", "Wrong name for destination relation.")) return try: # Converting string to utf8 and then from qstring to normal string expr = parser.parse(query) # Converting expression to python code print (query, "-->", expr) # Printing debug result = eval(expr, self.relations) # Evaluating the expression self.relations[ res_rel] = result # Add the relation to the dictionary self.updateRelations() # update the list self.selectedRelation = result self.showRelation(self.selectedRelation) # Show the result in the table except Exception as e: print (str(e)) QtWidgets.QMessageBox.information(None, QtWidgets.QApplication.translate("Form", "Error"), u"%s\n%s" % (QtWidgets.QApplication.translate("Form", "Check your query!"), str(e))) return # Adds to history item = u'%s = %s' % ( self.ui.txtResult.text(), self.ui.txtQuery.text() ) hitem = QtWidgets.QListWidgetItem(None, 0) hitem.setText(item) self.ui.lstHistory.addItem(hitem) self.ui.lstHistory.setCurrentItem(hitem) self.qcounter += 1 self.ui.txtResult.setText(u"_last%d" % self.qcounter) # Sets the result relation name to none
def exec_query(command): '''This function executes a query and prints the result on the screen if the command terminates with ";" the result will not be printed ''' command = unicode(command, 'utf-8') # If it terminates with ; doesn't print the result if command.endswith(';'): command = command[:-1] printrel = False else: printrel = True # Performs replacements for weird operators command = replacements(command) # Finds the name in where to save the query parts = command.split('=', 1) if len(parts) > 1 and rtypes.is_valid_relation_name(parts[0]): relname = parts[0] query = parts[1] else: relname = 'last_' query = command # Execute query try: pyquery = parser.parse(query) result = eval(pyquery, relations) print colorize("-> query: %s" % pyquery.encode('utf-8'), 0x00ff00) if printrel: print print result relations[relname] = result completer.add_completion(relname) except Exception, e: print colorize(str(e), ERROR_COLOR)