-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfind_and_count_all_test.go
104 lines (85 loc) · 2.82 KB
/
find_and_count_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
package reltest
import (
"context"
"database/sql"
"testing"
"github.com/go-rel/rel"
"github.com/go-rel/rel/where"
"github.com/stretchr/testify/assert"
)
func TestFindAndCountAll(t *testing.T) {
var (
repo = New()
result []Book
books = []Book{
{ID: 1, Title: "Golang for dummies"},
{ID: 2, Title: "Rel for dummies"},
}
query = rel.Where(where.Like("title", "%dummies%")).Limit(10).Offset(10)
)
repo.ExpectFindAndCountAll(query).Result(books, 12)
count, err := repo.FindAndCountAll(context.TODO(), &result, query)
assert.Nil(t, err)
assert.Equal(t, 12, count)
assert.Equal(t, books, result)
repo.AssertExpectations(t)
repo.ExpectFindAndCountAll(query).Result(books, 12)
assert.NotPanics(t, func() {
count := repo.MustFindAndCountAll(context.TODO(), &result, query)
assert.Equal(t, books, result)
assert.Equal(t, 12, count)
})
repo.AssertExpectations(t)
}
func TestFindAndCountAll_error(t *testing.T) {
var (
repo = New()
result []Book
books = []Book{
{ID: 1, Title: "Golang for dummies"},
{ID: 2, Title: "Rel for dummies"},
}
query = rel.Where(where.Like("title", "%dummies%")).Limit(10).Offset(10)
)
repo.ExpectFindAndCountAll(query).ConnectionClosed()
count, err := repo.FindAndCountAll(context.TODO(), &result, query)
assert.Equal(t, sql.ErrConnDone, err)
assert.Equal(t, 0, count)
assert.NotEqual(t, books, result)
repo.AssertExpectations(t)
repo.ExpectFindAndCountAll(query).ConnectionClosed()
assert.Panics(t, func() {
repo.MustFindAndCountAll(context.TODO(), &result, query)
})
assert.NotEqual(t, books, result)
repo.AssertExpectations(t)
}
func TestFindAndCountAll_assert(t *testing.T) {
var (
repo = New()
result []Book
)
repo.ExpectFindAndCountAll(where.Eq("title", "go"))
assert.Panics(t, func() {
repo.FindAndCountAll(context.TODO(), &result, where.Eq("title", "golang"))
})
assert.False(t, repo.AssertExpectations(nt))
assert.Equal(t, "FAIL: Mock defined but not called:\nFindAndCountAll(ctx, <Any>, rel.Where(where.Eq(\"title\", \"go\")))", nt.lastLog)
}
func TestFindAndCountAll_assert_transaction(t *testing.T) {
var (
repo = New()
)
repo.ExpectTransaction(func(repo *Repository) {
repo.ExpectFindAndCountAll(where.Eq("title", "go"))
})
assert.False(t, repo.AssertExpectations(nt))
assert.Equal(t, "FAIL: Mock defined but not called:\n<Transaction: 1> FindAndCountAll(ctx, <Any>, rel.Where(where.Eq(\"title\", \"go\")))", nt.lastLog)
}
func TestFindAndCountAll_String(t *testing.T) {
var (
mockFindAndCountAll = MockFindAndCountAll{assert: &Assert{}, argQuery: rel.Where(where.Eq("status", "paid"))}
)
assert.Equal(t, "FindAndCountAll(ctx, <Any>, rel.Where(where.Eq(\"status\", \"paid\")))", mockFindAndCountAll.String())
assert.Equal(t, "ExpectFindAndCountAll(rel.Where(where.Eq(\"status\", \"paid\")))", mockFindAndCountAll.ExpectString())
}