Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/dbtest/stripe_customer.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
"fmt"
9
"testing"
10
"time"
11
12
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
13
"github.com/google/uuid"
14
"github.com/stretchr/testify/require"
15
"gorm.io/gorm"
16
)
17
18
func NewStripeCustomer(t *testing.T, customer db.StripeCustomer) db.StripeCustomer {
19
t.Helper()
20
21
result := db.StripeCustomer{
22
StripeCustomerID: fmt.Sprintf("cus_%s", uuid.New().String()),
23
AttributionID: db.NewTeamAttributionID(uuid.New().String()),
24
CreationTime: db.NewVarCharTime(time.Now()),
25
}
26
27
if customer.StripeCustomerID != "" {
28
result.StripeCustomerID = customer.StripeCustomerID
29
}
30
31
if customer.AttributionID != "" {
32
result.AttributionID = customer.AttributionID
33
}
34
if customer.CreationTime.IsSet() {
35
result.CreationTime = customer.CreationTime
36
}
37
38
return result
39
}
40
41
func CreateStripeCustomers(t *testing.T, conn *gorm.DB, customers ...db.StripeCustomer) []db.StripeCustomer {
42
t.Helper()
43
44
var records []db.StripeCustomer
45
var ids []string
46
for _, c := range customers {
47
record := NewStripeCustomer(t, c)
48
records = append(records, record)
49
ids = append(ids, record.StripeCustomerID)
50
}
51
52
require.NoError(t, conn.CreateInBatches(&records, 1000).Error)
53
54
t.Cleanup(func() {
55
require.NoError(t, conn.Where(ids).Delete(&db.StripeCustomer{}).Error)
56
})
57
58
return records
59
}
60
61