def test_Dicts(self): gotException = False try: qlDicts = QueryableListDicts(self.dataDicts) found = qlDicts.filter(a='one') except Exception as e: gotException = e assert gotException is False, 'Got Exception for QueryableListDicts when should not have: %s%s' % ( str(type(e)), str(e)) assert len(found) == 2, 'Did not find correct number of items' gotException = False try: dDicts = QueryableListObjs(self.dataDicts) found = dDicts.filter(a='one') except Exception as e: # I personally think this should raise an exception, so test is written like this, # but it would be too performant loss to check every time. gotException = e #assert gotException is not False, 'Expected to get exception, but did not.' assert len( found ) == 0, 'Expected not to find any items when using QueryableListDicts with list of objs'
def test_sub(self): testHasItems = self._testHasItems set1Nums = [0, 1, 5, 1, 3] set2Nums = [3, 1] set3Nums = [0, 5] qObjs1 = QueryableListObjs(gdo(self, set1Nums)) qObjs2 = QueryableListObjs(gdo(self, set2Nums)) subRes = qObjs1 - qObjs2 # Assert sub removes items in 2 from 1 testHasItems(subRes, gdo(self, set3Nums), mkOpStr('o', set1Nums, '-', set2Nums, set3Nums)) # Assert originals are unchanged testHasItems(qObjs1, gdo(self, set1Nums), mkNumSet('o', set1Nums)) testHasItems(qObjs2, gdo(self, set2Nums), mkNumSet('o', set2Nums)) qDicts1 = QueryableListDicts(gdd(self, set1Nums)) qDicts2 = QueryableListDicts(gdd(self, set2Nums)) subRes = qDicts1 - qDicts2 # Assert sub removes items in 2 from 1 testHasItems(subRes, gdd(self, set3Nums), mkOpStr('d', set1Nums, '-', set2Nums, set3Nums)) # Assert originals are unchanged testHasItems(qDicts1, gdd(self, set1Nums), mkNumSet('d', set1Nums)) testHasItems(qDicts2, gdd(self, set2Nums), mkNumSet('d', set2Nums))
def test_add(self): testHasItems = self._testHasItems set1Nums = [0, 1] set2Nums = [3, 5] set3Nums = [0, 1, 3, 5] qObjs1 = QueryableListObjs(gdo(self, set1Nums)) qObjs2 = QueryableListObjs(gdo(self, set2Nums)) addRes = qObjs1 + qObjs2 # Assert add appends 2 to 1 testHasItems(addRes, gdo(self, set3Nums), mkOpStr('o', set1Nums, '+', set2Nums, set3Nums)) # Assert originals are unchanged testHasItems(qObjs1, gdo(self, set1Nums)) testHasItems(qObjs2, gdo(self, set2Nums)) qDicts1 = QueryableListDicts(gdd(self, set1Nums)) qDicts2 = QueryableListDicts(gdd(self, set2Nums)) addRes = qDicts1 + qDicts2 # Assert add appends 2 to 1 testHasItems(addRes, gdd(self, set3Nums), mkOpStr('d', set1Nums, '+', set2Nums, set3Nums)) # Assert originals are unchanged testHasItems(qDicts1, gdd(self, set1Nums), mkNumSet('d', set1Nums)) testHasItems(qDicts2, gdd(self, set2Nums), mkNumSet('d', set2Nums))
def test_isub(self): testHasItems = self._testHasItems set1Nums = [0, 1, 5, 1, 3] # A set2Nums = [3, 1] # B set3Nums = [0, 5] # = C qObjs1 = QueryableListObjs(gdo(self, [0, 1, 5, 1, 3])) qObjs2 = QueryableListObjs(gdo(self, [3, 1])) origRef1 = qObjs1 origID1 = id(qObjs1) qObjs1 -= qObjs2 # Assert sub removes items from 1 that are in 2 testHasItems(qObjs1, gdo(self, set3Nums), mkOpStr('o', set1Nums, '-', set2Nums, set3Nums)) # Assert original1 is modified, 2 is not testHasItems(qObjs1, gdo(self, set3Nums), 'o1 = ' + mkNumSet('o', set3Nums)) testHasItems(qObjs2, gdo(self, set2Nums), 'o2 = ' + mkNumSet('o', set2Nums)) afterID1 = id(qObjs1) assert origID1 == afterID1, 'Expected id to not change after iadd (i.e. a copy was not made.)\nBefore = %d\nAfter = %d' % ( origID1, afterID1) testHasItems(origRef1, gdo(self, set3Nums), 'o1ref = ' + mkNumSet('o', set3Nums)) qDicts1 = QueryableListDicts(gdd(self, set1Nums)) qDicts2 = QueryableListDicts(gdd(self, set2Nums)) origRef1 = qDicts1 origID1 = id(qDicts1) qDicts1 -= qDicts2 # Assert sub removes items from 1 that are in 2 testHasItems(qDicts1, gdd(self, set3Nums), mkOpStr('d', set1Nums, '-=', set2Nums)) # Assert original1 is modified, 2 is not testHasItems(qDicts1, gdd(self, set3Nums), 'd1 = ' + mkNumSet('d', set3Nums)) testHasItems(qDicts2, gdd(self, set2Nums), 'd1 = ' + mkNumSet('d', set2Nums)) afterID1 = id(qDicts1) assert origID1 == afterID1, 'Expected id to not change after iadd (i.e. a copy was not made.)\nBefore = %d\nAfter = %d' % ( origID1, afterID1) testHasItems(origRef1, gdd(self, set3Nums), 'd1ref = ' + mkNumSet('d', set3Nums))
def test_iadd(self): testHasItems = self._testHasItems set1Nums = [0, 1] set2Nums = [3, 5] set3Nums = [0, 1, 3, 5] qObjs1 = QueryableListObjs(gdo(self, set1Nums)) qObjs2 = QueryableListObjs(gdo(self, set2Nums)) origRef1 = qObjs1 origID1 = id(qObjs1) qObjs1 += qObjs2 # Assert add appends 2 to 1 testHasItems(qObjs1, gdo(self, set3Nums), mkOpStr('o', set1Nums, '+=', set2Nums)) # Assert original1 is modified, 2 is not testHasItems(qObjs1, gdo(self, set3Nums), mkNumSet('o', set3Nums)) testHasItems(qObjs2, gdo(self, set2Nums), mkNumSet('o', set2Nums)) afterID1 = id(qObjs1) assert origID1 == afterID1, 'Expected id to not change after iadd (i.e. a copy was not made.)\nBefore = %d\nAfter = %d' % ( origID1, afterID1) testHasItems(origRef1, gdo(self, set3Nums), mkNumSet('o', set3Nums)) qDicts1 = QueryableListDicts(gdd(self, set1Nums)) qDicts2 = QueryableListDicts(gdd(self, set2Nums)) origRef1 = qDicts1 origID1 = id(qDicts1) qDicts1 += qDicts2 # Assert add appends 2 to 1 testHasItems(qDicts1, gdd(self, set3Nums), mkOpStr('d', set1Nums, '+', set2Nums, set3Nums)) # Assert original1 is modified, 2 is not testHasItems(qDicts1, gdd(self, set3Nums), mkNumSet('d', set3Nums)) testHasItems(qDicts2, gdd(self, set2Nums), mkNumSet('d', set2Nums)) afterID1 = id(qDicts1) assert origID1 == afterID1, 'Expected id to not change after iadd (i.e. a copy was not made.)\nBefore = %d\nAfter = %d' % ( origID1, afterID1) testHasItems(origRef1, gdd(self, set3Nums), mkNumSet('d', set3Nums))
def test_performance(self): # Test __eq and = both qlObjs = QueryableListObjs(self.dataObjs) qlDicts = QueryableListDicts(self.dataDicts) start = time.time() for i in range(1, NUMI + 1, 1): filterA = 'x' * ((i * 7) % 5) filterA2 = 'x' * ((i * 13) % 5) filterB = 'y' * ((i * 11) % 5) filterB2 = 'y' * ((i * 3) % 5) filterNums1 = [(i * 100) % 5, (i * 33) % 5, (i * 121) % 5] filterNums2 = [(i * 177) % 5, (i * 62) % 5, (i * 101) % 5] res = qlObjs.filter( a=filterA, a__ne=filterB, b__in=[filterB[:min(len(filterB) - 1, 1)], filterB]).filterOr(num__gt=filterNums1[i % 3], num__ne=filterNums2[i % 3]) end = time.time() print("Total time: %f" % (end - start, ))
def test_sortDictsByInt(self): dataDicts = self.dataDicts qlDicts = QueryableListDicts(self.dataDicts) sortedByA = qlDicts.sort_by('a') sortedByAList = list(sortedByA) expectedList = [dataDicts[0], dataDicts[3], dataDicts[2], dataDicts[1]] assert sortedByAList == expectedList, 'Sort by field "a" failed to return expected order.\nGot: %s\nExpected: %s\n' % ( repr(self._get_list_of_values(sortedByAList, 'a')), repr(self._get_list_of_values(expectedList, 'a'))) assert dataDicts == self.dataDictsCopy, 'Expected sort_by to not modify original list' assert isinstance( sortedByA, QueryableListDicts ), 'Expected return to be a QueryableListDicts instance. Got: %s' % ( sortedByA.__class__.__name__, ) sortedByARev = qlDicts.sort_by('a', reverse=True) sortedByARevList = list(sortedByARev) expectedRevList = list(reversed(expectedList)) assert sortedByARevList == expectedRevList, 'Reverse sort by field "a" failed to return expected order.\nGot: %s\nExpected: %s\n' % ( repr(self._get_list_of_values(sortedByARevList, 'a')), repr(self._get_list_of_values(expectedRevList, 'a'))) assert dataDicts == self.dataDictsCopy, 'Expected sort_by to not modify original list' assert isinstance( sortedByARev, QueryableListDicts ), 'Expected return to be a QueryableListDicts instance. Got: %s' % ( sortedByARev.__class__.__name__, )
def test_Dicts(self): gotException = False try: qlDicts = QueryableListDicts(self.dataDicts) found = qlDicts.filter(a='one') except Exception as e: gotException = e assert gotException is False, 'Got Exception for QueryableListDicts when should not have: %s%s' %(str(type(e)), str(e)) assert len(found) == 2, 'Did not find correct number of items' gotException = False try: dDicts = QueryableListObjs(self.dataDicts) found = dDicts.filter(a='one') except Exception as e: # I personally think this should raise an exception, so test is written like this, # but it would be too performant loss to check every time. gotException = e #assert gotException is not False, 'Expected to get exception, but did not.' assert len(found) == 0, 'Expected not to find any items when using QueryableListDicts with list of objs'