forked from go-rel/changeset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangeset_test.go
204 lines (182 loc) · 4.3 KB
/
changeset_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package changeset
import (
"testing"
"time"
"github.com/Fs02/changeset/params"
"github.com/Fs02/rel"
"github.com/stretchr/testify/assert"
)
type Status string
type User struct {
ID int
Name string
Age int
Transactions []Transaction `ref:"id" fk:"user_id"`
Address Address
CreatedAt time.Time
UpdatedAt time.Time
}
type Transaction struct {
ID int
Item string
Status Status
BuyerID int `db:"user_id"`
Buyer User `ref:"user_id" fk:"id"`
}
type Notes string
func (n Notes) Equal(other interface{}) bool {
if o, ok := other.(Notes); ok {
return n == o
}
return false
}
type Address struct {
ID int
UserID *int
User *User
Street string
Notes Notes
Flagged *bool
DeletedAt *time.Time
}
type Owner struct {
User *User
UserID *int
}
func TestChangeset(t *testing.T) {
ch := Changeset{}
assert.Nil(t, ch.Errors())
assert.Nil(t, ch.Error())
assert.Nil(t, ch.Changes())
assert.Nil(t, ch.Values())
assert.Nil(t, ch.Types())
assert.Nil(t, ch.Constraints())
}
func TestChangeset_Get(t *testing.T) {
ch := Changeset{
changes: map[string]interface{}{
"a": 2,
},
}
assert.Equal(t, 2, ch.Get("a"))
assert.Equal(t, nil, ch.Get("b"))
assert.Equal(t, 1, len(ch.changes))
}
func TestChangeset_Fetch(t *testing.T) {
ch := Changeset{
changes: map[string]interface{}{
"a": 1,
},
values: map[string]interface{}{
"b": 2,
},
}
assert.Equal(t, 1, ch.Fetch("a"))
assert.Equal(t, 2, ch.Fetch("b"))
assert.Equal(t, nil, ch.Fetch("c"))
assert.Equal(t, 1, len(ch.changes))
assert.Equal(t, 1, len(ch.values))
}
func TestChangesetApply(t *testing.T) {
var (
user User
now = time.Now().Truncate(time.Second)
flagged = true
doc = rel.NewDocument(&user)
input = params.Map{
"name": "Luffy",
"age": 20,
"transactions": []params.Map{
{
"item": "Sword",
"status": "pending",
},
{
"item": "Shield",
"status": "paid",
},
},
"address": params.Map{
"street": "Grove Street",
"notes": "Brown fox jumps",
"flagged": true,
},
}
userMutation = rel.Apply(rel.NewDocument(&User{}),
rel.Set("name", "Luffy"),
rel.Set("age", 20),
rel.Set("created_at", now),
rel.Set("updated_at", now),
)
transaction1Mutation = rel.Apply(rel.NewDocument(&Transaction{}),
rel.Set("item", "Sword"),
rel.Set("status", Status("pending")),
)
transaction2Mutation = rel.Apply(rel.NewDocument(&Transaction{}),
rel.Set("item", "Shield"),
rel.Set("status", Status("paid")),
)
addressMutation = rel.Apply(rel.NewDocument(&Address{}),
rel.Set("street", "Grove Street"),
rel.Set("notes", Notes("Brown fox jumps")),
rel.Set("flagged", true),
)
)
ch := Cast(user, input, []string{"name", "age"})
CastAssoc(ch, "transactions", func(data interface{}, input params.Params) *Changeset {
ch := Cast(data, input, []string{"item", "status"})
return ch
})
CastAssoc(ch, "address", func(data interface{}, input params.Params) *Changeset {
ch := Cast(data, input, []string{"street", "notes", "flagged"})
return ch
})
userMutation.SetAssoc("transactions", transaction1Mutation, transaction2Mutation)
userMutation.SetAssoc("address", addressMutation)
assert.Nil(t, ch.Error())
assert.Equal(t, userMutation, rel.Apply(doc, ch))
assert.Equal(t, User{
Name: "Luffy",
Age: 20,
CreatedAt: now,
UpdatedAt: now,
Transactions: []Transaction{
{Item: "Sword", Status: "pending"},
{Item: "Shield", Status: "paid"},
},
Address: Address{
Street: "Grove Street",
Notes: "Brown fox jumps",
Flagged: &flagged,
},
}, user)
}
func TestChangesetApply_constraint(t *testing.T) {
var (
user User
now = time.Now().Truncate(time.Second)
doc = rel.NewDocument(&user)
input = params.Map{
"name": "Luffy",
"age": 20,
}
userMutation = rel.Apply(rel.NewDocument(&User{}),
rel.Set("name", "Luffy"),
rel.Set("age", 20),
rel.Set("created_at", now),
rel.Set("updated_at", now),
)
)
ch := Cast(user, input, []string{"name", "age"})
UniqueConstraint(ch, "name")
mut := rel.Apply(doc, ch)
assert.Nil(t, ch.Error())
assert.Equal(t, userMutation.Mutates, mut.Mutates)
assert.NotNil(t, mut.ErrorFunc)
assert.Equal(t, User{
Name: "Luffy",
Age: 20,
CreatedAt: now,
UpdatedAt: now,
}, user)
}