-
Notifications
You must be signed in to change notification settings - Fork 1
/
querify_test.go
69 lines (60 loc) · 1.48 KB
/
querify_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
63
64
65
66
67
68
69
package querify_test
import (
"testing"
"github.com/wroge/querify"
)
func Test(t *testing.T) {
hobbiesTable := []map[string]interface{}{
{"id": 1, "name": "Football"},
{"id": 2, "name": "Basketball"},
{"id": 3, "name": "Hockey"},
}
usersTable := []map[string]interface{}{
{"id": 1, "name": "Max"},
{"id": 2, "name": "Tom"},
{"id": 3, "name": "Alex"},
}
userHobbiesTable := []map[string]interface{}{
{"user_id": 1, "hobby_id": 1},
{"user_id": 1, "hobby_id": 2},
{"user_id": 2, "hobby_id": 3},
{"user_id": 3, "hobby_id": 1},
}
type User struct {
Name string
Hobbies []string
}
var users []User
err := querify.From(usersTable).As("users").
Join(
querify.LeftJoin{
Right: querify.From(userHobbiesTable).As("user_hobbies"),
On: querify.Equals{querify.Ident("users.id"), querify.Ident("user_hobbies.user_id")},
},
querify.LeftJoin{
Right: querify.From(hobbiesTable).As("hobbies"),
On: querify.Equals{querify.Ident("hobbies.id"), querify.Ident("user_hobbies.hobby_id")},
},
).
GroupBy(querify.Ident("users.name")).
Select(
querify.As{
Name: "name",
Expression: querify.Ident("users.name"),
},
querify.As{
Name: "hobbies",
Expression: querify.ArrayAgg{
Expression: querify.Ident("hobbies.name"),
},
},
).OrderBy(querify.Asc{
Expression: querify.Ident("users.name"),
}).Offset(1).Limit(1).Scan(&users)
if err != nil {
t.Fatal(err)
}
if users[0].Name != "Max" {
t.Fatal(users)
}
}