-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository_test.go
92 lines (77 loc) · 2.1 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
package gocruddy_test
import (
"testing"
"github.com/Becklyn/gocruddy"
"github.com/Becklyn/gocruddy/test/mock"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"
)
func TestRepository_GetAllEntries(t *testing.T) {
repo := gocruddy.Repository{}
tx, rollback := mock.Transaction()
defer rollback()
entries, err := repo.GetAllEntries(tx, func(gorm *gorm.DB) *gorm.DB {
return gorm
}, &mock.Entity{})
assert.Nil(t, err)
assert.Len(t, entries, 0)
assert.Nil(t, repo.Insert(tx, &mock.Entity{
Model: gorm.Model{
ID: 123,
},
}))
entries, err = repo.GetAllEntries(tx, func(gorm *gorm.DB) *gorm.DB {
return gorm
}, &mock.Entity{})
assert.Nil(t, err)
assert.Len(t, entries, 1)
// filter error test
_, err = repo.GetAllEntries(tx, func(gorm *gorm.DB) *gorm.DB {
return gorm.Where("not-existing = ?")
}, &mock.Entity{})
assert.NotNil(t, err)
// filter test
entries, err = repo.GetAllEntries(tx, func(gorm *gorm.DB) *gorm.DB {
return gorm.Where("id = ?", 1)
}, &mock.Entity{})
assert.Nil(t, err)
assert.Len(t, entries, 0)
entries, err = repo.GetAllEntries(tx, func(gorm *gorm.DB) *gorm.DB {
return gorm.Where("id = ?", 123)
}, &mock.Entity{})
assert.Nil(t, err)
assert.Len(t, entries, 1)
}
func TestRepository_GetByID(t *testing.T) {
repo := gocruddy.Repository{}
tx, rollback := mock.Transaction()
defer rollback()
_, err := repo.GetByID(tx, 1, func(gorm *gorm.DB) *gorm.DB {
return gorm
}, &mock.Entity{})
assert.NotNil(t, err)
assert.Nil(t, repo.Insert(tx, &mock.Entity{
Model: gorm.Model{
ID: 1,
},
}))
assert.Nil(t, repo.Insert(tx, &mock.Entity{
Model: gorm.Model{
ID: 2,
},
}))
entry, err := repo.GetByID(tx, 1, func(gorm *gorm.DB) *gorm.DB {
return gorm
}, &mock.Entity{})
assert.Nil(t, err)
assert.Equal(t, uint(1), entry.(*mock.Entity).ID)
entry, err = repo.GetByID(tx, 2, func(gorm *gorm.DB) *gorm.DB {
return gorm
}, &mock.Entity{})
assert.Nil(t, err)
assert.Equal(t, uint(2), entry.(*mock.Entity).ID)
_, err = repo.GetByID(tx, 1, func(gorm *gorm.DB) *gorm.DB {
return gorm.Where("not-existing = ?")
}, &mock.Entity{})
assert.NotNil(t, err)
}