Skip to content

Commit ad5ad41

Browse files
author
Andrew Farries
committed
WIP: Update api tests
1 parent 6c1a80c commit ad5ad41

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

Diff for: components/usage/pkg/apiv1/usage_test.go

+30-20
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,23 @@ package apiv1
66

77
import (
88
"context"
9+
"database/sql"
910
"testing"
1011

1112
"github.com/gitpod-io/gitpod/common-go/baseserver"
1213
v1 "github.com/gitpod-io/gitpod/usage-api/v1"
14+
"github.com/gitpod-io/gitpod/usage/pkg/db"
1315
"github.com/gitpod-io/gitpod/usage/pkg/db/dbtest"
14-
"github.com/google/go-cmp/cmp"
16+
"github.com/google/uuid"
1517
"github.com/stretchr/testify/require"
1618
"google.golang.org/grpc"
1719
"google.golang.org/grpc/codes"
1820
"google.golang.org/grpc/credentials/insecure"
1921
"google.golang.org/grpc/status"
20-
"google.golang.org/protobuf/testing/protocmp"
22+
"google.golang.org/protobuf/types/known/timestamppb"
2123
)
2224

2325
func TestUsageService_GetBilledUsage(t *testing.T) {
24-
const (
25-
attributionID = "team:123-456-789"
26-
)
27-
2826
srv := baseserver.NewForTests(t,
2927
baseserver.WithGRPC(baseserver.MustUseRandomLocalAddress(t)),
3028
)
@@ -39,9 +37,25 @@ func TestUsageService_GetBilledUsage(t *testing.T) {
3937
client := v1.NewUsageServiceClient(conn)
4038
ctx := context.Background()
4139

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+
4256
type Expectation struct {
43-
Code codes.Code
44-
Response *v1.GetBilledUsageResponse
57+
Code codes.Code
58+
InstanceIds []string
4559
}
4660

4761
scenarios := []struct {
@@ -50,13 +64,11 @@ func TestUsageService_GetBilledUsage(t *testing.T) {
5064
Expect Expectation
5165
}{
5266
{
53-
name: "returns a dummy response",
67+
name: "returns one usage record",
5468
AttributionID: attributionID,
5569
Expect: Expectation{
56-
Code: codes.OK,
57-
Response: &v1.GetBilledUsageResponse{
58-
Sessions: []*v1.BilledSession{},
59-
},
70+
Code: codes.OK,
71+
InstanceIds: []string{instanceId.String()},
6072
},
6173
},
6274
}
@@ -66,14 +78,12 @@ func TestUsageService_GetBilledUsage(t *testing.T) {
6678
resp, err := client.GetBilledUsage(ctx, &v1.GetBilledUsageRequest{
6779
AttributionId: scenario.AttributionID,
6880
})
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)
7484
}
85+
require.Equal(t, scenario.Expect.Code, status.Code(err))
86+
require.Equal(t, scenario.Expect.InstanceIds, instanceIds)
7587
})
76-
7788
}
78-
7989
}

0 commit comments

Comments
 (0)