Path: blob/main/components/gitpod-db/go/dbtest/cost_center.go
2500 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package dbtest56import (7"testing"8"time"910db "github.com/gitpod-io/gitpod/components/gitpod-db/go"11"github.com/google/uuid"12"github.com/stretchr/testify/require"13"gorm.io/gorm"14)1516func NewCostCenter(t *testing.T, record db.CostCenter) db.CostCenter {17t.Helper()1819result := db.CostCenter{20ID: db.NewTeamAttributionID(uuid.New().String()),21CreationTime: db.NewVarCharTime(time.Now()),22SpendingLimit: 100,23BillingStrategy: db.CostCenter_Stripe,24BillingCycleStart: db.NewVarCharTime(time.Now()),25NextBillingTime: db.NewVarCharTime(time.Now().Add(10 * time.Hour)),26}2728if record.ID != "" {29result.ID = record.ID30}31if record.CreationTime.IsSet() {32result.CreationTime = record.CreationTime33}34if record.SpendingLimit != 0 {35result.SpendingLimit = record.SpendingLimit36}37if record.BillingStrategy != "" {38result.BillingStrategy = record.BillingStrategy39}4041result.BillingCycleStart = record.BillingCycleStart42result.NextBillingTime = record.NextBillingTime4344return result45}4647func CreateCostCenters(t *testing.T, conn *gorm.DB, entries ...db.CostCenter) []db.CostCenter {48t.Helper()4950var records []db.CostCenter51var ids []string52for _, entry := range entries {53record := NewCostCenter(t, entry)54records = append(records, record)55ids = append(ids, string(record.ID))56}5758tx := conn.CreateInBatches(records, 100)59require.NoError(t, tx.Error)60t.Cleanup(func() {61if len(ids) > 0 {62require.NoError(t, conn.Where(ids).Delete(&db.CostCenter{}).Error)63}64})6566return records67}686970