-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathentity_repository_go1.18_test.go
90 lines (73 loc) · 2.05 KB
/
entity_repository_go1.18_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
//go:build go1.18
// +build go1.18
package reltest
import (
"context"
"fmt"
"testing"
"github.com/go-rel/rel"
"github.com/go-rel/rel/where"
"github.com/stretchr/testify/assert"
)
func TestEntityRepository_Aggregate(t *testing.T) {
var (
repo = NewEntityRepository[Book]()
)
repo.ExpectAggregate(rel.From("books"), "sum", "id").Result(3)
sum, err := repo.Aggregate(context.TODO(), "sum", "id")
assert.Nil(t, err)
assert.Equal(t, 3, sum)
repo.AssertExpectations(t)
repo.ExpectAggregate(rel.From("books"), "sum", "id").Result(3)
assert.NotPanics(t, func() {
sum := repo.MustAggregate(context.TODO(), "sum", "id")
assert.Equal(t, 3, sum)
})
repo.AssertExpectations(t)
}
func TestEntityRepository_Count(t *testing.T) {
var (
repo = NewEntityRepository[Book]()
)
repo.ExpectCount("books").Result(2)
count, err := repo.Count(context.TODO())
assert.Nil(t, err)
assert.Equal(t, 2, count)
repo.AssertExpectations(t)
repo.ExpectCount("books").Result(2)
assert.NotPanics(t, func() {
count := repo.MustCount(context.TODO())
assert.Equal(t, 2, count)
})
repo.AssertExpectations(t)
}
func TestEntityRepository_Transaction(t *testing.T) {
var (
repo = NewEntityRepository[Book]()
result Book
book = Book{ID: 1, Title: "Golang for dummies", Ratings: []Rating{}}
)
repo.ExpectTransaction(func(txRepo *Repository) {
repo.ExpectInsert()
repo.ExpectTransaction(func(txRepo *Repository) {
repo := repo.WrapTransaction(txRepo)
repo.ExpectFind(where.Eq("id", 1)).Result(book)
repo.ExpectTransaction(func(txRepo *Repository) {
repo := repo.WrapTransaction(txRepo)
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 {
result = repo.MustFind(ctx, where.Eq("id", 1))
fmt.Println(result)
return repo.Transaction(ctx, func(ctx context.Context) error {
return repo.Delete(ctx, &result)
})
})
}))
assert.Equal(t, book, result)
repo.AssertExpectations(t)
}