forked from go-rel/reltest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository_test.go
157 lines (126 loc) · 2.9 KB
/
repository_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
package reltest
import (
"context"
"database/sql"
"testing"
"github.com/go-rel/rel"
"github.com/go-rel/rel/where"
"github.com/stretchr/testify/assert"
)
type Author struct {
ID int
Name string
Books []Book
}
type Rating struct {
ID int
Score int
BookID int
Book *Book
}
type Poster struct {
ID int
Image string
BookID int
}
type Book struct {
ID int
Title string
Author Author
AuthorID *int
Ratings []Rating `auto:"true"`
Poster Poster `autosave:"true"`
AbstractID int
Abstract Abstract `autosave:"true"`
Views int
}
type Abstract struct {
ID int
Content string
}
func TestRepository_Adapter(t *testing.T) {
var (
ctx = context.TODO()
repo = New()
)
assert.NotNil(t, repo.Adapter(ctx))
}
func TestRepository_Instrumentation(t *testing.T) {
assert.NotPanics(t, func() {
New().Instrumentation(rel.DefaultLogger)
})
}
func TestRepository_Ping(t *testing.T) {
assert.Nil(t, New().Ping(context.TODO()))
}
func TestRepository_Transaction(t *testing.T) {
var (
repo = New()
result = Book{Title: "Golang for dummies"}
book = Book{ID: 1, Title: "Golang for dummies", Ratings: []Rating{}}
)
repo.ExpectTransaction(func(repo *Repository) {
repo.ExpectInsert()
repo.ExpectTransaction(func(repo *Repository) {
repo.ExpectFind(where.Eq("id", 1))
repo.ExpectTransaction(func(repo *Repository) {
repo.ExpectDelete()
})
})
})
assert.Nil(t, repo.Transaction(context.TODO(), func(ctx context.Context) error {
repo.MustInsert(ctx, &result)
return repo.Transaction(ctx, func(ctx context.Context) error {
repo.MustFind(ctx, &result, where.Eq("id", 1))
return repo.Transaction(ctx, func(ctx context.Context) error {
return repo.Delete(ctx, &result)
})
})
}))
assert.Equal(t, book, result)
repo.AssertExpectations(t)
}
func TestRepository_Transaction_error(t *testing.T) {
var (
repo = New()
result = Book{Title: "Golang for dummies"}
book = Book{ID: 1, Title: "Golang for dummies"}
)
repo.ExpectTransaction(func(repo *Repository) {
repo.ExpectInsert().ConnectionClosed()
})
assert.Equal(t, sql.ErrConnDone, repo.Transaction(context.TODO(), func(ctx context.Context) error {
repo.MustInsert(ctx, &result)
return nil
}))
assert.Equal(t, book, result)
repo.AssertExpectations(t)
}
func TestRepository_Transaction_panic(t *testing.T) {
var (
repo = New()
)
repo.ExpectTransaction(func(repo *Repository) {
})
assert.Panics(t, func() {
_ = repo.Transaction(context.TODO(), func(ctx context.Context) error {
panic("error")
})
})
repo.AssertExpectations(t)
}
func TestRepository_Transaction_runtimerError(t *testing.T) {
var (
book *Book
repo = New()
)
repo.ExpectTransaction(func(repo *Repository) {
})
assert.Panics(t, func() {
_ = repo.Transaction(context.TODO(), func(ctx context.Context) error {
_ = book.ID
return nil
})
})
repo.AssertExpectations(t)
}