-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_test.go
More file actions
130 lines (117 loc) · 3.25 KB
/
Copy pathupdate_test.go
File metadata and controls
130 lines (117 loc) · 3.25 KB
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
package monq_test
import (
"reflect"
"testing"
"go.mongodb.org/mongo-driver/v2/bson"
"github.com/behzadsh/monq"
)
func TestUpdate(t *testing.T) {
tests := []struct {
name string
ops []bson.D
want bson.D
}{
{
name: "no operators",
ops: nil,
want: bson.D{},
},
{
name: "one operator passes through",
ops: []bson.D{monq.Set("status", "active")},
want: bson.D{{Key: "$set", Value: bson.D{{Key: "status", Value: "active"}}}},
},
{
name: "same operator merges its fields",
ops: []bson.D{monq.Set("status", "active"), monq.Set("name", "ada")},
want: bson.D{{Key: "$set", Value: bson.D{
{Key: "status", Value: "active"},
{Key: "name", Value: "ada"},
}}},
},
{
name: "different operators keep first-seen order",
ops: []bson.D{
monq.Set("status", "active"),
monq.Inc("logins", 1),
monq.Set("name", "ada"),
monq.Unset("deleted_at"),
},
want: bson.D{
{Key: "$set", Value: bson.D{
{Key: "status", Value: "active"},
{Key: "name", Value: "ada"},
}},
{Key: "$inc", Value: bson.D{{Key: "logins", Value: 1}}},
{Key: "$unset", Value: bson.D{{Key: "deleted_at", Value: ""}}},
},
},
{
name: "same field twice under one operator keeps the last value",
ops: []bson.D{monq.Set("status", "active"), monq.Set("status", "banned")},
want: bson.D{{Key: "$set", Value: bson.D{{Key: "status", Value: "banned"}}}},
},
{
name: "merges an already merged update",
ops: []bson.D{
monq.Update(monq.Set("status", "active"), monq.Inc("logins", 1)),
monq.Set("name", "ada"),
},
want: bson.D{
{Key: "$set", Value: bson.D{
{Key: "status", Value: "active"},
{Key: "name", Value: "ada"},
}},
{Key: "$inc", Value: bson.D{{Key: "logins", Value: 1}}},
},
},
{
name: "a hand-written operator composes like the rest",
ops: []bson.D{
monq.Set("status", "active"),
monq.Raw(bson.D{{Key: "$set", Value: bson.D{{Key: "legacy", Value: 1}}}}),
},
want: bson.D{{Key: "$set", Value: bson.D{
{Key: "status", Value: "active"},
{Key: "legacy", Value: 1},
}}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := monq.Update(tt.ops...)
if !reflect.DeepEqual(got, tt.want) {
t.Fatalf("Update() = %v, want %v", got, tt.want)
}
})
}
}
func TestUpdateLeavesOperandsUntouched(t *testing.T) {
status := monq.Set("status", "active")
name := monq.Set("name", "ada")
logins := monq.Inc("logins", 1)
first := monq.Update(status, name, logins)
second := monq.Update(status, name, logins)
if !reflect.DeepEqual(first, second) {
t.Fatalf("Update() = %v on the second call, want %v", second, first)
}
want := []bson.D{
{{Key: "$set", Value: bson.D{{Key: "status", Value: "active"}}}},
{{Key: "$set", Value: bson.D{{Key: "name", Value: "ada"}}}},
{{Key: "$inc", Value: bson.D{{Key: "logins", Value: 1}}}},
}
for i, op := range []bson.D{status, name, logins} {
if !reflect.DeepEqual(op, want[i]) {
t.Fatalf("operand %d = %v, want %v unchanged", i, op, want[i])
}
}
}
func ExampleUpdate() {
update := monq.Update(
monq.Set("status", "active"),
monq.Inc("logins", 1),
monq.Set("name", "ada"),
)
printFilter(update)
// Output: {"$set":{"status":"active","name":"ada"},"$inc":{"logins":1}}
}