Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/dbtest/usage.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
"context"
9
"testing"
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 NewUsage(t *testing.T, record db.Usage) db.Usage {
18
t.Helper()
19
20
workspaceInstanceId := uuid.New()
21
22
result := db.Usage{
23
ID: uuid.New(),
24
AttributionID: db.NewTeamAttributionID(uuid.New().String()),
25
Description: "some description",
26
CreditCents: 42,
27
EffectiveTime: db.VarcharTime{},
28
Kind: db.WorkspaceInstanceUsageKind,
29
WorkspaceInstanceID: &workspaceInstanceId,
30
}
31
32
if record.ID.ID() != 0 {
33
result.ID = record.ID
34
}
35
if record.EffectiveTime.IsSet() {
36
result.EffectiveTime = record.EffectiveTime
37
}
38
if record.AttributionID != "" {
39
result.AttributionID = record.AttributionID
40
}
41
if record.Description != "" {
42
result.Description = record.Description
43
}
44
if record.CreditCents != 0 {
45
result.CreditCents = record.CreditCents
46
}
47
if record.WorkspaceInstanceID != nil && (*record.WorkspaceInstanceID).ID() != 0 {
48
result.WorkspaceInstanceID = record.WorkspaceInstanceID
49
}
50
if record.Kind != "" {
51
result.Kind = record.Kind
52
}
53
if record.Draft {
54
result.Draft = true
55
}
56
if record.Metadata != nil {
57
result.Metadata = record.Metadata
58
}
59
return result
60
}
61
62
func CreateUsageRecords(t *testing.T, conn *gorm.DB, entries ...db.Usage) []db.Usage {
63
t.Helper()
64
65
var records []db.Usage
66
var ids []string
67
for _, usageEntry := range entries {
68
record := NewUsage(t, usageEntry)
69
records = append(records, record)
70
ids = append(ids, record.ID.String())
71
}
72
73
require.NoError(t, db.InsertUsage(context.Background(), conn, entries...))
74
t.Cleanup(func() {
75
if len(ids) > 0 {
76
require.NoError(t, conn.Where(ids).Delete(&db.Usage{}).Error)
77
}
78
})
79
80
return records
81
}
82
83