Skip to content

Commit 87a93b0

Browse files
Add pubsub test and remove mock_interface from coverage (#366)
1 parent 18fe524 commit 87a93b0

File tree

7 files changed

+401
-25
lines changed

7 files changed

+401
-25
lines changed

.github/workflows/go.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ jobs:
152152
run: |
153153
export GOFR_ENV=test
154154
go test gofr.dev/pkg/... -tags migration -v -short -coverprofile packageWithMigration.cov -coverpkg=gofr.dev/pkg/...
155-
grep -v 'gofr.dev/pkg/gofr/migration' packageWithMigration.cov > profile.cov
155+
grep -v 'gofr.dev/pkg/gofr/migration' packageWithMigration.cov > packageWithoutMigration.cov
156+
grep -v 'google/mock_interfaces\.go' packageWithoutMigration.cov > profile.cov
156157
go tool cover -func profile.cov
157158
158159
- name: Upload Test Coverage
@@ -216,7 +217,6 @@ jobs:
216217
awk '!/^mode: / && FNR==1{print "mode: set"} {print}' ./Example-Test-Report/profile.cov > merged_profile.cov
217218
tail -n +2 ./PKG-Coverage-Report/profile.cov >> merged_profile.cov
218219
219-
220220
- name: Upload
221221
uses: paambaati/[email protected]
222222
env:

pkg/gofr/datasource/pubsub/google/google.go

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,11 @@ type Config struct {
2424
type googleClient struct {
2525
Config
2626

27-
client *gcPubSub.Client
27+
client Client
2828
logger pubsub.Logger
2929
metrics Metrics
3030
}
3131

32-
func (g *googleClient) DeleteTopic(ctx context.Context, name string) error {
33-
topic := g.client.Topic(name)
34-
35-
err := topic.Delete(ctx)
36-
37-
if err != nil && strings.Contains(err.Error(), "Topic not found") {
38-
return nil
39-
}
40-
41-
return err
42-
}
43-
44-
func (g *googleClient) CreateTopic(ctx context.Context, name string) error {
45-
_, err := g.client.CreateTopic(ctx, name)
46-
47-
if err != nil && strings.Contains(err.Error(), "Topic already exists") {
48-
return nil
49-
}
50-
51-
return err
52-
}
53-
5432
//nolint:revive // We do not want anyone using the client without initialization steps.
5533
func New(conf Config, logger pubsub.Logger, metrics Metrics) *googleClient {
5634
err := validateConfigs(&conf)
@@ -192,3 +170,23 @@ func (g *googleClient) getSubscription(ctx context.Context, topic *gcPubSub.Topi
192170

193171
return subscription, nil
194172
}
173+
174+
func (g *googleClient) DeleteTopic(ctx context.Context, name string) error {
175+
err := g.client.Topic(name).Delete(ctx)
176+
177+
if err != nil && strings.Contains(err.Error(), "Topic not found") {
178+
return nil
179+
}
180+
181+
return err
182+
}
183+
184+
func (g *googleClient) CreateTopic(ctx context.Context, name string) error {
185+
_, err := g.client.CreateTopic(ctx, name)
186+
187+
if err != nil && strings.Contains(err.Error(), "Topic already exists") {
188+
return nil
189+
}
190+
191+
return err
192+
}

pkg/gofr/datasource/pubsub/google/google_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,50 @@ func Test_validateConfigs(t *testing.T) {
169169
assert.Equal(t, tc.expErr, err)
170170
}
171171
}
172+
173+
func TestGoogleClient_CreateTopicSuccess(t *testing.T) {
174+
ctrl := gomock.NewController(t)
175+
mock := NewMockClient(ctrl)
176+
177+
client := googleClient{
178+
client: mock,
179+
}
180+
181+
mock.EXPECT().CreateTopic(context.Background(), "test-topic").Return(&gcPubSub.Topic{}, nil)
182+
183+
err := client.CreateTopic(context.Background(), "test-topic")
184+
185+
assert.Nil(t, err)
186+
}
187+
188+
func TestGoogleClient_CreateTopic_AlreadyExist(t *testing.T) {
189+
ctrl := gomock.NewController(t)
190+
mock := NewMockClient(ctrl)
191+
192+
client := googleClient{
193+
client: mock,
194+
}
195+
196+
mock.EXPECT().CreateTopic(context.Background(), "test-topic").Return(&gcPubSub.Topic{},
197+
testutil.CustomError{ErrorMessage: "Topic already exists"})
198+
199+
err := client.CreateTopic(context.Background(), "test-topic")
200+
201+
assert.Nil(t, err)
202+
}
203+
204+
func TestGoogleClient_CreateTopicFailure(t *testing.T) {
205+
ctrl := gomock.NewController(t)
206+
mock := NewMockClient(ctrl)
207+
208+
client := googleClient{
209+
client: mock,
210+
}
211+
212+
mock.EXPECT().CreateTopic(context.Background(), "test-topic").Return(&gcPubSub.Topic{},
213+
testutil.CustomError{ErrorMessage: "Unknown Error"})
214+
215+
err := client.CreateTopic(context.Background(), "test-topic")
216+
217+
assert.NotNil(t, err)
218+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package google
2+
3+
import (
4+
"context"
5+
6+
"cloud.google.com/go/pubsub"
7+
)
8+
9+
type Client interface {
10+
Writer
11+
Reader
12+
13+
Close() error
14+
Topics(ctx context.Context) *pubsub.TopicIterator
15+
Subscriptions(ctx context.Context) *pubsub.SubscriptionIterator
16+
}
17+
18+
type Writer interface {
19+
Subscription(id string) *pubsub.Subscription
20+
CreateSubscription(ctx context.Context, id string, cfg pubsub.SubscriptionConfig) (*pubsub.Subscription, error)
21+
}
22+
23+
type Reader interface {
24+
Topic(id string) *pubsub.Topic
25+
CreateTopic(ctx context.Context, topicID string) (*pubsub.Topic, error)
26+
}

0 commit comments

Comments
 (0)