-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathaggregate_test.go
62 lines (54 loc) · 1.52 KB
/
aggregate_test.go
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
package datatable_test
import (
"fmt"
"testing"
"time"
"github.com/datasweet/datatable"
"github.com/stretchr/testify/assert"
)
func TestAggregate(t *testing.T) {
customers, orders := sampleForJoin()
dt, err := customers.LeftJoin(orders, datatable.On("[Customers].[id]", "[Orders].[user_id]"))
assert.NoError(t, err)
assert.NotNil(t, dt)
fmt.Println(dt)
// Aggregate by SUM
out, err := dt.Aggregate(datatable.AggregateBy{datatable.Sum, "prix_total", "sum_prix_total"})
assert.NoError(t, err)
assert.NotNil(t, out)
fmt.Println(out)
// Aggregate by SUM(prix_total), COUNT_DISTINCT(ville)
out, err = dt.Aggregate(datatable.AggregateBy{datatable.Sum, "prix_total", "sum_prix_total"}, datatable.AggregateBy{datatable.CountDistinct, "ville", "uniq_count_ville"})
assert.NoError(t, err)
assert.NotNil(t, out)
fmt.Println(out)
groups, err := dt.GroupBy(
datatable.GroupBy{
Name: "Year",
Type: datatable.Int64,
Keyer: func(row datatable.Row) (interface{}, bool) {
t, ok := row["date_achat"].(time.Time)
if !ok {
return 0, false
}
return t.Year(), true
},
},
datatable.GroupBy{
Name: "Month",
Type: datatable.Int,
Keyer: func(row datatable.Row) (interface{}, bool) {
t, ok := row["date_achat"].(time.Time)
if !ok {
return 0, false
}
return int(t.Month()), true
},
},
)
assert.NoError(t, err)
assert.NotNil(t, groups)
gdt, err := groups.Aggregate(datatable.AggregateBy{datatable.Sum, "prix_total", "sum_prix_total"})
assert.NoError(t, err)
fmt.Println(gdt)
}