/
mutate.go
71 lines (58 loc) · 1.61 KB
/
mutate.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
package reltest
import (
"strings"
"github.com/go-rel/rel"
"github.com/stretchr/testify/mock"
)
// Mutate asserts and simulate insert or update function for test.
type Mutate struct {
*Expect
}
// For match expect calls for given record.
func (m *Mutate) For(record interface{}) *Mutate {
m.Arguments[1] = record
return m
}
// ForType match expect calls for given type.
// Type must include package name, example: `model.User`.
func (m *Mutate) ForType(typ string) *Mutate {
return m.For(mock.AnythingOfType("*" + strings.TrimPrefix(typ, "*")))
}
// NotUnique sets not unique error to be returned.
func (m *Mutate) NotUnique(key string) {
m.Error(rel.ConstraintError{
Key: key,
Type: rel.UniqueConstraint,
})
}
func expectMutate(r *Repository, methodName string, mutators []rel.Mutator) *Mutate {
mutatorsArgument := interface{}(mutators)
if mutators == nil {
mutatorsArgument = mock.Anything
}
em := &Mutate{
Expect: newExpect(r, methodName,
[]interface{}{r.ctxData, mock.Anything, mutatorsArgument},
[]interface{}{nil},
),
}
return em
}
// ExpectInsert to be called with given field and queries.
func ExpectInsert(r *Repository, mutators []rel.Mutator) *Mutate {
return expectMutate(r, "Insert", mutators)
}
// ExpectUpdate to be called with given field and queries.
func ExpectUpdate(r *Repository, mutators []rel.Mutator) *Mutate {
return expectMutate(r, "Update", mutators)
}
// ExpectInsertAll to be called.
func ExpectInsertAll(r *Repository) *Mutate {
em := &Mutate{
Expect: newExpect(r, "InsertAll",
[]interface{}{r.ctxData, mock.Anything},
[]interface{}{nil},
),
}
return em
}