def test_aggregate_with_filter_and_sort(self, data): res = ( Query(data) .query("aggregate(state,sum(balance))&isActive=true&sort(balance)") .all() ) states = [] balances = [] for row in data: if not row["isActive"]: continue if row["state"] not in states: states.append(row["state"]) balances.append(row["balance"]) else: i = states.index(row["state"]) balances[i] += row["balance"] exp = [ {"state": state, "balance": balance} for (state, balance) in zip(states, balances) ] exp.sort(key=operator.itemgetter("balance")) assert res assert res == exp
def test_aggregate_with_filter(self, data): res = Query(data).query("aggregate(state,sum(balance))&isActive=true").all() states = [] balances = [] for row in data: if not row["isActive"]: continue if row["state"] not in states: states.append(row["state"]) balances.append(row["balance"]) else: i = states.index(row["state"]) balances[i] += row["balance"] exp = [ {"state": state, "balance": balance} for (state, balance) in zip(states, balances) ] assert res assert res == exp
def test_simple_cmp(self, data, op, val): opc = getattr(operator, op) rep = Query(data).query("{}(index,{})".format(op, val)).all() exp = [row for row in data if opc(row["index"], val)] assert exp == rep
def test_aggregate_multiple_with_filter_and_sort(self, data): res = ( Query(data) .query( "aggregate(state,sum(balance),min(latitude),max(longitude),count())&isActive=true&sort(balance)", ) .all() ) states = [] balances = [] latitudes = [] longitudes = [] counts = [] for row in data: if not row["isActive"]: continue if row["state"] not in states: states.append(row["state"]) balances.append(row["balance"]) latitudes.append(row["latitude"]) longitudes.append(row["longitude"]) counts.append(1) else: i = states.index(row["state"]) balances[i] += row["balance"] latitudes[i] = min(latitudes[i], row["latitude"]) longitudes[i] = max(longitudes[i], row["longitude"]) counts[i] += 1 exp = [ { "state": state, "balance": balance, "latitude": latitude, "longitude": longitude, "count": count, } for (state, balance, latitude, longitude, count) in zip( states, balances, latitudes, longitudes, counts ) ] exp.sort(key=operator.itemgetter("balance")) assert res assert res == exp
def test_double_cmp_with_or(self, data, op1, op2, v1, v2): opc1 = getattr(operator, op1) opc2 = getattr(operator, op2) rep = ( Query(data) .query("or({op1}(index,{v1}),{op2}(latitude,{v2}))".format(**locals())) .all() ) exp = [ row for row in data if opc1(row["index"], v1) or opc2(row["latitude"], v2) ] assert exp == rep
def test_aggregate(self, data): res = Query(data).query("aggregate(state,sum(balance))").all() states = [] balances = [] for row in data: if row["state"] not in states: states.append(row["state"]) balances.append(row["balance"]) else: i = states.index(row["state"]) balances[i] += row["balance"] exp = [ {"state": state, "balance": balance} for (state, balance) in zip(states, balances) ] assert res assert res == exp
def test_excludes_array(self, data): res = Query(data).query("excludes(tags,aliqua)").all() exp = [row for row in data if "aliqua" not in row["tags"]] assert res assert res == exp
def test_contains_array(self, data): res = Query(data).query("contains(tags,aliqua)").all() exp = [row for row in data if "aliqua" in row["tags"]] assert res assert res == exp
def test_excludes_string(self, data): res = Query(data).query("excludes(email,besto.com)").all() exp = [row for row in data if "besto.com" not in row["email"]] assert res assert res == exp
def test_out(self, data): rep = Query(data).query("out(index,(11,12,13,14,15))").all() exp = [row for row in data if row["index"] not in (11, 12, 13, 14, 15)] assert rep == exp
def test_simple_eq(self, data): rep = Query(data).query("eq(index,1)").all() assert len(rep) == 1 assert rep[0]["index"] == 1
def test_complex_sort(self, data): rep = Query(data).query("sort(balance,registered,birthdate)").all() assert rep == sorted( data, key=lambda x: (x["balance"], x["registered"], x["birthdate"]) )
def test_sum(self, data): rep = Query(data).query("sum(balance)").all() assert rep == sum(row["balance"] for row in data)
def test_max(self, data): rep = Query(data).query("max(balance)").all() assert rep == max(row["balance"] for row in data)
def test_mean(self, data): rep = Query(data).query("mean(balance)").all() assert rep == sum(row["balance"] for row in data) / len(data)
def test_one(self, data): rep = Query(data).query("eq(index,11)&one()").all() assert rep == [data[11]]
def test_first(self, data): rep = Query(data).query("first()").all() assert rep == [data[0]]
def test_distinct(self, data): rep = Query(data + data).query("distinct()").all() assert rep == data
def test_select(self, data): res = Query(data).query("select(index,state)").all() exp = [{"index": row["index"], "state": row["state"]} for row in data] assert res assert res == exp
def test_count1(self, data): rep = Query(data).query("count()").all() assert rep == len(data)
def test_values(self, data): res = Query(data).query("values(state)").all() exp = [row["state"] for row in data] assert res assert res == exp
def test_simple_sort(self, data, key): rep = Query(data).query("sort({})".format(key)).all() assert rep == sorted(data, key=operator.itemgetter(key))
def test_simple_limit(self, data, limit): rep = Query(data).query("limit({})".format(limit)).all() assert rep == data[:limit]
def test_out_operator(self, data): res = Query(data).query("out(state,(FL,TX))").all() exp = [row for row in data if row["state"] not in {"FL", "TX"}] assert res assert res == exp
def test_reverse_sort(self, data, key): rep = Query(data).query("sort(-{})".format(key)).all() assert rep == sorted(data, key=operator.itemgetter(key), reverse=True)
def test_contains_string(self, data): res = Query(data).query("contains(email,besto.com)").all() exp = [row for row in data if "besto.com" in row["email"]] assert res assert res == exp
def test_count2(self, data): rep = Query(data).query("gt(balance,2000)&count()").all() assert rep == len([row for row in data if row["balance"] > 2000])
def test_limit_offset(self, data, limit, offset): rep = Query(data).query("limit({},{})".format(limit, offset)).all() assert rep == data[offset:][:limit]