-
Notifications
You must be signed in to change notification settings - Fork 0
/
must_exists_test.go
152 lines (139 loc) · 5.5 KB
/
must_exists_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package lightweight_api
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/ssst0n3/lightweight_api/test/model"
"github.com/ssst0n3/lightweight_api/test/test_data"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func WrapperTestResourceMustResourceExistsById(t *testing.T, router *gin.Engine) {
t.Run("not exists", func(t *testing.T) {
assert.NoError(t, test_data.InitEmptyChallenge(DB))
req, _ := http.NewRequest(http.MethodGet, challenge.BaseRelativePath+"/1", nil)
w := ObjectOperate(req, router)
assert.Equal(t, http.StatusBadRequest, w.Code)
})
t.Run("exists", func(t *testing.T) {
assert.NoError(t, test_data.InitEmptyChallenge(DB))
DB.Create(&test_data.Challenge1)
req, _ := http.NewRequest(http.MethodGet, challenge.BaseRelativePath+fmt.Sprintf("/%d", test_data.Challenge1.ID), nil)
w := ObjectOperate(req, router)
assert.Equal(t, http.StatusOK, w.Code)
})
}
func TestResource_MustResourceExistsGetModelByIdAutoParseParam(t *testing.T) {
router := gin.Default()
var resource model.Challenge
router.GET(challenge.BaseRelativePath+"/:id", func(context *gin.Context) {
_, m, _ := challenge.MustResourceExistsGetModelByIdAutoParseParam(context)
if m != nil {
resource = m.(model.Challenge)
}
})
WrapperTestResourceMustResourceExistsById(t, router)
assert.Equal(t, test_data.Challenge1.Name, resource.Name)
}
func TestResource_MustResourceExistsByIdAutoParseParam(t *testing.T) {
router := gin.Default()
var id int64
router.GET(challenge.BaseRelativePath+"/:id", func(context *gin.Context) {
id, _ = challenge.MustResourceExistsByIdAutoParseParam(context)
})
WrapperTestResourceMustResourceExistsById(t, router)
assert.Equal(t, test_data.Challenge1.ID, uint(id))
}
func TestResource_MustResourceExistsById(t *testing.T) {
router := gin.Default()
router.GET(challenge.BaseRelativePath+"/:id", func(context *gin.Context) {
_ = challenge.MustResourceExistsById(context, test_data.Challenge1.ID)
})
WrapperTestResourceMustResourceExistsById(t, router)
}
func WrapperTestResourceMustResourceExistsByGuid(t *testing.T, executor func(c *gin.Context) error) {
t.Run("not exists", func(t *testing.T) {
assert.NoError(t, test_data.InitEmptyChallenge(DB))
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
assert.Error(t, executor(c))
assert.Equal(t, http.StatusBadRequest, w.Code)
})
t.Run("exists", func(t *testing.T) {
assert.NoError(t, test_data.InitEmptyChallenge(DB))
DB.Create(&test_data.Challenge1)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
assert.NoError(t, executor(c))
assert.Equal(t, http.StatusOK, w.Code)
})
}
func TestResource_MustResourceExistsByGuid(t *testing.T) {
WrapperTestResourceMustResourceExistsByGuid(t, func(c *gin.Context) error {
return challenge.MustResourceExistsByGuid(c, challenge.GuidFieldJsonTag, test_data.Challenge1.Name)
})
}
func WrapperTestResourceMustResourceNotExistsByGuid(t *testing.T, executor func(c *gin.Context) error) {
t.Run("not exists", func(t *testing.T) {
assert.NoError(t, test_data.InitEmptyChallenge(DB))
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
assert.NoError(t, executor(c))
assert.Equal(t, http.StatusOK, w.Code)
})
t.Run("exists", func(t *testing.T) {
assert.NoError(t, test_data.InitEmptyChallenge(DB))
DB.Create(&test_data.Challenge1)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
assert.Error(t, executor(c))
assert.Equal(t, http.StatusBadRequest, w.Code)
})
}
func TestResource_MustResourceNotExistsByGuid(t *testing.T) {
WrapperTestResourceMustResourceNotExistsByGuid(t, func(c *gin.Context) error {
return challenge.MustResourceNotExistsByGuid(c, challenge.GuidFieldJsonTag, test_data.Challenge1.Name)
})
}
func TestResource_MustResourceNotExistsByModelPtrWithGuid(t *testing.T) {
WrapperTestResourceMustResourceNotExistsByGuid(t, func(c *gin.Context) error {
return challenge.MustResourceNotExistsByModelPtrWithGuid(c, &test_data.Challenge1, challenge.GuidFieldJsonTag)
})
}
func WrapperTestResourceMustResourceNotExistsExceptSelf(t *testing.T, executor func(c *gin.Context) error) {
t.Run("not exists", func(t *testing.T) {
assert.NoError(t, test_data.InitEmptyChallenge(DB))
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
assert.NoError(t, executor(c))
assert.Equal(t, http.StatusOK, w.Code)
})
t.Run("not exists2", func(t *testing.T) {
assert.NoError(t, test_data.InitEmptyChallenge(DB))
DB.Create(&test_data.Challenge1)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
assert.NoError(t, executor(c))
assert.Equal(t, http.StatusOK, w.Code)
})
t.Run("exists", func(t *testing.T) {
assert.NoError(t, test_data.InitEmptyChallenge(DB))
DB.Create(&test_data.Challenge1)
DB.Create(&test_data.ChallengeSameName)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
assert.Error(t, executor(c))
assert.Equal(t, http.StatusBadRequest, w.Code)
})
}
func TestResource_MustResourceNotExistsExceptSelfByGuid(t *testing.T) {
WrapperTestResourceMustResourceNotExistsExceptSelf(t, func(c *gin.Context) error {
return challenge.MustResourceNotExistsExceptSelfByGuid(c, challenge.GuidFieldJsonTag, test_data.Challenge1.Name, test_data.Challenge1.ID)
})
}
func TestResource_MustResourceNotExistsExceptSelfByModelPtrWithGuid(t *testing.T) {
WrapperTestResourceMustResourceNotExistsExceptSelf(t, func(c *gin.Context) error {
return challenge.MustResourceNotExistsExceptSelfByModelPtrWithGuid(c, &test_data.Challenge1, challenge.GuidFieldJsonTag, test_data.Challenge1.ID)
})
}