@@ -6,25 +6,23 @@ package apiv1
6
6
7
7
import (
8
8
"context"
9
+ "database/sql"
9
10
"testing"
10
11
11
12
"github.com/gitpod-io/gitpod/common-go/baseserver"
12
13
v1 "github.com/gitpod-io/gitpod/usage-api/v1"
14
+ "github.com/gitpod-io/gitpod/usage/pkg/db"
13
15
"github.com/gitpod-io/gitpod/usage/pkg/db/dbtest"
14
- "github.com/google/go-cmp/cmp "
16
+ "github.com/google/uuid "
15
17
"github.com/stretchr/testify/require"
16
18
"google.golang.org/grpc"
17
19
"google.golang.org/grpc/codes"
18
20
"google.golang.org/grpc/credentials/insecure"
19
21
"google.golang.org/grpc/status"
20
- "google.golang.org/protobuf/testing/protocmp "
22
+ "google.golang.org/protobuf/types/known/timestamppb "
21
23
)
22
24
23
25
func TestUsageService_GetBilledUsage (t * testing.T ) {
24
- const (
25
- attributionID = "team:123-456-789"
26
- )
27
-
28
26
srv := baseserver .NewForTests (t ,
29
27
baseserver .WithGRPC (baseserver .MustUseRandomLocalAddress (t )),
30
28
)
@@ -39,9 +37,25 @@ func TestUsageService_GetBilledUsage(t *testing.T) {
39
37
client := v1 .NewUsageServiceClient (conn )
40
38
ctx := context .Background ()
41
39
40
+ const attributionID = "team:123-456-789"
41
+ instanceId := uuid .New ()
42
+ startedAt := timestamppb .Now ()
43
+ instanceUsages := []db.WorkspaceInstanceUsage {
44
+ {
45
+ InstanceID : instanceId ,
46
+ AttributionID : attributionID ,
47
+ StartedAt : startedAt .AsTime (),
48
+ StoppedAt : sql.NullTime {},
49
+ CreditsUsed : 0 ,
50
+ GenerationId : 0 ,
51
+ Deleted : false ,
52
+ },
53
+ }
54
+ dbtest .CreateWorkspaceInstanceUsageRecords (t , dbconn , instanceUsages ... )
55
+
42
56
type Expectation struct {
43
- Code codes.Code
44
- Response * v1. GetBilledUsageResponse
57
+ Code codes.Code
58
+ InstanceIds [] string
45
59
}
46
60
47
61
scenarios := []struct {
@@ -50,13 +64,11 @@ func TestUsageService_GetBilledUsage(t *testing.T) {
50
64
Expect Expectation
51
65
}{
52
66
{
53
- name : "returns a dummy response " ,
67
+ name : "returns one usage record " ,
54
68
AttributionID : attributionID ,
55
69
Expect : Expectation {
56
- Code : codes .OK ,
57
- Response : & v1.GetBilledUsageResponse {
58
- Sessions : []* v1.BilledSession {},
59
- },
70
+ Code : codes .OK ,
71
+ InstanceIds : []string {instanceId .String ()},
60
72
},
61
73
},
62
74
}
@@ -66,14 +78,12 @@ func TestUsageService_GetBilledUsage(t *testing.T) {
66
78
resp , err := client .GetBilledUsage (ctx , & v1.GetBilledUsageRequest {
67
79
AttributionId : scenario .AttributionID ,
68
80
})
69
- if diff := cmp .Diff (scenario .Expect , Expectation {
70
- Code : status .Code (err ),
71
- Response : resp ,
72
- }, protocmp .Transform ()); diff != "" {
73
- t .Errorf ("unexpected difference:\n %v" , diff )
81
+ var instanceIds []string
82
+ for _ , billedSession := range resp .Sessions {
83
+ instanceIds = append (instanceIds , billedSession .InstanceId )
74
84
}
85
+ require .Equal (t , scenario .Expect .Code , status .Code (err ))
86
+ require .Equal (t , scenario .Expect .InstanceIds , instanceIds )
75
87
})
76
-
77
88
}
78
-
79
89
}
0 commit comments