-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_get_joined_test.go
158 lines (140 loc) · 4.42 KB
/
db_get_joined_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package structdbpostgres
import (
"fmt"
"testing"
)
type ProductKind struct {
ID int64
Name string
}
type ProductGroup struct {
ID int64
Name string
Description string
Code string
}
type Product struct {
ID int64
Name string
Price int
ProductKindID int64
ProductGrpID int64
}
type Product_WithDetails struct {
ID int64
Name string
Price int
ProductKindID int64
ProductGrpID int64
ProductKind *ProductKind `2db:"join"`
ProductKind_Name string
ProductGrp *ProductGroup `2db:"join"`
ProductGrp_Code string
}
// Scenarios to test:
// * Load()
// * Get()
// There is no plan on making Save() at this point
func TestJoinedLoad(t *testing.T) {
createTestJoinedStructs()
p := &Product_WithDetails{}
err := testController.Load(p, fmt.Sprintf("%d", 6), LoadOptions{})
if err != nil {
t.Fatalf("Load failed to get data for struct with other joined structs: %s", err.(ErrController).Err.Error())
}
if p.Name != "Product Name" || p.Price != 1234 || p.ProductKindID != 33 || p.ProductGrpID != 113 {
t.Fatalf("Load failed to set struct fields: %s", err.(ErrController).Err.Error())
}
if p.ProductKind_Name != "Kind 1" || p.ProductGrp_Code != "GRP1" {
t.Fatalf("Load failed to set 'joined' fields: %s", err.(ErrController).Op)
}
}
func TestJoinedGet(t *testing.T) {
createTestJoinedStructs()
ps, err := testController.Get(func() interface{} {
return &Product_WithDetails{}
}, GetOptions{
Order: []string{"ID", "asc"},
Limit: 22,
Offset: 0,
Filters: map[string]interface{}{
"Name": "Product Name",
"ProductKind_Name": "Kind 1",
"ProductGrp_Code": "GRP1",
},
})
if err != nil {
t.Fatalf("Get failed to return list of joined structs: %s", err.(ErrController).Op)
}
if len(ps) != 1 {
t.Fatalf("Get failed to return list of joined structs, want %v, got %v", 1, len(ps))
}
if ps[0].(*Product_WithDetails).Name != "Product Name" || ps[0].(*Product_WithDetails).Price != 1234 || ps[0].(*Product_WithDetails).ProductKindID != 33 || ps[0].(*Product_WithDetails).ProductGrpID != 113 {
t.Fatalf("Load failed to set struct fields: %s", err.(ErrController).Op)
}
if ps[0].(*Product_WithDetails).ProductKind_Name != "Kind 1" || ps[0].(*Product_WithDetails).ProductGrp_Code != "GRP1" {
t.Fatalf("Get failed to return correct list of joined objects")
}
ps, err = testController.Get(func() interface{} {
return &Product_WithDetails{}
}, GetOptions{
Order: []string{"ID", "asc"},
Limit: 22,
Offset: 0,
Filters: map[string]interface{}{
"Name": "Product Name 1",
"ProductKind_Name": "Kind 12",
"ProductGrp_Code": "GRP12",
"_raw": []interface{}{
"(.Name=? OR .ProductGrp_Code=? OR .ProductKind_Name IN (?))",
"Product Name",
"GRP1",
[]string{"Kind 1", "Kind 2"},
},
"_rawConjuction": RawConjuctionOR,
},
})
if err != nil {
t.Fatalf("Get failed to return list of joined structs using raw filter: %s", err.(ErrController).Op)
}
if len(ps) != 1 {
t.Fatalf("Get failed to return list of joined structs using raw filter, want %v, got %v", 1, len(ps))
}
if ps[0].(*Product_WithDetails).Name != "Product Name" || ps[0].(*Product_WithDetails).Price != 1234 || ps[0].(*Product_WithDetails).ProductKindID != 33 || ps[0].(*Product_WithDetails).ProductGrpID != 113 {
t.Fatalf("Get failed to set fields on returned list of joined structs using raw filter")
}
if ps[0].(*Product_WithDetails).ProductKind_Name != "Kind 1" || ps[0].(*Product_WithDetails).ProductGrp_Code != "GRP1" {
t.Fatalf("Get failed to set fields on returned list of joined structs using raw filter")
}
}
func createTestJoinedStructs() {
recreateTestJoinedStructTables()
pg := &ProductGroup{
ID: 113,
Name: "Group 1",
Description: "A group of products",
Code: "GRP1",
}
testController.Save(pg, SaveOptions{})
pk := &ProductKind{
ID: 33,
Name: "Kind 1",
}
testController.Save(pk, SaveOptions{})
p := &Product{
ID: 6,
Name: "Product Name",
Price: 1234,
ProductKindID: 33,
ProductGrpID: 113,
}
testController.Save(p, SaveOptions{})
}
func recreateTestJoinedStructTables() {
testController.DropTable(&Product{})
testController.DropTable(&ProductGroup{})
testController.DropTable(&ProductKind{})
testController.CreateTable(&Product{})
testController.CreateTable(&ProductGroup{})
testController.CreateTable(&ProductKind{})
}