Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/dbtest/user.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package dbtest
6
7
import (
8
"testing"
9
"time"
10
11
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
12
"github.com/google/uuid"
13
"github.com/stretchr/testify/require"
14
"gorm.io/gorm"
15
)
16
17
func NewUser(t *testing.T, user db.User) db.User {
18
t.Helper()
19
20
orgID := uuid.New()
21
22
userID := uuid.New()
23
result := db.User{
24
ID: userID,
25
OrganizationID: &orgID,
26
UsageAttributionID: db.NewTeamAttributionID(uuid.NewString()),
27
28
Name: "HomerJSimpson",
29
FullName: "Homer Simpson",
30
AvatarURL: "https://avatars.githubusercontent.com/u/9071",
31
32
CreationDate: db.NewVarCharTime(time.Now().Round(time.Second)),
33
34
Identities: []db.Identity{
35
NewIdentity(t, db.Identity{
36
UserID: userID,
37
}),
38
},
39
}
40
41
if user.ID != uuid.Nil {
42
result.ID = user.ID
43
}
44
if user.OrganizationID != nil {
45
result.OrganizationID = user.OrganizationID
46
}
47
if user.UsageAttributionID != "" {
48
result.UsageAttributionID = user.UsageAttributionID
49
}
50
51
if user.AvatarURL != "" {
52
result.AvatarURL = user.AvatarURL
53
}
54
if user.Name != "" {
55
result.Name = user.Name
56
}
57
if user.FullName != "" {
58
result.FullName = user.FullName
59
}
60
61
return result
62
}
63
64
func CreatUsers(t *testing.T, conn *gorm.DB, user ...db.User) []db.User {
65
t.Helper()
66
67
var records []db.User
68
var ids []uuid.UUID
69
for _, u := range user {
70
record := NewUser(t, u)
71
records = append(records, record)
72
ids = append(ids, record.ID)
73
}
74
75
require.NoError(t, conn.CreateInBatches(&records, 1000).Error)
76
77
t.Cleanup(func() {
78
if len(ids) > 0 {
79
require.NoError(t, conn.Where(ids).Delete(&db.User{}).Error)
80
}
81
})
82
83
return records
84
}
85
86