@@ -139,9 +139,6 @@ func TestChangesetApply(t *testing.T) {
139139 )
140140 )
141141
142- userMutation .SetAssoc ("transactions" , transaction1Mutation , transaction2Mutation )
143- userMutation .SetAssoc ("address" , addressMutation )
144-
145142 ch := Cast (user , input , []string {"name" , "age" })
146143 CastAssoc (ch , "transactions" , func (data interface {}, input params.Params ) * Changeset {
147144 ch := Cast (data , input , []string {"item" , "status" })
@@ -152,6 +149,9 @@ func TestChangesetApply(t *testing.T) {
152149 return ch
153150 })
154151
152+ userMutation .SetAssoc ("transactions" , transaction1Mutation , transaction2Mutation )
153+ userMutation .SetAssoc ("address" , addressMutation )
154+
155155 assert .Nil (t , ch .Error ())
156156 assert .Equal (t , userMutation , rel .Apply (doc , ch ))
157157 assert .Equal (t , User {
@@ -170,3 +170,35 @@ func TestChangesetApply(t *testing.T) {
170170 },
171171 }, user )
172172}
173+
174+ func TestChangesetApply_constraint (t * testing.T ) {
175+ var (
176+ user User
177+ now = time .Now ().Truncate (time .Second )
178+ doc = rel .NewDocument (& user )
179+ input = params.Map {
180+ "name" : "Luffy" ,
181+ "age" : 20 ,
182+ }
183+ userMutation = rel .Apply (rel .NewDocument (& User {}),
184+ rel .Set ("name" , "Luffy" ),
185+ rel .Set ("age" , 20 ),
186+ rel .Set ("created_at" , now ),
187+ rel .Set ("updated_at" , now ),
188+ )
189+ )
190+
191+ ch := Cast (user , input , []string {"name" , "age" })
192+ UniqueConstraint (ch , "name" )
193+ mut := rel .Apply (doc , ch )
194+
195+ assert .Nil (t , ch .Error ())
196+ assert .Equal (t , userMutation .Mutates , mut .Mutates )
197+ assert .NotNil (t , mut .ErrorFunc )
198+ assert .Equal (t , User {
199+ Name : "Luffy" ,
200+ Age : 20 ,
201+ CreatedAt : now ,
202+ UpdatedAt : now ,
203+ }, user )
204+ }
0 commit comments