-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepository_test.go
124 lines (92 loc) · 2.72 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
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
121
122
123
124
package gormet
import (
"testing"
"github.com/stretchr/testify/assert"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func getGormConnection(t *testing.T, entity interface{}) *gorm.DB {
db, err := gorm.Open(sqlite.Open("./database.db"), &gorm.Config{})
assert.Nil(t, err)
db.AutoMigrate(entity)
return db
}
type testNew struct {
gorm.Model
SomeField string `json:"somefield" gorm:"unique;not null;default:null" validate:"required,min=3,max=50"`
}
func TestNew(t *testing.T) {
db := getGormConnection(t, &testNew{})
t.Run("Pagination active", func(t *testing.T) {
repo, err := New[testNew](db)
assert.Nil(t, err)
assert.NotNil(t, repo)
repo.PageSize = 10
assert.Equal(t, uint(10), repo.PageSize)
})
t.Run("Pagination Inactive", func(t *testing.T) {
repo, err := New[testNew](db)
assert.Nil(t, err)
assert.NotNil(t, repo)
repo.PageSize = 0
assert.Equal(t, uint(0), repo.PageSize)
})
t.Run("Struct with custom pk", func(t *testing.T) {
type CustomPK struct {
MyID string `json:"myid" gorm:"primaryKey;autoIncrement:false"`
}
db.AutoMigrate(CustomPK{})
repo, err := New[CustomPK](db)
assert.Nil(t, err)
assert.NotNil(t, repo)
assert.Equal(t, "my_id", repo.pkName)
})
t.Run("Struct without pk", func(t *testing.T) {
type NoPK struct {
MyID string `json:"myid" gorm:"autoIncrement:false"`
}
db.AutoMigrate(NoPK{})
repo, err := New[NoPK](db)
assert.NotNil(t, err)
assert.Nil(t, repo)
assert.Equal(t, "impossible to retrieve primary key: no primary key found", err.Error())
})
}
func Test_getPrimaryKeyFieldName(t *testing.T) {
db, err := gorm.Open(sqlite.Open("./database.db"), &gorm.Config{})
assert.Nil(t, err)
t.Run("Default Primary Key", func(t *testing.T) {
type DefaultPK struct {
gorm.Model
Name string `json:"name" gorm:"unique;not null;default:null"`
}
pk, err := getPrimaryKeyFieldName(db, DefaultPK{})
assert.Nil(t, err)
assert.NotNil(t, pk)
assert.Equal(t, "id", pk)
})
t.Run("Custom Primary Key", func(t *testing.T) {
type CustomPK struct {
Email string `json:"email" gorm:"primaryKey"`
Name string `json:"name" gorm:"not null;default:null"`
}
pk, err := getPrimaryKeyFieldName(db, CustomPK{})
assert.Nil(t, err)
assert.NotNil(t, pk)
assert.Equal(t, "email", pk)
})
t.Run("No Primary Key", func(t *testing.T) {
type NoPk struct {
Email string `json:"email" gorm:"unique;not null;default:null"`
Name string `json:"name" gorm:"not null;default:null"`
}
pk, err := getPrimaryKeyFieldName(db, NoPk{})
assert.NotNil(t, err)
assert.Equal(t, "", pk)
})
t.Run("Invalid struct", func(t *testing.T) {
pk, err := getPrimaryKeyFieldName(db, "Just a parser error string")
assert.NotNil(t, err)
assert.Equal(t, "", pk)
})
}