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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
// Copyright 2022 The Gitea Authors. All rights reserved.
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package packages_test
import (
"testing"
"code.gitea.io/gitea/models/db"
packages_model "code.gitea.io/gitea/models/packages"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
_ "code.gitea.io/gitea/models"
_ "code.gitea.io/gitea/models/actions"
_ "code.gitea.io/gitea/models/activities"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
unittest.MainTest(m)
}
func prepareExamplePackage(t *testing.T) *packages_model.Package {
require.NoError(t, unittest.PrepareTestDatabase())
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
p0 := &packages_model.Package{
OwnerID: owner.ID,
RepoID: repo.ID,
LowerName: "package",
Type: packages_model.TypeGeneric,
}
p, err := packages_model.TryInsertPackage(db.DefaultContext, p0)
require.NotNil(t, p)
require.NoError(t, err)
require.Equal(t, *p0, *p)
return p
}
func deletePackage(t *testing.T, p *packages_model.Package) {
err := packages_model.DeletePackageByID(db.DefaultContext, p.ID)
require.NoError(t, err)
}
func TestTryInsertPackage(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
p0 := &packages_model.Package{
OwnerID: owner.ID,
LowerName: "package",
}
// Insert package should return the package and yield no error
p, err := packages_model.TryInsertPackage(db.DefaultContext, p0)
require.NotNil(t, p)
require.NoError(t, err)
require.Equal(t, *p0, *p)
// Insert same package again should return the same package and yield ErrDuplicatePackage
p, err = packages_model.TryInsertPackage(db.DefaultContext, p0)
require.NotNil(t, p)
require.IsType(t, packages_model.ErrDuplicatePackage, err)
require.Equal(t, *p0, *p)
err = packages_model.DeletePackageByID(db.DefaultContext, p0.ID)
require.NoError(t, err)
}
func TestGetPackageByID(t *testing.T) {
p0 := prepareExamplePackage(t)
// Get package should return package and yield no error
p, err := packages_model.GetPackageByID(db.DefaultContext, p0.ID)
require.NotNil(t, p)
require.Equal(t, *p0, *p)
require.NoError(t, err)
// Get package with non-existng ID should yield ErrPackageNotExist
p, err = packages_model.GetPackageByID(db.DefaultContext, 999)
require.Nil(t, p)
require.Error(t, err)
require.IsType(t, packages_model.ErrPackageNotExist, err)
deletePackage(t, p0)
}
func TestDeletePackageByID(t *testing.T) {
p0 := prepareExamplePackage(t)
// Delete existing package should yield no error
err := packages_model.DeletePackageByID(db.DefaultContext, p0.ID)
require.NoError(t, err)
// Delete (now) non-existing package should yield ErrPackageNotExist
err = packages_model.DeletePackageByID(db.DefaultContext, p0.ID)
require.Error(t, err)
require.IsType(t, packages_model.ErrPackageNotExist, err)
}
func TestSetRepositoryLink(t *testing.T) {
p0 := prepareExamplePackage(t)
// Set repository link to package should yield no error and package RepoID should be updated
err := packages_model.SetRepositoryLink(db.DefaultContext, p0.ID, 5)
require.NoError(t, err)
p, err := packages_model.GetPackageByID(db.DefaultContext, p0.ID)
require.NoError(t, err)
require.EqualValues(t, 5, p.RepoID)
// Set repository link to non-existing package should yied ErrPackageNotExist
err = packages_model.SetRepositoryLink(db.DefaultContext, 999, 5)
require.Error(t, err)
require.IsType(t, packages_model.ErrPackageNotExist, err)
deletePackage(t, p0)
}
func TestUnlinkRepositoryFromAllPackages(t *testing.T) {
p0 := prepareExamplePackage(t)
// Unlink repository from all packages should yield no error and package with p0.ID should have RepoID 0
err := packages_model.UnlinkRepositoryFromAllPackages(db.DefaultContext, p0.RepoID)
require.NoError(t, err)
p, err := packages_model.GetPackageByID(db.DefaultContext, p0.ID)
require.NoError(t, err)
require.EqualValues(t, 0, p.RepoID)
// Unlink repository again from all packages should also yield no error
err = packages_model.UnlinkRepositoryFromAllPackages(db.DefaultContext, p0.RepoID)
require.NoError(t, err)
deletePackage(t, p0)
}
func TestGetPackageByName(t *testing.T) {
p0 := prepareExamplePackage(t)
// Get package should return package and yield no error
p, err := packages_model.GetPackageByName(db.DefaultContext, p0.OwnerID, p0.Type, p0.LowerName)
require.NotNil(t, p)
require.Equal(t, *p0, *p)
require.NoError(t, err)
// Get package with uppercase name should return package and yield no error
p, err = packages_model.GetPackageByName(db.DefaultContext, p0.OwnerID, p0.Type, "Package")
require.NotNil(t, p)
require.Equal(t, *p0, *p)
require.NoError(t, err)
// Get package with wrong owner ID, type or name should return no package and yield ErrPackageNotExist
p, err = packages_model.GetPackageByName(db.DefaultContext, 999, p0.Type, p0.LowerName)
require.Nil(t, p)
require.Error(t, err)
require.IsType(t, packages_model.ErrPackageNotExist, err)
p, err = packages_model.GetPackageByName(db.DefaultContext, p0.OwnerID, packages_model.TypeDebian, p0.LowerName)
require.Nil(t, p)
require.Error(t, err)
require.IsType(t, packages_model.ErrPackageNotExist, err)
p, err = packages_model.GetPackageByName(db.DefaultContext, p0.OwnerID, p0.Type, "package1")
require.Nil(t, p)
require.Error(t, err)
require.IsType(t, packages_model.ErrPackageNotExist, err)
deletePackage(t, p0)
}
func TestHasCountPackages(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
p, err := packages_model.TryInsertPackage(db.DefaultContext, &packages_model.Package{
OwnerID: owner.ID,
RepoID: repo.ID,
LowerName: "package",
})
require.NotNil(t, p)
require.NoError(t, err)
// A package without package versions gets automatically cleaned up and should return false for owner
has, err := packages_model.HasOwnerPackages(db.DefaultContext, owner.ID)
require.False(t, has)
require.NoError(t, err)
count, err := packages_model.CountOwnerPackages(db.DefaultContext, owner.ID)
require.EqualValues(t, 0, count)
require.NoError(t, err)
// A package without package versions gets automatically cleaned up and should return false for repository
has, err = packages_model.HasRepositoryPackages(db.DefaultContext, repo.ID)
require.False(t, has)
require.NoError(t, err)
count, err = packages_model.CountRepositoryPackages(db.DefaultContext, repo.ID)
require.EqualValues(t, 0, count)
require.NoError(t, err)
pv, err := packages_model.GetOrInsertVersion(db.DefaultContext, &packages_model.PackageVersion{
PackageID: p.ID,
LowerVersion: "internal",
IsInternal: true,
})
require.NotNil(t, pv)
require.NoError(t, err)
// A package with an internal package version gets automatically cleaned up and should return false
has, err = packages_model.HasOwnerPackages(db.DefaultContext, owner.ID)
require.False(t, has)
require.NoError(t, err)
count, err = packages_model.CountOwnerPackages(db.DefaultContext, owner.ID)
require.EqualValues(t, 0, count)
require.NoError(t, err)
has, err = packages_model.HasRepositoryPackages(db.DefaultContext, repo.ID)
require.False(t, has)
require.NoError(t, err)
count, err = packages_model.CountRepositoryPackages(db.DefaultContext, repo.ID)
require.EqualValues(t, 0, count)
require.NoError(t, err)
pv, err = packages_model.GetOrInsertVersion(db.DefaultContext, &packages_model.PackageVersion{
PackageID: p.ID,
LowerVersion: "normal",
IsInternal: false,
})
require.NotNil(t, pv)
require.NoError(t, err)
// A package with a normal package version should return true
has, err = packages_model.HasOwnerPackages(db.DefaultContext, owner.ID)
require.True(t, has)
require.NoError(t, err)
count, err = packages_model.CountOwnerPackages(db.DefaultContext, owner.ID)
require.EqualValues(t, 1, count)
require.NoError(t, err)
has, err = packages_model.HasRepositoryPackages(db.DefaultContext, repo.ID)
require.True(t, has)
require.NoError(t, err)
count, err = packages_model.CountRepositoryPackages(db.DefaultContext, repo.ID)
require.EqualValues(t, 1, count)
require.NoError(t, err)
pv2, err := packages_model.GetOrInsertVersion(db.DefaultContext, &packages_model.PackageVersion{
PackageID: p.ID,
LowerVersion: "normal2",
IsInternal: false,
})
require.NotNil(t, pv2)
require.NoError(t, err)
// A package withmultiple package versions should be counted only once
has, err = packages_model.HasOwnerPackages(db.DefaultContext, owner.ID)
require.True(t, has)
require.NoError(t, err)
count, err = packages_model.CountOwnerPackages(db.DefaultContext, owner.ID)
require.EqualValues(t, 1, count)
require.NoError(t, err)
has, err = packages_model.HasRepositoryPackages(db.DefaultContext, repo.ID)
require.True(t, has)
require.NoError(t, err)
count, err = packages_model.CountRepositoryPackages(db.DefaultContext, repo.ID)
require.EqualValues(t, 1, count)
require.NoError(t, err)
// For owner ID 0 there should be no packages
has, err = packages_model.HasOwnerPackages(db.DefaultContext, 0)
require.False(t, has)
require.NoError(t, err)
count, err = packages_model.CountOwnerPackages(db.DefaultContext, 0)
require.EqualValues(t, 0, count)
require.NoError(t, err)
// For repo ID 0 there should be no packages
has, err = packages_model.HasRepositoryPackages(db.DefaultContext, 0)
require.False(t, has)
require.NoError(t, err)
count, err = packages_model.CountRepositoryPackages(db.DefaultContext, 0)
require.EqualValues(t, 0, count)
require.NoError(t, err)
p1, err := packages_model.TryInsertPackage(db.DefaultContext, &packages_model.Package{
OwnerID: owner.ID,
LowerName: "package0",
})
require.NotNil(t, p1)
require.NoError(t, err)
p1v, err := packages_model.GetOrInsertVersion(db.DefaultContext, &packages_model.PackageVersion{
PackageID: p1.ID,
LowerVersion: "normal",
IsInternal: false,
})
require.NotNil(t, p1v)
require.NoError(t, err)
// Owner owner.ID should have two packages now
has, err = packages_model.HasOwnerPackages(db.DefaultContext, owner.ID)
require.True(t, has)
require.NoError(t, err)
count, err = packages_model.CountOwnerPackages(db.DefaultContext, owner.ID)
require.EqualValues(t, 2, count)
require.NoError(t, err)
// For repo ID 0 there should be now one package, because p1 is not assigned to a repo
has, err = packages_model.HasRepositoryPackages(db.DefaultContext, 0)
require.True(t, has)
require.NoError(t, err)
count, err = packages_model.CountRepositoryPackages(db.DefaultContext, 0)
require.EqualValues(t, 1, count)
require.NoError(t, err)
}
|