def test_equals(self): # Test __eq and = both qlObjs = QueryableListObjs(self.dataObjs) found = qlObjs.filter(a='six') assert len( found) == 1, 'Expected to find one item in query(=), found %d' % ( len(found), ) assert found[0].a == 'six' and found[ 0].c == 'eleven', 'Found wrong item from equals query' found = qlObjs.filter(a__eq='six') assert len( found ) == 1, 'Expected to find one item in query(__eq), found %d' % ( len(found), ) assert found[0].a == 'six' and found[ 0].c == 'eleven', 'Found wrong item from equals query' found = QueryableListObjs(self.dataObjs).filter(a__eq='one') assert len( found) == 2, 'Expected to find two items in query, found %d' % ( len(found), ) assert found.count( ) == 2, 'Expected count() to return 2 elements. Got: %d' % ( found.count(), )
def test_Objs(self): gotException = False try: qlObjs = QueryableListObjs(self.dataObjs) found = qlObjs.filter(a='one') except Exception as e: gotException = e assert gotException is False, 'Got Exception for QueryableListObjs 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: dObjs = QueryableListObjs(self.dataDicts) found = dObjs.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 QueryableListObjs with list of dicts'
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_copy(self): qlObjs = QueryableListObjs(self.dataObjs) assert copy.copy( qlObjs) == qlObjs, "Expected copy to equal the original" assert id(copy.copy(qlObjs)) != id( qlObjs), 'Expected id of copy to not euqla original' assert copy.deepcopy( qlObjs) != qlObjs, "Expected deepcopy to NOT equal the original" assert copy.deepcopy(qlObjs).count() == qlObjs.count( ), 'Expected deepcopy to have same number of items' aObjs = qlObjs.filter(a='one') assert copy.copy( aObjs ) == aObjs, 'After filter, expected copy to equal filtered original' assert id(copy.copy(aObjs)) != id( aObjs ), 'After filter, expected id of copy to not equal id of filtered original' assert copy.deepcopy( aObjs ) != aObjs, 'After filter, expected deepcopy to NOT equal filtered original' assert copy.deepcopy(aObjs).count() == aObjs.count( ), 'Expected deepcopy to have same number of items'
def test_ne(self): qlObjs = QueryableListObjs(self.dataObjs) found = qlObjs.filter(a__ne='one') assert len( found) == 1, 'Expected to find one item in query, found %d' % ( len(found), ) assert found[0].a == 'six' and found[ 0].c == 'eleven', 'Found wrong item from equals query'
def test_chaining(self): qlObjs = QueryableListObjs(self.dataObjs) found = qlObjs.filter(a__eq='one').filter(b__eq='two') assert len( found ) == 1, 'Expected chained filter to return one element, got %d' % ( len(found), ) assert found[0].a == 'one' and found[ 0].b == 'two', 'Got wrong item in chained query'
def test_Objs(self): gotException = False try: qlObjs = QueryableListObjs(self.dataObjs) found = qlObjs.filter(a='one') except Exception as e: gotException = e assert gotException is False, 'Got Exception for QueryableListObjs 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: dObjs = QueryableListObjs(self.dataDicts) found = dObjs.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 QueryableListObjs with list of dicts'
def test_all(self): qlObjs = QueryableListObjs(self.dataObjs) qlObjsAll = qlObjs.all() assert qlObjsAll == qlObjs, 'Expected .all() to equal original' assert id(qlObjsAll) != id( qlObjs), 'Expected id of .all() to not equal id of original' aObjs = qlObjs.filter(a='one') aObjsAll = aObjs.all() assert aObjs == aObjsAll, 'After filter, Expected .all() to equal filtered original' assert id(aObjs) != id( aObjsAll ), 'After filter, expected id of .all() NOT to equal id of filtered original'
SampleDataObj(colour='red', age=55, name='Jack', likes=['puppies', 'milk']), { 'colour' : 'green', 'age' : 88, 'name' : 'John', 'likes' : ['puppies', 'games'] }, { 'colour' : 'orange', 'age' : 18, 'name' : 'Phil', 'likes' : ['puppies', 'gnomes'] }, ] # data = QueryableListDicts(data) data = QueryableListObjs(data) sys.stdout.write("Data: %s\n\n" %(data,)) sys.stdout.write('People who are over 22 years old:\n%s\n\n' %(data.filter(age__gt=22),)) # sys.stdout.write('People who like puppies or bricks, and their favourite colour is purple:\n\n' %(data.filter(likes__containsAny=('puppies', 'bricks')).filter(colour__ieq='purple'),)) sys.stdout.write('People who like puppies or bricks, and their favourite colour is purple:\n%s\n\n' %(data.filter(likes__containsAny=('puppies', 'bricks'), colour__ieq='purple'),)) sys.stdout.write('People who are at least 30 years old or like cheese:\n%s\n\n' %(data.filterOr(likes__contains='cheese', age__gte=30),)) # Create a QueryBuilder to execute a query builder = QueryBuilder() builder.addFilter("AND", age__gt=22) builder.addFilter(likes__contains='puppies') # Execute on a QueryableList sys.stdout.write('Over 22 and likes puppies (dataset1):\n%s\n\n' %(str(builder.execute(data)))) # Execute on a normal list, creating a QueryableListMixed