def test_GetData_where(self): d = DbUtils.GetData(self.dbName, 'ten_elements_dups', forceList=0, randomAccess=0, whereString='id<4') self.assertEqual(len(list(d)), 4) self.assertTrue(all(x[0] < 4 for x in d)) d = DbUtils.GetData(self.dbName, 'ten_elements_dups', forceList=0, randomAccess=0, whereString='id<10') self.assertEqual(len(list(d)), 10) self.assertTrue(all(x[0] < 10 for x in d)) d = DbUtils.GetData(self.dbName, 'ten_elements_dups', removeDups=1, forceList=True) self.assertEqual(len(list(d)), 10)
def GetDataCount(self, table=None, where='', join='', **kwargs): """ returns a count of the number of results a query will return **Arguments** - table: (optional) the table to use - where: the SQL where clause to be used with the DB query - join: the SQL join clause to be used with the DB query **Returns** an int **Notes** - this uses _DbUtils.GetData_ """ table = table or self.tableName return DbUtils.GetData(self.dbName, table, fieldString='count(*)', whereString=where, cn=self.cn, user=self.user, password=self.password, join=join, forceList=0)[0][0]
def GetData(self,table=None,fields='*',where='',removeDups=-1,join='', transform=None,randomAccess=1,**kwargs): """ a more flexible method to get a set of data from a table **Arguments** - table: (optional) the table to use - fields: a string with the names of the fields to be extracted, this should be a comma delimited list - where: the SQL where clause to be used with the DB query - removeDups: indicates which column should be used to recognize duplicates in the data. -1 for no duplicate removal. **Returns** a list of the data **Notes** - this uses _DbUtils.GetData_ """ if table is None: table = self.tableName kwargs['forceList'] = kwargs.get('forceList',0) return DbUtils.GetData(self.dbName,table,fieldString=fields,whereString=where, user=self.user,password=self.password,removeDups=removeDups, join=join,cn=self.cn, transform=transform,randomAccess=randomAccess,**kwargs)
def testGetData1(self): """ basic functionality """ d = DbUtils.GetData(self.dbName, 'ten_elements', forceList=1) assert len(d) == 10 assert tuple(d[0]) == (0, 11) assert tuple(d[2]) == (4, 31) with self.assertRaisesRegexp(IndexError, ""): d[11]
def testGetData2(self): """ using a RandomAccessDbResultSet """ d = DbUtils.GetData(self.dbName, 'ten_elements', forceList=0, randomAccess=1) assert tuple(d[0]) == (0, 11) assert tuple(d[2]) == (4, 31) assert len(d) == 10 with self.assertRaisesRegexp(IndexError, ""): d[11]
def testGetData4(self): """ using a RandomAccessDbResultSet with a Transform """ fn = lambda x:(x[0],x[1]*2) d = DbUtils.GetData(self.dbName,'ten_elements',forceList=0,randomAccess=1, transform=fn) assert tuple(d[0])==(0,22) assert tuple(d[2])==(4,62) assert len(d)==10 with self.assertRaisesRegexp(IndexError, ""): d[11]
def testGetData3(self): """ using a DbResultSet """ d = DbUtils.GetData(self.dbName, 'ten_elements', forceList=0, randomAccess=0) with self.assertRaisesRegexp(TypeError, ""): len(d) rs = [] for thing in d: rs.append(thing) assert len(rs) == 10 assert tuple(rs[0]) == (0, 11) assert tuple(rs[2]) == (4, 31)
def testGetData1(self): """ basic functionality """ d = DbUtils.GetData(self.dbName, 'ten_elements', forceList=1) assert len(d) == 10 assert tuple(d[0]) == (0, 11) assert tuple(d[2]) == (4, 31) try: d[11] except IndexError: pass except: assert 0, 'bad exception type raised' else: assert 0, 'failed to raise expected exception'
def testGetData5(self): """ using a DbResultSet with a Transform """ fn = lambda x:(x[0],x[1]*2) d = DbUtils.GetData(self.dbName,'ten_elements',forceList=0,randomAccess=0, transform=fn) with self.assertRaisesRegexp(TypeError, ""): len(d) rs = [] for thing in d: rs.append(thing) assert len(rs)==10 assert tuple(rs[0])==(0,22) assert tuple(rs[2])==(4,62)
def testGetData2(self): """ using a RandomAccessDbResultSet """ d = DbUtils.GetData(self.dbName, 'ten_elements', forceList=0, randomAccess=1) assert tuple(d[0]) == (0, 11) assert tuple(d[2]) == (4, 31) assert len(d) == 10 try: d[11] except IndexError: pass except: assert 0, 'bad exception type raised' else: assert 0, 'failed to raise expected exception'
def testGetData4(self): """ using a RandomAccessDbResultSet with a Transform """ fn = lambda x: (x[0], x[1] * 2) d = DbUtils.GetData(self.dbName, 'ten_elements', forceList=0, randomAccess=1, transform=fn) assert tuple(d[0]) == (0, 22) assert tuple(d[2]) == (4, 62) assert len(d) == 10 try: d[11] except IndexError: pass except: assert 0, 'bad exception type raised' else: assert 0, 'failed to raise expected exception'
def testGetData3(self): """ using a DbResultSet """ d = DbUtils.GetData(self.dbName, 'ten_elements', forceList=0, randomAccess=0) try: len(d) except TypeError: pass except: assert 0, 'bad exception type raised' else: assert 0, 'failed to raise expected exception' rs = [] for thing in d: rs.append(thing) assert len(rs) == 10 assert tuple(rs[0]) == (0, 11) assert tuple(rs[2]) == (4, 31)
def testGetData5(self): """ using a DbResultSet with a Transform """ fn = lambda x: (x[0], x[1] * 2) d = DbUtils.GetData(self.dbName, 'ten_elements', forceList=0, randomAccess=0, transform=fn) try: len(d) except TypeError: pass except: assert 0, 'bad exception type raised' else: assert 0, 'failed to raise expected exception' rs = [] for thing in d: rs.append(thing) assert len(rs) == 10 assert tuple(rs[0]) == (0, 22) assert tuple(rs[2]) == (4, 62)