-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_test.go
155 lines (141 loc) · 3.78 KB
/
update_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
package solr
import (
"testing"
)
func TestNewUpdateBuilder(t *testing.T) {
u := NewUpdateBuilder()
if len(u.commands) != 0 {
t.Fatal("update builder should be initialized")
}
}
func TestUpdateBuilderAdd(t *testing.T) {
u := NewUpdateBuilder()
input := map[string]interface{}{"test": "test"}
u.add(input)
u.prepare()
_, ok := u.commands[CommandAdd]
if !ok {
t.Fatal("add command not found!")
}
}
func TestUpdateBuilderCommit(t *testing.T) {
u := NewUpdateBuilder()
u.commit(nil)
_, ok := u.commands[CommandCommit]
if !ok {
t.Fatal("commit command not found!")
}
}
func TestUpdateBuilderOptimize(t *testing.T) {
u := NewUpdateBuilder()
u.optimize(nil)
_, ok := u.commands[CommandOptimize]
if !ok {
t.Fatal("optimize command not found!")
}
}
func TestUpdateBuilderRollback(t *testing.T) {
u := NewUpdateBuilder()
u.rollback()
_, ok := u.commands[CommandRollback]
if !ok {
t.Fatal("rollback command not found!")
}
}
func TestUpdateBuilderDelete(t *testing.T) {
u := NewUpdateBuilder()
u.DeleteByQuery("test")
u.prepare()
_, ok := u.commands[CommandDelete]
if !ok {
t.Fatal("delete command not found!")
}
}
func TestNewUpdateDocument(t *testing.T) {
upd := NewUpdateDocument("test")
id, ok := upd.fields["id"]
if !ok {
t.Fatal("Id property not found!")
}
if upd.fields["id"] != "test" {
t.Fatalf("expected id to be %s but got %s", "test", id)
}
}
func TestUpdateAddDistinct(t *testing.T) {
upd := NewUpdateDocument("test")
input := []string{"one", "two", "one"}
upd.AddDistinct("field", input)
innerMap := upd.fields["field"].(map[string]interface{})
actual, ok := innerMap[ActionAddDistinct]
if !ok {
t.Fatal("Add distinct property not found!")
}
if len(actual.([]string)) != len(input) {
t.Fatalf("expected property to contain %d values but instead found %d", len(input), len(actual.([]string)))
}
}
func TestUpdateAdd(t *testing.T) {
upd := NewUpdateDocument("test")
input := []string{"one", "two"}
upd.Add("field", input)
innerMap := upd.fields["field"].(map[string]interface{})
actual, ok := innerMap[ActionAdd]
if !ok {
t.Fatal("Add property not found!")
}
if len(actual.([]string)) != len(input) {
t.Fatalf("expected property to contain %d values but instead found %d", len(input), len(actual.([]string)))
}
}
func TestUpdateSet(t *testing.T) {
upd := NewUpdateDocument("test")
input := "settest"
upd.Set("field", input)
innerMap := upd.fields["field"].(map[string]interface{})
actual, ok := innerMap[ActionSet]
if !ok {
t.Fatal("Set property not found!")
}
if actual.(string) != input {
t.Fatalf("expected property to be %s but instead got %s", input, actual.(string))
}
}
func TestUpdateRemove(t *testing.T) {
upd := NewUpdateDocument("test")
input := "removetest"
upd.Remove("field", input)
innerMap := upd.fields["field"].(map[string]interface{})
actual, ok := innerMap[ActionRemove]
if !ok {
t.Fatal("Remove property not found!")
}
if actual.(string) != input {
t.Fatalf("expected property to be %s but instead got %s", input, actual.(string))
}
}
func TestUpdateRemoveRegex(t *testing.T) {
upd := NewUpdateDocument("test")
input := "set*"
upd.RemoveRegex("field", input)
innerMap := upd.fields["field"].(map[string]interface{})
actual, ok := innerMap[ActionRemoveRegex]
if !ok {
t.Fatal("RemoveRegex property not found!")
}
if actual.(string) != input {
t.Fatalf("expected property to be %s but instead got %s", input, actual.(string))
}
}
func TestUpdateIncrementBy(t *testing.T) {
upd := NewUpdateDocument("test")
input := 2
upd.IncrementBy("field", input)
innerMap := upd.fields["field"].(map[string]interface{})
actual, ok := innerMap[ActionIncrement]
if !ok {
t.Fatal("Increment property not found!")
}
if actual.(int) != input {
t.Fatalf("expected property to be %d but instead got %d", input, actual.(int))
}
}