Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/dbtest/cost_center.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 NewCostCenter(t *testing.T, record db.CostCenter) db.CostCenter {
18
t.Helper()
19
20
result := db.CostCenter{
21
ID: db.NewTeamAttributionID(uuid.New().String()),
22
CreationTime: db.NewVarCharTime(time.Now()),
23
SpendingLimit: 100,
24
BillingStrategy: db.CostCenter_Stripe,
25
BillingCycleStart: db.NewVarCharTime(time.Now()),
26
NextBillingTime: db.NewVarCharTime(time.Now().Add(10 * time.Hour)),
27
}
28
29
if record.ID != "" {
30
result.ID = record.ID
31
}
32
if record.CreationTime.IsSet() {
33
result.CreationTime = record.CreationTime
34
}
35
if record.SpendingLimit != 0 {
36
result.SpendingLimit = record.SpendingLimit
37
}
38
if record.BillingStrategy != "" {
39
result.BillingStrategy = record.BillingStrategy
40
}
41
42
result.BillingCycleStart = record.BillingCycleStart
43
result.NextBillingTime = record.NextBillingTime
44
45
return result
46
}
47
48
func CreateCostCenters(t *testing.T, conn *gorm.DB, entries ...db.CostCenter) []db.CostCenter {
49
t.Helper()
50
51
var records []db.CostCenter
52
var ids []string
53
for _, entry := range entries {
54
record := NewCostCenter(t, entry)
55
records = append(records, record)
56
ids = append(ids, string(record.ID))
57
}
58
59
tx := conn.CreateInBatches(records, 100)
60
require.NoError(t, tx.Error)
61
t.Cleanup(func() {
62
if len(ids) > 0 {
63
require.NoError(t, conn.Where(ids).Delete(&db.CostCenter{}).Error)
64
}
65
})
66
67
return records
68
}
69
70