-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
repo_test.go
120 lines (94 loc) · 2.65 KB
/
repo_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
package grimoire
import (
"testing"
"github.com/Fs02/grimoire/errors"
"github.com/stretchr/testify/assert"
)
var repo = Repo{}
func TestNew(t *testing.T) {
adapter := new(TestAdapter)
repo := New(adapter)
assert.NotNil(t, repo)
assert.Equal(t, adapter, repo.Adapter())
}
func TestRepo_SetLogger(t *testing.T) {
repo := Repo{}
assert.Nil(t, repo.logger)
repo.SetLogger(DefaultLogger)
assert.NotNil(t, repo.logger)
}
func TestRepo_From(t *testing.T) {
assert.Equal(t, repo.From("users"), Query{
repo: &repo,
Collection: "users",
Fields: []string{"users.*"},
})
}
func TestRepo_Transaction(t *testing.T) {
mock := new(TestAdapter)
mock.On("Begin").Return(nil).
On("Commit").Return(nil)
repo := Repo{adapter: mock}
err := repo.Transaction(func(repo Repo) error {
assert.True(t, repo.inTransaction)
return nil
})
assert.False(t, repo.inTransaction)
assert.Nil(t, err)
mock.AssertExpectations(t)
}
func TestRepo_Transaction_beginError(t *testing.T) {
mock := new(TestAdapter)
mock.On("Begin").Return(errors.NewUnexpected("error"))
err := Repo{adapter: mock}.Transaction(func(r Repo) error {
// doing good things
return nil
})
assert.Equal(t, errors.NewUnexpected("error"), err)
mock.AssertExpectations(t)
}
func TestRepo_Transaction_commitError(t *testing.T) {
mock := new(TestAdapter)
mock.On("Begin").Return(nil).
On("Commit").Return(errors.NewUnexpected("error"))
err := Repo{adapter: mock}.Transaction(func(r Repo) error {
// doing good things
return nil
})
assert.Equal(t, errors.NewUnexpected("error"), err)
mock.AssertExpectations(t)
}
func TestRepo_Transaction_returnErrorAndRollback(t *testing.T) {
mock := new(TestAdapter)
mock.On("Begin").Return(nil).
On("Rollback").Return(nil)
err := Repo{adapter: mock}.Transaction(func(r Repo) error {
// doing good things
return errors.NewUnexpected("error")
})
assert.Equal(t, errors.NewUnexpected("error"), err)
mock.AssertExpectations(t)
}
func TestRepo_Transaction_panicWithKnownErrorAndRollback(t *testing.T) {
mock := new(TestAdapter)
mock.On("Begin").Return(nil).
On("Rollback").Return(nil)
err := Repo{adapter: mock}.Transaction(func(r Repo) error {
// doing good things
panic(errors.New("error", "", errors.NotFound))
})
assert.Equal(t, errors.New("error", "", errors.NotFound), err)
mock.AssertExpectations(t)
}
func TestRepo_Transaction_panicAndRollback(t *testing.T) {
mock := new(TestAdapter)
mock.On("Begin").Return(nil).
On("Rollback").Return(nil)
assert.Panics(t, func() {
Repo{adapter: mock}.Transaction(func(r Repo) error {
// doing good things
panic(errors.NewUnexpected("error"))
})
})
mock.AssertExpectations(t)
}