forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abtest_test.go
164 lines (151 loc) · 4.44 KB
/
abtest_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
159
160
161
162
163
164
package uadmin
import (
"fmt"
"net/http"
"time"
)
func (t *UAdminTests) TestABTest() {
currentAbTestsCount := abTestCount
test01 := ABTest{
Name: "test 01",
Type: ABTestType(0).Static(),
Active: true,
Group: "test 1",
}
test01.Save()
test01.Save()
if test01.ID == 0 {
t.Errorf("ABTest was not saved correctly. Got 0 ID")
}
if abTestCount != currentAbTestsCount+1 {
t.Errorf("abTestCount didn't increment correctly. Expected %d but got %d", currentAbTestsCount+1, abTestCount)
}
test02 := ABTest{
Name: "test 02",
Type: ABTestType(0).Model(),
ModelName: ModelList(0),
Field: FieldList(0),
Active: true,
Group: "test 1",
}
test02.Save()
if test02.ID == 0 {
t.Errorf("ABTest was not saved correctly. Got 0 ID")
}
if abTestCount != currentAbTestsCount+2 {
t.Errorf("abTestCount didn't increment correctly. Expected %d but got %d", currentAbTestsCount+1, abTestCount)
}
// Add Values
value1A := ABTestValue{
ABTestID: test01.ID,
Value: "A",
Active: true,
}
value1B := ABTestValue{
ABTestID: test01.ID,
Value: "B",
Active: true,
}
Save(&value1A)
Save(&value1B)
value2A := ABTestValue{
ABTestID: test02.ID,
Value: "A",
Active: true,
}
value2B := ABTestValue{
ABTestID: test02.ID,
Value: "B",
Active: true,
}
Save(&value2A)
Save(&value2B)
syncABTests()
r := &http.Request{}
r.Header = http.Header{}
// get abt
abt := getABT(r)
r.AddCookie(&http.Cookie{
Name: "abt",
Value: fmt.Sprint(abt),
Path: "/",
})
ABTestClick(r, "test 1")
time.Sleep(time.Millisecond * 10)
// We lock and unlock to ensure that ABTestClick is done
abTestsMutex.Lock()
time.Sleep(time.Millisecond)
abTestsMutex.Unlock()
syncABTests()
// Check click
clicksRight := 0
clicksWrong := 0
if abt%2 == 0 {
GetValueSorted("ab_test_values", "clicks", "", true, &clicksRight, "id = ?", value1A.ID)
GetValueSorted("ab_test_values", "clicks", "", true, &clicksWrong, "id = ?", value1B.ID)
} else {
GetValueSorted("ab_test_values", "clicks", "", true, &clicksWrong, "id = ?", value1A.ID)
GetValueSorted("ab_test_values", "clicks", "", true, &clicksRight, "id = ?", value1B.ID)
}
if clicksRight != 1 {
t.Errorf("Test 1 Expected 1 click for the right value got %d", clicksRight)
}
if clicksWrong != 0 {
t.Errorf("Test 1 Expected 0 click for the wrong value got %d", clicksWrong)
}
clicksRight = 0
clicksWrong = 0
if abt%2 == 0 {
GetValueSorted("ab_test_values", "clicks", "", true, &clicksRight, "id = ?", value2A.ID)
GetValueSorted("ab_test_values", "clicks", "", true, &clicksWrong, "id = ?", value2B.ID)
} else {
GetValueSorted("ab_test_values", "clicks", "", true, &clicksWrong, "id = ?", value2A.ID)
GetValueSorted("ab_test_values", "clicks", "", true, &clicksRight, "id = ?", value2B.ID)
}
if clicksRight != 1 {
t.Errorf("Test 2 Expected 1 click for the right value got %d", clicksRight)
}
if clicksWrong != 0 {
t.Errorf("Test 2 Expected 0 click for the wrong value got %d", clicksWrong)
}
// test reset
test01.Reset()
clicksRight = 0
clicksWrong = 0
GetValueSorted("ab_test_values", "clicks", "", true, &clicksRight, "id = ?", value1A.ID)
GetValueSorted("ab_test_values", "clicks", "", true, &clicksWrong, "id = ?", value1B.ID)
if clicksRight != 0 {
t.Errorf("Expected 0 click for the right value got %d", clicksRight)
}
if clicksWrong != 0 {
t.Errorf("Expected 0 click for the wrong value got %d", clicksWrong)
}
// Clean up
Delete(&test01)
}
func (t *UAdminTests) TestLoadModels() {
choiceList := loadModels(nil, nil)
if len(modelList) != len(choiceList) {
t.Errorf("loadModels didn't return the correct number of choices, expected %d but got %d", len(modelList), len(choiceList))
}
}
func (t *UAdminTests) TestLoadFields() {
test := ABTest{
Type: ABTestType(0).Static(),
}
choiceList := loadFields(test, nil)
if len(choiceList) != 0 {
t.Errorf("loadFields didn't return the correct number of choices, expected %d but got %d", 0, len(choiceList))
}
choiceList = loadFields(&test, nil)
if len(choiceList) != 0 {
t.Errorf("loadFields on pointer didn't return the correct number of choices, expected %d but got %d", 0, len(choiceList))
}
test.Type = ABTestType(0).Model()
test.ModelName = ModelList(0)
choiceList = loadFields(test, nil)
s := Schema[getModelName(modelList[0])]
if len(choiceList) != len(s.Fields) {
t.Errorf("loadFields didn't return the correct number of choices, expected %d but got %d", len(s.Fields), len(choiceList))
}
}