@@ -6,12 +6,14 @@ package controller
6
6
7
7
import (
8
8
"context"
9
+ "database/sql"
10
+ "testing"
11
+ "time"
12
+
9
13
"github.com/gitpod-io/gitpod/usage/pkg/db"
10
14
"github.com/gitpod-io/gitpod/usage/pkg/db/dbtest"
11
15
"github.com/google/uuid"
12
16
"github.com/stretchr/testify/require"
13
- "testing"
14
- "time"
15
17
)
16
18
17
19
func TestUsageReconciler_ReconcileTimeRange (t * testing.T ) {
@@ -145,3 +147,69 @@ func TestUsageReport_CreditSummaryForTeams(t *testing.T) {
145
147
})
146
148
}
147
149
}
150
+
151
+ func TestUsageReportConversionToDBUsageRecords (t * testing.T ) {
152
+ teamID := uuid .New ().String ()
153
+ teamAttributionID := db .NewTeamAttributionID (teamID )
154
+ instanceId := uuid .New ()
155
+ creationTime := db .NewVarcharTime (time .Date (2022 , 05 , 30 , 00 , 00 , 00 , 00 , time .UTC ))
156
+ stoppedTime := db .NewVarcharTime (time .Date (2022 , 06 , 1 , 1 , 0 , 0 , 0 , time .UTC ))
157
+
158
+ scenarios := []struct {
159
+ Name string
160
+ Report UsageReport
161
+ Expected []db.WorkspaceInstanceUsage
162
+ }{
163
+ {
164
+ Name : "a stopped workspace instance" ,
165
+ Report : map [db.AttributionID ][]db.WorkspaceInstanceForUsage {
166
+ teamAttributionID : {
167
+ db.WorkspaceInstanceForUsage {
168
+ ID : instanceId ,
169
+ UsageAttributionID : teamAttributionID ,
170
+ WorkspaceClass : defaultWorkspaceClass ,
171
+ CreationTime : creationTime ,
172
+ StoppedTime : stoppedTime ,
173
+ },
174
+ },
175
+ },
176
+ Expected : []db.WorkspaceInstanceUsage {{
177
+ InstanceID : instanceId ,
178
+ AttributionID : teamAttributionID ,
179
+ StartedAt : creationTime .Time (),
180
+ StoppedAt : sql.NullTime {Time : stoppedTime .Time (), Valid : true },
181
+ CreditsUsed : 0 ,
182
+ GenerationId : 0 ,
183
+ }},
184
+ },
185
+ {
186
+ Name : "workspace instance that is still running" ,
187
+ Report : map [db.AttributionID ][]db.WorkspaceInstanceForUsage {
188
+ teamAttributionID : {
189
+ db.WorkspaceInstanceForUsage {
190
+ ID : instanceId ,
191
+ WorkspaceClass : defaultWorkspaceClass ,
192
+ UsageAttributionID : teamAttributionID ,
193
+ CreationTime : db .NewVarcharTime (time .Date (2022 , 05 , 30 , 00 , 00 , 00 , 00 , time .UTC )),
194
+ StoppedTime : db.VarcharTime {},
195
+ },
196
+ },
197
+ },
198
+ Expected : []db.WorkspaceInstanceUsage {{
199
+ InstanceID : instanceId ,
200
+ AttributionID : teamAttributionID ,
201
+ StartedAt : creationTime .Time (),
202
+ StoppedAt : sql.NullTime {},
203
+ CreditsUsed : 0 ,
204
+ GenerationId : 0 ,
205
+ }},
206
+ },
207
+ }
208
+
209
+ for _ , s := range scenarios {
210
+ t .Run (s .Name , func (t * testing.T ) {
211
+ actual := usageReportToUsageRecords (s .Report )
212
+ require .Equal (t , s .Expected , actual )
213
+ })
214
+ }
215
+ }
0 commit comments