-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtest_aggregates.py
More file actions
75 lines (56 loc) · 2.21 KB
/
Copy pathtest_aggregates.py
File metadata and controls
75 lines (56 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from sqlobject import FloatCol, IntCol, SQLObject
from sqlobject.tests.dbtest import setupClass
# Test MIN, AVG, MAX, COUNT, SUM
class IntAccumulator(SQLObject):
so_value = IntCol()
class FloatAccumulator(SQLObject):
so_value = FloatCol()
def test_integer():
setupClass(IntAccumulator)
IntAccumulator(so_value=1)
IntAccumulator(so_value=2)
IntAccumulator(so_value=3)
assert IntAccumulator.select().min(IntAccumulator.q.so_value) == 1
assert IntAccumulator.select().avg(IntAccumulator.q.so_value) == 2
assert IntAccumulator.select().max(IntAccumulator.q.so_value) == 3
assert IntAccumulator.select().sum(IntAccumulator.q.so_value) == 6
assert IntAccumulator.select(IntAccumulator.q.so_value > 1).\
max(IntAccumulator.q.so_value) == 3
assert IntAccumulator.select(IntAccumulator.q.so_value > 1).\
sum(IntAccumulator.q.so_value) == 5
def floatcmp(f1, f2):
if abs(f1 - f2) < 0.1:
return 0
if f1 < f2:
return 1
return -1
def test_float():
setupClass(FloatAccumulator)
FloatAccumulator(so_value=1.2)
FloatAccumulator(so_value=2.4)
FloatAccumulator(so_value=3.8)
assert floatcmp(
FloatAccumulator.select().min(FloatAccumulator.q.so_value), 1.2) == 0
assert floatcmp(
FloatAccumulator.select().avg(FloatAccumulator.q.so_value), 2.5) == 0
assert floatcmp(
FloatAccumulator.select().max(FloatAccumulator.q.so_value), 3.8) == 0
assert floatcmp(
FloatAccumulator.select().sum(FloatAccumulator.q.so_value), 7.4) == 0
def test_many():
setupClass(IntAccumulator)
IntAccumulator(so_value=1)
IntAccumulator(so_value=1)
IntAccumulator(so_value=2)
IntAccumulator(so_value=2)
IntAccumulator(so_value=3)
IntAccumulator(so_value=3)
attribute = IntAccumulator.q.so_value
assert list(IntAccumulator.select().accumulateMany(
("MIN", attribute), ("AVG", attribute), ("MAX", attribute),
("COUNT", attribute), ("SUM", attribute)
)) == [1, 2, 3, 6, 12]
assert list(IntAccumulator.select(distinct=True).accumulateMany(
("MIN", attribute), ("AVG", attribute), ("MAX", attribute),
("COUNT", attribute), ("SUM", attribute)
)) == [1, 2, 3, 3, 6]