MCPcopy Create free account
hub / github.com/datasweet/datatable / TestAggregate

Function TestAggregate

aggregate_test.go:12–62  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

10)
11
12func TestAggregate(t *testing.T) {
13 customers, orders := sampleForJoin()
14
15 dt, err := customers.LeftJoin(orders, datatable.On("[Customers].[id]", "[Orders].[user_id]"))
16 assert.NoError(t, err)
17 assert.NotNil(t, dt)
18 fmt.Println(dt)
19
20 // Aggregate by SUM
21 out, err := dt.Aggregate(datatable.AggregateBy{datatable.Sum, "prix_total", "sum_prix_total"})
22 assert.NoError(t, err)
23 assert.NotNil(t, out)
24 fmt.Println(out)
25
26 // Aggregate by SUM(prix_total), COUNT_DISTINCT(ville)
27 out, err = dt.Aggregate(datatable.AggregateBy{datatable.Sum, "prix_total", "sum_prix_total"}, datatable.AggregateBy{datatable.CountDistinct, "ville", "uniq_count_ville"})
28 assert.NoError(t, err)
29 assert.NotNil(t, out)
30 fmt.Println(out)
31
32 groups, err := dt.GroupBy(
33 datatable.GroupBy{
34 Name: "Year",
35 Type: datatable.Int64,
36 Keyer: func(row datatable.Row) (interface{}, bool) {
37 t, ok := row["date_achat"].(time.Time)
38 if !ok {
39 return 0, false
40 }
41 return t.Year(), true
42 },
43 },
44 datatable.GroupBy{
45 Name: "Month",
46 Type: datatable.Int,
47 Keyer: func(row datatable.Row) (interface{}, bool) {
48 t, ok := row["date_achat"].(time.Time)
49 if !ok {
50 return 0, false
51 }
52 return int(t.Month()), true
53 },
54 },
55 )
56 assert.NoError(t, err)
57 assert.NotNil(t, groups)
58
59 gdt, err := groups.Aggregate(datatable.AggregateBy{datatable.Sum, "prix_total", "sum_prix_total"})
60 assert.NoError(t, err)
61 fmt.Println(gdt)
62}

Callers

nothing calls this directly

Calls 5

OnFunction · 0.92
sampleForJoinFunction · 0.85
LeftJoinMethod · 0.80
GroupByMethod · 0.80
AggregateMethod · 0.45

Tested by

no test coverage detected