-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupby_test.go
More file actions
82 lines (66 loc) · 1.8 KB
/
groupby_test.go
File metadata and controls
82 lines (66 loc) · 1.8 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
76
77
78
79
80
81
82
package squildx
import (
"testing"
)
func TestGroupByBasic(t *testing.T) {
q, _, err := New().
Select("department", "COUNT(*) AS cnt").
From("employees").
GroupBy("department").
Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := "SELECT department, COUNT(*) AS cnt FROM employees GROUP BY department"
if q != expected {
t.Errorf("SQL mismatch\n got: %s\nwant: %s", q, expected)
}
}
func TestGroupByMultipleCalls(t *testing.T) {
q, _, err := New().
Select("department", "role", "COUNT(*)").
From("employees").
GroupBy("department").
GroupBy("role").
Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := "SELECT department, role, COUNT(*) FROM employees GROUP BY department, role"
if q != expected {
t.Errorf("SQL mismatch\n got: %s\nwant: %s", q, expected)
}
}
func TestGroupByMultipleExprs(t *testing.T) {
q, _, err := New().
Select("department", "role", "COUNT(*)").
From("employees").
GroupBy("department", "role").
Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := "SELECT department, role, COUNT(*) FROM employees GROUP BY department, role"
if q != expected {
t.Errorf("SQL mismatch\n got: %s\nwant: %s", q, expected)
}
}
func TestGroupByImmutability(t *testing.T) {
base := New().Select("department", "COUNT(*)").From("employees")
withGroup := base.GroupBy("department")
q1, _, err := base.Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
q2, _, err := withGroup.Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if q1 == q2 {
t.Error("expected different SQL for base and grouped builder")
}
expected := "SELECT department, COUNT(*) FROM employees"
if q1 != expected {
t.Errorf("base builder was mutated\n got: %s\nwant: %s", q1, expected)
}
}