-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusecase_test.go
119 lines (96 loc) · 3.29 KB
/
usecase_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
package golconda
import (
"fmt"
"testing"
)
type filters struct {
byEmail interface{}
byName interface{}
byId interface{}
byLocation interface{}
byDate interface{}
byDateStart interface{}
byDateEnd interface{}
byLastUpdateIsNull bool
byConfirmDate bool
byLastUpdateStart interface{}
}
// this is a real use case
func TestFullUseCase(t *testing.T) {
// build the condition
condition := NewAnd()
// filters could come from func arguments or somewhere else
filters := filters{}
filters.byEmail = "myemail@test.it"
filters.byId = []int{13, 21}
filters.byLocation = 19
filters.byDate = SqlExpression("NOW()")
filters.byDateStart = "2021-01-01"
filters.byDateEnd = "2021-01-02"
filters.byConfirmDate = true
filters.byLastUpdateStart = "2021-02-02"
subCondition := NewOr()
subCondition.Append(
IsEqual("confirmed", 1),
IsEqual("waiting", 2),
)
condition.Append(
IsEqual("email", filters.byEmail),
IsEqual("name", filters.byName), // <nil>, so this will not be appended
IsEqual("id", filters.byId), // Slice, so the equal operator is converted in IN operator
IsEqual("location", filters.byLocation),
IsEqual("date", filters.byDate),
IsBetween("date", filters.byDateStart, filters.byDateEnd),
IsNull("last_update", filters.byLastUpdateIsNull),
IsNotNull("confirm_date", filters.byConfirmDate),
IsGreaterOrEqual("last_update", filters.byLastUpdateStart),
subCondition.AsOperator(),
)
// "name" condition should not be printed
expected := "(email = ? AND id IN (?,?) AND location = ? AND date = NOW() AND date BETWEEN ? AND ? AND confirm_date IS NOT NULL AND last_update >= ? AND (confirmed = ? OR waiting = ?))"
current, vals := condition.Build()
if expected != current {
t.Errorf("Expected `%s`, got `%s`", expected, current)
}
expectedLen := 9
if len(vals) != expectedLen {
t.Errorf("Expected length o condition.Values() to be %d, got %d", expectedLen, len(vals))
}
if vals[0] != filters.byEmail {
t.Errorf("Expected vals[0] to be %s, got %s", filters.byEmail, vals[0])
}
if vals[1] != filters.byId.([]int)[0] {
t.Errorf("Expected vals[1] to be %d, got %s", filters.byId.([]int)[0], vals[1])
}
if vals[2] != filters.byId.([]int)[1] {
t.Errorf("Expected vals[2] to be %d, got %s", filters.byId.([]int)[1], vals[2])
}
if vals[3] != filters.byLocation {
t.Errorf("Expected vals[3] to be %d, got %s", filters.byLocation, vals[3])
}
if vals[4] != filters.byDateStart {
t.Errorf("Expected vals[4] to be %d, got %s", filters.byDateStart, vals[4])
}
if vals[5] != filters.byDateEnd {
t.Errorf("Expected vals[5] to be %d, got %s", filters.byDateEnd, vals[5])
}
if vals[6] != filters.byLastUpdateStart {
t.Errorf("Expected vals[6] to be %d, got %s", filters.byLastUpdateStart, vals[6])
}
if vals[7] != 1 {
t.Errorf("Expected vals[6] to be %d, got %s", 1, vals[7])
}
if vals[8] != 2 {
t.Errorf("Expected vals[6] to be %d, got %s", 2, vals[8])
}
rawQuery := fmt.Sprintf("SELECT * FROM users WHERE %s", current)
// ok this is pretty useless to test but...
expectedRaw := "SELECT * FROM users WHERE " + expected
if rawQuery != expectedRaw {
t.Errorf("Raw query built failed. Expected `%s`, got `%s` ", expectedRaw, rawQuery)
}
// and then...
/*
db.Raw(rawQuery, vals...)
*/
}