Skip to content

Commit 9e9f5f7

Browse files
committed
add tests for subscriber handler after seperating them
1 parent dc6380a commit 9e9f5f7

File tree

2 files changed

+145
-27
lines changed

2 files changed

+145
-27
lines changed

examples/using-subscriber/main.go

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,45 @@ func main() {
1010

1111
app.Migrate(migrations.All())
1212

13-
app.Subscribe("products", func(c *gofr.Context) error {
14-
var productInfo struct {
15-
ProductId string `json:"productId"`
16-
Price string `json:"price"`
17-
}
13+
app.Subscribe("products", productHandler)
1814

19-
err := c.Bind(&productInfo)
20-
if err != nil {
21-
c.Logger.Error(err)
15+
app.Subscribe("order-logs", orderHandler)
2216

23-
return nil
24-
}
17+
app.Run()
18+
}
19+
20+
func productHandler(c *gofr.Context) error {
21+
var productInfo struct {
22+
ProductId string `json:"productId"`
23+
Price string `json:"price"`
24+
}
2525

26-
c.Logger.Info("Received product", productInfo)
26+
err := c.Bind(&productInfo)
27+
if err != nil {
28+
c.Logger.Error(err)
2729

2830
return nil
29-
})
31+
}
3032

31-
app.Subscribe("order-logs", func(c *gofr.Context) error {
32-
var orderStatus struct {
33-
OrderId string `json:"orderId"`
34-
Status string `json:"status"`
35-
}
33+
c.Logger.Info("Received product", productInfo)
3634

37-
err := c.Bind(&orderStatus)
38-
if err != nil {
39-
c.Logger.Error(err)
35+
return nil
36+
}
4037

41-
return nil
42-
}
38+
func orderHandler(c *gofr.Context) error {
39+
var orderStatus struct {
40+
OrderId string `json:"orderId"`
41+
Status string `json:"status"`
42+
}
4343

44-
c.Logger.Info("Received order", orderStatus)
44+
err := c.Bind(&orderStatus)
45+
if err != nil {
46+
c.Logger.Error(err)
4547

4648
return nil
47-
})
49+
}
4850

49-
app.Run()
51+
c.Logger.Info("Received order", orderStatus)
52+
53+
return nil
5054
}

examples/using-subscriber/main_test.go

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"os"
67
"strings"
78
"testing"
89
"time"
910

11+
"gofr.dev/pkg/gofr"
12+
"gofr.dev/pkg/gofr/container"
1013
"gofr.dev/pkg/gofr/datasource/pubsub/kafka"
1114
"gofr.dev/pkg/gofr/logging"
1215
"gofr.dev/pkg/gofr/testutil"
@@ -49,10 +52,10 @@ func TestExampleSubscriber(t *testing.T) {
4952
testutil.NewServerConfigs(t)
5053

5154
go main()
52-
time.Sleep(time.Second * 1) // Giving some time to start the server
55+
time.Sleep(time.Second * 30) // Giving some time to start the server
5356

5457
initializeTest(t)
55-
time.Sleep(time.Second * 20) // Giving some time to publish events
58+
time.Sleep(time.Second * 5) // Giving some time to publish events
5659
})
5760

5861
testCases := []struct {
@@ -75,3 +78,114 @@ func TestExampleSubscriber(t *testing.T) {
7578
}
7679
}
7780
}
81+
82+
type errorRequest struct{}
83+
84+
func (e *errorRequest) Context() context.Context {
85+
return context.Background()
86+
}
87+
88+
func (e *errorRequest) Bind(v interface{}) error { return errors.New("bind error") }
89+
func (e *errorRequest) Param(key string) string { return "" }
90+
func (e *errorRequest) PathParam(key string) string { return "" }
91+
func (e *errorRequest) HostName() string { return "" }
92+
func (e *errorRequest) Params(key string) []string { return nil }
93+
94+
func TestProductSubscribe_BindError(t *testing.T) {
95+
mockContainer, _ := container.NewMockContainer(t)
96+
97+
ctx := &gofr.Context{
98+
Request: &errorRequest{},
99+
Container: mockContainer,
100+
ContextLogger: *logging.NewContextLogger(context.Background(), mockContainer.Logger),
101+
}
102+
103+
err := productHandler(ctx)
104+
105+
if err != nil {
106+
t.Errorf("Expected nil error, got %v", err)
107+
}
108+
}
109+
110+
func TestOrderSubscribe_BindError(t *testing.T) {
111+
mockContainer, _ := container.NewMockContainer(t)
112+
113+
ctx := &gofr.Context{
114+
Request: &errorRequest{},
115+
Container: mockContainer,
116+
ContextLogger: *logging.NewContextLogger(context.Background(), mockContainer.Logger),
117+
}
118+
119+
err := orderHandler(ctx)
120+
if err != nil {
121+
t.Errorf("Expected nil error, got %v", err)
122+
}
123+
}
124+
125+
type successProductRequest struct{}
126+
127+
func (r *successProductRequest) Context() context.Context {
128+
return context.Background()
129+
}
130+
131+
func (r *successProductRequest) Bind(v interface{}) error {
132+
p := v.(*struct {
133+
ProductId string `json:"productId"`
134+
Price string `json:"price"`
135+
})
136+
p.ProductId = "123"
137+
p.Price = "599"
138+
return nil
139+
}
140+
141+
func (r *successProductRequest) Param(string) string { return "" }
142+
func (r *successProductRequest) PathParam(string) string { return "" }
143+
func (r *successProductRequest) HostName() string { return "" }
144+
func (r *successProductRequest) Params(string) []string { return nil }
145+
146+
func TestProductHandler_Success(t *testing.T) {
147+
mockContainer, _ := container.NewMockContainer(t)
148+
ctx := &gofr.Context{
149+
Request: &successProductRequest{},
150+
Container: mockContainer,
151+
ContextLogger: *logging.NewContextLogger(context.Background(), mockContainer.Logger),
152+
}
153+
154+
err := productHandler(ctx)
155+
if err != nil {
156+
t.Errorf("Expected nil error, got %v", err)
157+
}
158+
}
159+
160+
type successOrderRequest struct{}
161+
162+
func (r *successOrderRequest) Context() context.Context {
163+
return context.Background()
164+
}
165+
166+
func (r *successOrderRequest) Bind(v interface{}) error {
167+
o := v.(*struct {
168+
OrderId string `json:"orderId"`
169+
Status string `json:"status"`
170+
})
171+
o.OrderId = "456"
172+
o.Status = "pending"
173+
return nil
174+
}
175+
func (r *successOrderRequest) Param(string) string { return "" }
176+
func (r *successOrderRequest) PathParam(string) string { return "" }
177+
func (r *successOrderRequest) HostName() string { return "" }
178+
func (r *successOrderRequest) Params(string) []string { return nil }
179+
180+
func TestOrderHandler_Success(t *testing.T) {
181+
mockContainer, _ := container.NewMockContainer(t)
182+
ctx := &gofr.Context{
183+
Request: &successOrderRequest{},
184+
Container: mockContainer,
185+
ContextLogger: *logging.NewContextLogger(context.Background(), mockContainer.Logger),
186+
}
187+
err := orderHandler(ctx)
188+
if err != nil {
189+
t.Errorf("Expected nil error, got %v", err)
190+
}
191+
}

0 commit comments

Comments
 (0)