Skip to content

Commit 476e9ad

Browse files
author
Andrew Farries
committed
Add CreateWorkspaceInstanceUsageRecords to dbtest
To populate the test db with instance usage records.
1 parent aa1412d commit 476e9ad

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
"github.com/gitpod-io/gitpod/usage/pkg/db"
9+
"github.com/stretchr/testify/require"
10+
"gorm.io/gorm"
11+
"testing"
12+
)
13+
14+
func CreateWorkspaceInstanceUsageRecords(t *testing.T, conn *gorm.DB, instancesUsages ...db.WorkspaceInstanceUsage) []db.WorkspaceInstanceUsage {
15+
t.Helper()
16+
17+
var ids []string
18+
for _, instanceUsage := range instancesUsages {
19+
ids = append(ids, instanceUsage.InstanceID.String())
20+
}
21+
22+
require.NoError(t, conn.CreateInBatches(&instancesUsages, 1000).Error)
23+
24+
t.Cleanup(func() {
25+
require.NoError(t, conn.Where(ids).Delete(&db.WorkspaceInstanceUsage{}).Error)
26+
})
27+
28+
return instancesUsages
29+
}

0 commit comments

Comments
 (0)