-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinsert_all_test.go
117 lines (95 loc) · 2.8 KB
/
insert_all_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
package reltest
import (
"context"
"testing"
"github.com/go-rel/rel"
"github.com/stretchr/testify/assert"
)
func TestInsertAll(t *testing.T) {
var (
repo = New()
results = []Book{
{Title: "Golang for dummies"},
{Title: "Rel for dummies"},
}
books = []Book{
{ID: 1, Title: "Golang for dummies"},
{ID: 2, Title: "Rel for dummies"},
}
)
repo.ExpectInsertAll().Success()
assert.Nil(t, repo.InsertAll(context.TODO(), &results))
assert.Equal(t, books, results)
repo.AssertExpectations(t)
repo.ExpectInsertAll().Success()
assert.NotPanics(t, func() {
repo.MustInsertAll(context.TODO(), &results)
assert.Equal(t, books, results)
})
repo.AssertExpectations(t)
}
func TestInsertAll_error(t *testing.T) {
var (
repo = New()
results []Book
)
repo.ExpectInsertAll().ConnectionClosed()
assert.Equal(t, ErrConnectionClosed, repo.InsertAll(context.TODO(), &results))
repo.AssertExpectations(t)
repo.ExpectInsertAll().NotUnique("title")
assert.Equal(t, rel.ConstraintError{
Key: "title",
Type: rel.UniqueConstraint,
}, repo.InsertAll(context.TODO(), &results))
repo.AssertExpectations(t)
}
func TestInsertAll_assert(t *testing.T) {
var (
repo = New()
)
repo.ExpectInsertAll().For(&[]Book{{Title: "Golang"}})
assert.Panics(t, func() {
repo.InsertAll(context.TODO(), &[]Book{{Title: "Go"}})
})
assert.False(t, repo.AssertExpectations(nt))
assert.Equal(t, "FAIL: Mock defined but not called:\nInsertAll(ctx, &[]reltest.Book{reltest.Book{Title: \"Golang\"}})", nt.lastLog)
}
func TestInsertAll_assertForTable(t *testing.T) {
var (
repo = New()
)
repo.ExpectInsertAll().ForTable("users")
assert.Panics(t, func() {
repo.InsertAll(context.TODO(), &[]Book{})
})
assert.False(t, repo.AssertExpectations(nt))
assert.Equal(t, "FAIL: Mock defined but not called:\nInsertAll(ctx, <Table: users>)", nt.lastLog)
}
func TestInsertAll_assertForType(t *testing.T) {
var (
repo = New()
)
repo.ExpectInsertAll().ForType("[]User")
assert.Panics(t, func() {
repo.InsertAll(context.TODO(), &[]Book{})
})
assert.False(t, repo.AssertExpectations(nt))
assert.Equal(t, "FAIL: Mock defined but not called:\nInsertAll(ctx, <Type: *[]User>)", nt.lastLog)
}
func TestInsertAll_assert_transaction(t *testing.T) {
var (
repo = New()
)
repo.ExpectTransaction(func(repo *Repository) {
repo.ExpectInsertAll().ForType("[]User")
})
assert.False(t, repo.AssertExpectations(nt))
assert.Equal(t, "FAIL: Mock defined but not called:\n<Transaction: 1> InsertAll(ctx, <Type: *[]User>)", nt.lastLog)
}
func TestInsertAll_String(t *testing.T) {
var (
mockInsertAll = MockInsertAll{assert: &Assert{}, argEntity: &[]Book{}}
)
assert.Equal(t, "InsertAll(ctx, &[]reltest.Book{})", mockInsertAll.String())
assert.Equal(t, "InsertAll().ForType(\"*[]reltest.Book\")", mockInsertAll.ExpectString())
}