forked from go-rel/reltest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_test.go
100 lines (80 loc) · 2.49 KB
/
find_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
package reltest
import (
"context"
"testing"
"github.com/go-rel/rel"
"github.com/go-rel/rel/where"
"github.com/stretchr/testify/assert"
)
func TestFind(t *testing.T) {
var (
repo = New()
result Book
book = Book{ID: 2, Title: "Rel for dummies"}
)
repo.ExpectFind(where.Eq("id", 2)).Result(book)
assert.Nil(t, repo.Find(context.TODO(), &result, where.Eq("id", 2)))
assert.Equal(t, book, result)
repo.AssertExpectations(t)
repo.ExpectFind(where.Eq("id", 2)).Result(book)
assert.NotPanics(t, func() {
repo.MustFind(context.TODO(), &result, where.Eq("id", 2))
assert.Equal(t, book, result)
})
repo.AssertExpectations(t)
}
func TestFind_noResult(t *testing.T) {
var (
result Book
repo = New()
book = Book{ID: 2, Title: "Rel for dummies"}
)
repo.ExpectFind(where.Eq("id", 2)).NotFound()
assert.Equal(t, rel.NotFoundError{}, repo.Find(context.TODO(), &result, where.Eq("id", 2)))
assert.NotEqual(t, book, result)
repo.AssertExpectations(t)
repo.ExpectFind(where.Eq("id", 2)).NotFound()
assert.Panics(t, func() {
repo.MustFind(context.TODO(), &result, where.Eq("id", 2))
assert.NotEqual(t, book, result)
})
repo.AssertExpectations(t)
}
func TestFind_connectionClosed(t *testing.T) {
var (
result Book
repo = New()
)
repo.ExpectFind(where.Eq("id", 2)).ConnectionClosed()
assert.Equal(t, ErrConnectionClosed, repo.Find(context.TODO(), &result, where.Eq("id", 2)))
repo.AssertExpectations(t)
}
func TestFind_assert(t *testing.T) {
var (
repo = New()
result Book
)
repo.ExpectFind(where.Eq("title", "go"))
assert.Panics(t, func() {
repo.Find(context.TODO(), &result, where.Eq("title", "golang"))
})
assert.False(t, repo.AssertExpectations(nt))
assert.Equal(t, "FAIL: Mock defined but not called:\nFind(ctx, <Any>, rel.Where(where.Eq(\"title\", \"go\")))", nt.lastLog)
}
func TestFind_assert_transaction(t *testing.T) {
var (
repo = New()
)
repo.ExpectTransaction(func(repo *Repository) {
repo.ExpectFind(where.Eq("title", "go"))
})
assert.False(t, repo.AssertExpectations(nt))
assert.Equal(t, "FAIL: Mock defined but not called:\n<Transaction: 1> Find(ctx, <Any>, rel.Where(where.Eq(\"title\", \"go\")))", nt.lastLog)
}
func TestFind_String(t *testing.T) {
var (
mockFind = MockFind{assert: &Assert{}, argQuery: rel.Where(where.Eq("status", "paid"))}
)
assert.Equal(t, "Find(ctx, <Any>, rel.Where(where.Eq(\"status\", \"paid\")))", mockFind.String())
assert.Equal(t, "ExpectFind(rel.Where(where.Eq(\"status\", \"paid\")))", mockFind.ExpectString())
}