def test_pre_write_to_full_table_q2(self): qids.reset_atomic_qid_seen() qids.reset_full_qid_seen() entry = qr.QueryResultBase._pre_write_to_full_table( self._query2, self._result2) matching_record_ids = self._result2[rdb.DBF_MATCHINGRECORDIDS] matching_records = len(matching_record_ids) gold = \ { rdb.DBF_FQID : self._query2[qs.QRY_QID], rdb.DBF_CAT : self._query2[qs.QRY_CAT], rdb.DBF_SUBCAT : self._query2[qs.QRY_SUBCAT], rdb.DBF_NUMRECORDS : self._query2[qs.QRY_DBNUMRECORDS], rdb.DBF_RECORDSIZE : self._query2[qs.QRY_DBRECORDSIZE], rdb.DBF_WHERECLAUSE : self._query2[qs.QRY_WHERECLAUSE], rdb.DBF_IBM1SUPPORTED : "IBM1" in self._query2[qs.QRY_PERF], rdb.DBF_IBM2SUPPORTED : "IBM2" in self._query2[qs.QRY_PERF], rdb.DBF_COLUMBIASUPPORTED : "COL" in self._query2[qs.QRY_PERF], rdb.DBF_NUMMATCHINGRECORDS : matching_records, rdb.DBF_MATCHINGRECORDIDS : matching_record_ids, rdb.DBF_P1NEGATEDTERM : self._query2[qs.QRY_NEGATEDTERMS], rdb.DBF_P1NUMTERMSPERCLAUSE : self._query2[qs.QRY_NUMTERMSPERCLAUSE], rdb.DBF_P1NUMCLAUSES : self._query2[qs.QRY_NUMCLAUSES] } gold[rdb.DBF_SELECTSTAR] = entry[rdb.DBF_SELECTSTAR] self.assertEqual(entry, gold)
def test_process_repeat_query(self): ''' test process_query with repeats''' qids.reset_atomic_qid_seen() qids.reset_full_qid_seen() query_result = qr.P9AlarmQueryResult(self.query1, self.result1, None, True) (atomic_entry, full_entry, full_to_atomic_entry, full_to_full_entry) = \ query_result.process_query() full_entry[rdb.DBF_SELECTSTAR] = True self.assertEqual(atomic_entry, self.query1_atomic_entry) self.assertEqual(full_entry, self.query1_full_entry) self.assertEqual(full_to_atomic_entry, self.query1_full_to_atomic_entry) self.assertEqual(full_to_full_entry, {}) # test again but pass in None for results query_result = qr.EqualityQueryResult(self.query1, None, None, True) (atomic_entry, full_entry, full_to_atomic_entry, full_to_full_entry) = \ query_result.process_query() self.assertEqual(atomic_entry, {}) self.assertEqual(full_entry, {}) self.assertEqual(full_to_atomic_entry, {}) self.assertEqual(full_to_full_entry, {})
def test_process_query(self): ''' test process_query ''' qids.reset_atomic_qid_seen() qids.reset_full_qid_seen() query_result = qr.P3P4P6P7QueryResult(self.__query1, self.__result1, None, True) (atomic_entry, full_entry, full_to_atomic_entry, full_to_full_entry) = \ query_result.process_query() full_entry[rdb.DBF_SELECTSTAR] = True self.assertEqual(atomic_entry, self.__query1_atomic_entry) self.assertEqual(full_entry, self.__query1_full_entry) self.assertEqual(full_to_atomic_entry, self.__query1_full_to_atomic_entry) self.assertEqual(full_to_full_entry, {}) # test again but pass in None for results qids.reset_atomic_qid_seen() qids.reset_full_qid_seen() query_result = qr.P3P4P6P7QueryResult(self.__query1, None, None, True) (atomic_entry, full_entry, full_to_atomic_entry, full_to_full_entry) = \ query_result.process_query() full_entry[rdb.DBF_SELECTSTAR] = True self.assertEqual(atomic_entry, self.__query1_atomic_entry_no_matches) self.assertEqual(full_entry, self.__query1_full_entry_no_matches) self.assertEqual(full_to_atomic_entry, self.__query1_full_to_atomic_entry) self.assertEqual(full_to_full_entry, {})
def test_write_query(self): ''' test write_query ''' qids.reset_atomic_qid_seen() qids.reset_full_qid_seen() db_name = ':memory:' db_object = ta1_database.Ta1ResultsDB(db_name) query_result = qr.P9AlarmQueryResult(self.query1, self.result1, db_object, True) query_result.write_query() db_object.close()
def testPreWriteToFullTable(self): qids.reset_full_qid_seen() count = 0 for x in xrange(10): self.__query[qs.QRY_QID] = x full_entry = qr.QueryResultBase._pre_write_to_full_table( self.__query, self.__result) if full_entry[rdb.DBF_SELECTSTAR]: count += 1 self.assertEqual(count, 2)
def testQueryResult(self): qids.reset_full_qid_seen() count = 0 for x in xrange(10): self.__query[qs.QRY_QID] = x query_result = qr.EqualityQueryResult(self.__query, self.__result, None, True) (_, full_entry, _, _) = query_result.process_query() if full_entry[rdb.DBF_SELECTSTAR]: count += 1 self.assertEqual(count, 2)
def test_process_query(self): ''' test process_query ''' qids.reset_atomic_qid_seen() qids.reset_full_qid_seen() query_result = qr.P9AlarmQueryResult(self.query1, self.result1, None, True) (atomic_entry, full_entry, full_to_atomic_entry, full_to_full_entry) = \ query_result.process_query() full_entry[rdb.DBF_SELECTSTAR] = True self.assertEqual(atomic_entry, self.query1_atomic_entry) self.assertEqual(full_entry, self.query1_full_entry) self.assertEqual(full_to_atomic_entry, self.query1_full_to_atomic_entry) self.assertEqual(full_to_full_entry, {})
def test_write_to_full_table_q1(self): qids.reset_atomic_qid_seen() qids.reset_full_qid_seen() db_name = ':memory:' db_object = ta1_database.Ta1ResultsDB(db_name) qr.QueryResultBase.write_to_full_table(self._query1, self._result1, db_object) db_object._execute("SELECT * FROM " + rdb.DBF_TABLENAME) rows = db_object._fetchall() self.assertEqual(len(rows), 1) fields = [ \ (rdb.DBF_FQID, 1), (rdb.DBF_CAT, self._query1[qs.QRY_CAT]), (rdb.DBF_SUBCAT, self._query1[qs.QRY_SUBCAT]), (rdb.DBF_NUMRECORDS, self._query1[qs.QRY_DBNUMRECORDS]), (rdb.DBF_RECORDSIZE, self._query1[qs.QRY_DBRECORDSIZE]), (rdb.DBF_WHERECLAUSE, self._query1[qs.QRY_WHERECLAUSE]), (rdb.DBF_NUMMATCHINGRECORDS, 2), (rdb.DBF_MATCHINGRECORDIDS, "1|3" ), (rdb.DBF_P1NUMTERMSPERCLAUSE, self._query1[qs.QRY_NUMTERMSPERCLAUSE]), (rdb.DBF_P1ANDNUMRECORDSMATCHINGFIRSTTERM, self._result1[qs.QRY_NUMRECORDSMATCHINGFIRSTTERM]), (rdb.DBF_P1NUMCLAUSES, self._query1[qs.QRY_NUMCLAUSES]), (rdb.DBF_IBM1SUPPORTED, "IBM1" in self._query1[qs.QRY_PERF]), (rdb.DBF_IBM2SUPPORTED, "IBM2" in self._query1[qs.QRY_PERF]), (rdb.DBF_COLUMBIASUPPORTED, "COL" in self._query1[qs.QRY_PERF]) ] for field, gold in fields: cmd = "SELECT " + field + " FROM " + rdb.DBF_TABLENAME db_object._execute(cmd) row = db_object._fetchone() # note: row is a tuple, we want the first element self.assertEqual(row[0], gold) db_object.close()
def setUp(self): ''' make a fake resultdb for testing ''' queries = [] results = [] query1 = { qs.QRY_CAT : 'P1', qs.QRY_PERF : 'IBM1', qs.QRY_SUBCAT : 'eqand', qs.QRY_ENUM : qs.CAT.P1_EQ_AND, qs.QRY_DBNUMRECORDS : 30, qs.QRY_DBRECORDSIZE : 1003, qs.QRY_QID : 1, qs.QRY_WHERECLAUSE : 'fname = nick AND lname = jones', qs.QRY_NUMTERMSPERCLAUSE : 3, qs.QRY_NUMCLAUSES : 2 } sub_result1 = { qs.QRY_QID : 2, rdb.DBF_MATCHINGRECORDIDS : set([1,3]) } sub_result2 = { qs.QRY_QID : 3, rdb.DBF_MATCHINGRECORDIDS : set([1,3,5]) } result1 = { qs.QRY_QID : 1, rdb.DBF_MATCHINGRECORDIDS : set([1,3]), qs.QRY_NUMRECORDSMATCHINGFIRSTTERM : 2, qs.QRY_SUBRESULTS : [sub_result1, sub_result2] } queries.append(query1) results.append(result1) query2 = { qs.QRY_CAT : 'P1', qs.QRY_SUBCAT : 'eqnot', qs.QRY_PERF : 'IBM1', qs.QRY_ENUM : qs.CAT.P1_EQ_NOT, qs.QRY_DBNUMRECORDS : 30, qs.QRY_DBRECORDSIZE : 1003, qs.QRY_QID : 2, qs.QRY_WHERECLAUSE : 'NOT(fname = nick) AND NOT(lname = jones)', qs.QRY_NEGATEDTERMS : set([0,1]), qs.QRY_NUMTERMSPERCLAUSE : 3, qs.QRY_NUMCLAUSES : 2 } result2 = { qs.QRY_QID : 2, rdb.DBF_MATCHINGRECORDIDS : set([1,3]), qs.QRY_NUMRECORDSMATCHINGFIRSTTERM : 2, rdb.DBF_P1NEGATEDTERM : set([0,1]), qs.QRY_SUBRESULTS : [sub_result1, sub_result2] } queries.append(query2) results.append(result2) # make a bunch of equality queries for num in range(20): ran_num = random.randint(1, 100000) qid = 100 + num name = 'fred' + str(num) matches = set(range(ran_num)) # meaningless set of numbers query = { qs.QRY_CAT : 'EQ', qs.QRY_SUBCAT : '', qs.QRY_PERF : 'IBM1', qs.QRY_DBNUMRECORDS : 30, qs.QRY_DBRECORDSIZE : 1003, qs.QRY_QID : qid, qs.QRY_WHERECLAUSE : 'fname = ' + name, qs.QRY_FIELD : 'FNAME', qs.QRY_FIELDTYPE : 'string', qs.QRY_VALUE : name } result = { qs.QRY_QID : qid, rdb.DBF_MATCHINGRECORDIDS : matches } queries.append(query) results.append(result) # make a bunch of equality queries for num in range(20): ran_num = random.randint(1, 2000) qid = 1000 + num name = 'dan' + str(num) matches = set(range(ran_num)) # meaningless set of numbers query = { qs.QRY_CAT : 'EQ', qs.QRY_SUBCAT : '', qs.QRY_PERF : 'IBM1', qs.QRY_DBNUMRECORDS : 30, qs.QRY_DBRECORDSIZE : 1003, qs.QRY_QID : qid, qs.QRY_WHERECLAUSE : 'fname = ' + name, qs.QRY_FIELD : 'FNAME', qs.QRY_FIELDTYPE : 'string', qs.QRY_VALUE : name } result = { qs.QRY_QID : qid, rdb.DBF_MATCHINGRECORDIDS : matches } queries.append(query) results.append(result) query3 = { qs.QRY_CAT : 'EQ', qs.QRY_SUBCAT : '', qs.QRY_PERF : 'IBM1', qs.QRY_DBNUMRECORDS : 30, qs.QRY_DBRECORDSIZE : 1003, qs.QRY_QID : 3, qs.QRY_WHERECLAUSE : 'fname = nick', qs.QRY_FIELD : 'FNAME', qs.QRY_FIELDTYPE : 'string', qs.QRY_VALUE : 'nick' } result3 = { qs.QRY_QID : 3, rdb.DBF_MATCHINGRECORDIDS : set([1,3]) } queries.append(query3) results.append(result3) query4 = { qs.QRY_CAT : 'P2', qs.QRY_SUBCAT : 'foo-range', qs.QRY_PERF : 'IBM1', qs.QRY_DBNUMRECORDS : 30, qs.QRY_DBRECORDSIZE : 1003, qs.QRY_QID : 4, qs.QRY_WHERECLAUSE : '100 <= foo <= 500', qs.QRY_FIELD : 'foo', qs.QRY_FIELDTYPE : 'integer', qs.QRY_LBOUND : 100, qs.QRY_UBOUND : 500, qs.QRY_RANGE : 4, qs.QRY_RANGECOVERAGE : 400} result4 = { qs.QRY_QID : 4, rdb.DBF_MATCHINGRECORDIDS : set([1,3]) } queries.append(query4) results.append(result4) query5 = { qs.QRY_CAT : 'P3', qs.QRY_SUBCAT : '', qs.QRY_PERF : 'IBM1', qs.QRY_DBNUMRECORDS : 33, qs.QRY_DBRECORDSIZE : 1003, qs.QRY_QID : 5, qs.QRY_WHERECLAUSE : 'CONTAINED_IN(notes1, \"dogs\")', qs.QRY_FIELD : 'notes1', qs.QRY_FIELDTYPE : 'text', qs.QRY_SEARCHFOR : 'dogs', qs.QRY_KEYWORDLEN : 4 } result5 = { qs.QRY_QID : 5, rdb.DBF_MATCHINGRECORDIDS : set([1,3]) } queries.append(query5) results.append(result5) qids.reset_full_qid_seen() db_name = ':memory:' db_object = ta1_database.Ta1ResultsDB(db_name) for query, result in zip(queries, results): qr.QueryResultBase.write_to_full_table(query, result, db_object) self.resultdb = db_object
def testFullQID(self): qids.reset_full_qid_seen() self.assertEqual(qids.full_qid_seen(100), False) self.assertEqual(qids.full_qid_seen(100), True) qids.reset_full_qid_seen() self.assertEqual(qids.full_qid_seen(100), False)