from test.helpers.psqldb import PgSQLConnection def find_between(s, first, last): try: start = s.index(first) + len(first) end = s.index(last, start) return s[start:end] except ValueError: return "" class TestSrv_Database(object): ''' TestSrv Database Helper ''' __sqlObject = None __xml_setup = None def __init__(self): pass def open(self, xmlObj): self.__xml_setup = xmlObj self.__sqlObject = PgSQLConnection() return self.__sqlObject.db_connect(self.__xml_setup.getdbConnectionStr()) def create_board(self, processor_id, model_id, variant, station, bmac=None): '''create a new board''' if bmac is None: sql = "SELECT * FROM isee.f_create_board('{}', '{}', '{}', NULL, '{}');".format(processor_id, model_id, variant, station) else: sql = "SELECT * FROM isee.f_create_board('{}', '{}', '{}', '{}', '{}');".format(processor_id, model_id, variant, bmac, station) # print('>>>' + sql) try: res = self.__sqlObject.db_execute_query(sql) # print(res) return res[0][0] except Exception as err: r = find_between(str(err), '#', '#') # print(r) return None def get_tests_list(self, board_uuid): '''get the board test list''' sql = "SELECT * FROM isee.f_get_tests_list('{}')".format(board_uuid) # print('>>>' + sql) try: res = self.__sqlObject.db_execute_query(sql) # print(res) return res except Exception as err: r = find_between(str(err), '#', '#') # print(r) return None def get_test_params_list(self, testid): sql = "SELECT * FROM isee.f_get_test_params_list({})".format(testid) # print('>>>' + sql) try: res = self.__sqlObject.db_execute_query(sql) # print(res) return res except Exception as err: r = find_between(str(err), '#', '#') # print(r) return None def open_test(self, board_uuid): sql = "SELECT * FROM isee.f_open_test('{}')".format(board_uuid) # print('>>>' + sql) try: res = self.__sqlObject.db_execute_query(sql) # print(res) return res[0][0] except Exception as err: r = find_between(str(err), '#', '#') # print(r) return None def run_test(self, testid_ctl, testid): sql = "SELECT isee.f_run_test({},{})".format(testid_ctl, testid) # print('>>>' + sql) try: self.__sqlObject.db_execute_query(sql) except Exception as err: r = find_between(str(err), '#', '#') # print(r) return None def finish_test(self, testid_ctl, testid, newstatus): sql = "SELECT isee.f_finish_test({},{},'{}')".format(testid_ctl, testid, newstatus) # print('>>>' + sql) try: self.__sqlObject.db_execute_query(sql) except Exception as err: r = find_between(str(err), '#', '#') # print(r) return None def get_board_variables(self, board_uuid): sql = "SELECT * FROM isee.f_get_boardvariables('{}')".format(board_uuid) # print('>>>' + sql) try: res = self.__sqlObject.db_execute_query(sql) # print(res) return res except Exception as err: r = find_between(str(err), '#', '#') # print(r) return None def get_board_macaddr(self, board_uuid): sql = "SELECT * FROM isee.f_get_board_macaddr('{}')".format(board_uuid) # print('>>>' + sql) try: res = self.__sqlObject.db_execute_query(sql) # print(res) return res[0][0] except Exception as err: r = find_between(str(err), '#', '#') # print(r) return None def create_process(self, testid_ctl): sql = "SELECT * FROM isee.f_create_process({})".format(testid_ctl) # print('>>>' + sql) try: res = self.__sqlObject.db_execute_query(sql) # print(res) return res[0][0] except Exception as err: r = find_between(str(err), '#', '#') # print(r) return None def create_task_result(self, taskid_ctl, name, newstatus): sql = "SELECT isee.f_create_task_result({},'{}','{}')".format(taskid_ctl, name, newstatus) # print('>>>' + sql) try: self.__sqlObject.db_execute_query(sql) except Exception as err: r = find_between(str(err), '#', '#') # print(r) return None def update_taskctl_status(self, taskid_ctl, newstatus): sql = "SELECT isee.f_update_taskctl_status({},'{}')".format(taskid_ctl, newstatus) # print('>>>' + sql) try: self.__sqlObject.db_execute_query(sql) except Exception as err: r = find_between(str(err), '#', '#') # print(r) return None