-
Notifications
You must be signed in to change notification settings - Fork 13
/
mock_groups.go
105 lines (89 loc) · 2.69 KB
/
mock_groups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Copyright 2022 Chainguard, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package test
import (
"context"
"fmt"
iam "chainguard.dev/sdk/proto/platform/iam/v1"
"github.com/google/go-cmp/cmp"
"google.golang.org/grpc"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/emptypb"
)
var _ iam.GroupsClient = (*MockGroupsClient)(nil)
type MockGroupsClient struct {
OnCreate []GroupOnCreate
OnUpdate []GroupOnUpdate
OnDelete []GroupOnDelete
OnList []GroupOnList
}
type GroupOnCreate struct {
Given *iam.CreateGroupRequest
Created *iam.Group
Error error
}
type GroupOnUpdate struct {
Given *iam.Group
Updated *iam.Group
Error error
}
type GroupOnDelete struct {
Given *iam.DeleteGroupRequest
Error error
}
type GroupOnList struct {
Given *iam.GroupFilter
List *iam.GroupList
Error error
}
func (m MockGroupsClient) Create(_ context.Context, given *iam.CreateGroupRequest, _ ...grpc.CallOption) (*iam.Group, error) {
for _, o := range m.OnCreate {
if cmp.Equal(o.Given, given, protocmp.Transform()) {
return o.Created, o.Error
}
}
return nil, fmt.Errorf("mock not found for %v", given)
}
func (m MockGroupsClient) Update(_ context.Context, given *iam.Group, _ ...grpc.CallOption) (*iam.Group, error) {
for _, o := range m.OnUpdate {
if cmp.Equal(o.Given, given, protocmp.Transform()) {
return o.Updated, o.Error
}
}
return nil, fmt.Errorf("mock not found for %v", given)
}
func (m MockGroupsClient) Delete(_ context.Context, given *iam.DeleteGroupRequest, _ ...grpc.CallOption) (*emptypb.Empty, error) {
for _, o := range m.OnDelete {
if cmp.Equal(o.Given, given, protocmp.Transform()) {
return &emptypb.Empty{}, o.Error
}
}
return &emptypb.Empty{}, fmt.Errorf("mock not found for %v", given)
}
func (m MockGroupsClient) List(_ context.Context, given *iam.GroupFilter, _ ...grpc.CallOption) (*iam.GroupList, error) {
for _, o := range m.OnList {
if cmp.Equal(o.Given, given, protocmp.Transform()) {
return o.List, o.Error
}
}
return nil, fmt.Errorf("mock not found for %v", given)
}
// -- Server --
type MockGroupsServer struct {
iam.UnimplementedGroupsServer
Client MockGroupsClient
}
func (m MockGroupsServer) Create(ctx context.Context, req *iam.CreateGroupRequest) (*iam.Group, error) {
return m.Client.Create(ctx, req)
}
func (m MockGroupsServer) Update(ctx context.Context, req *iam.Group) (*iam.Group, error) {
return m.Client.Update(ctx, req)
}
func (m MockGroupsServer) List(ctx context.Context, req *iam.GroupFilter) (*iam.GroupList, error) {
return m.Client.List(ctx, req)
}
func (m MockGroupsServer) Delete(ctx context.Context, req *iam.DeleteGroupRequest) (*emptypb.Empty, error) {
return m.Client.Delete(ctx, req)
}