Skip to content

Commit 2a4b4e7

Browse files
add tests for various packages (#365)
Co-authored-by: mehrotra234 <[email protected]>
1 parent 87a93b0 commit 2a4b4e7

File tree

7 files changed

+93
-16
lines changed

7 files changed

+93
-16
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package migrations
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"gofr.dev/pkg/gofr/migration"
9+
)
10+
11+
func TestAll(t *testing.T) {
12+
// Get the map of migrations
13+
allMigrations := All()
14+
15+
expected := map[int64]migration.Migrate{
16+
1708322067: createTableEmployee(),
17+
1708322089: addEmployeeInRedis(),
18+
1708322090: createTopicsForStore(),
19+
}
20+
21+
// Check if the length of the maps match
22+
assert.Equal(t, len(expected), len(allMigrations), "TestAll Failed!")
23+
}

examples/using-publisher/main.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ package main
22

33
import (
44
"encoding/json"
5-
"fmt"
6-
75
"gofr.dev/pkg/gofr"
86
)
97

108
func main() {
119
app := gofr.New()
1210

13-
fmt.Println(app.Config.Get("PUBSUB_BROKER"))
14-
1511
app.POST("/publish-order", order)
1612

1713
app.POST("/publish-product", product)

pkg/gofr/cmd/request.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,10 @@ func (r *Request) Context() context.Context {
8080
return context.Background()
8181
}
8282

83-
func (r *Request) HostName() string {
84-
h, err := os.Hostname()
85-
if err != nil {
86-
return ""
87-
}
83+
func (r *Request) HostName() (hostname string) {
84+
hostname, _ = os.Hostname()
8885

89-
return h
86+
return hostname
9087
}
9188

9289
func (r *Request) Bind(i interface{}) error {

pkg/gofr/cmd/request_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,18 @@ func TestRequest_WithOneArg(t *testing.T) {
5454

5555
assert.Equal(t, req, r, "TEST Failed.\n Hostname did not match.")
5656
}
57+
58+
func TestHostName(t *testing.T) {
59+
r := &Request{}
60+
61+
// Get the hostname using os.Hostname()
62+
hostname, err := os.Hostname()
63+
if err != nil {
64+
t.Fatalf("Error getting hostname: %v", err)
65+
}
66+
67+
// Get the hostname from the mock request
68+
result := r.HostName()
69+
70+
assert.Equal(t, hostname, result, "TestHostName Failed!")
71+
}

pkg/gofr/container/container_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ func TestContainer_GetSubscriber(t *testing.T) {
130130
assert.Equal(t, subscriber, out)
131131
}
132132

133+
func TestContainer_NewEmptyContainer(t *testing.T) {
134+
container := NewEmptyContainer()
135+
136+
assert.Nil(t, container.Redis, "TestContainer_NewEmptyContainer Failed!")
137+
assert.Nil(t, container.SQL, "TestContainer_NewEmptyContainer Failed")
138+
assert.Nil(t, container.Services, "TestContainer_NewEmptyContainer Failed")
139+
assert.Nil(t, container.PubSub, "TestContainer_NewEmptyContainer Failed")
140+
assert.Nil(t, container.Logger, "TestContainer_NewEmptyContainer Failed")
141+
}
142+
133143
type mockPubSub struct {
134144
}
135145

pkg/gofr/logging/dynamicLevelLogger_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"testing"
88
"time"
99

10+
"github.com/stretchr/testify/assert"
11+
12+
"gofr.dev/pkg/gofr/service"
1013
"gofr.dev/pkg/gofr/testutil"
1114
)
1215

@@ -47,3 +50,42 @@ func TestDynamicLoggerSuccess(t *testing.T) {
4750
t.Errorf("TestDynamicLoggerSuccess failed! missing debug log")
4851
}
4952
}
53+
54+
func Test_fetchAndUpdateLogLevel_ErrorCases(t *testing.T) {
55+
logger := testutil.NewMockLogger(testutil.INFOLOG)
56+
57+
remoteService := service.NewHTTPService("http://", logger, nil)
58+
59+
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
60+
w.Header().Set("Content-Type", "application/json")
61+
body := `{
62+
"data": [
63+
{
64+
"invalid"
65+
}
66+
}
67+
]
68+
}`
69+
_, _ = w.Write([]byte(body))
70+
}))
71+
defer mockServer.Close()
72+
73+
remoteService2 := service.NewHTTPService(mockServer.URL, logger, nil)
74+
75+
tests := []struct {
76+
desc string
77+
remoteService service.HTTP
78+
currentLogLevel Level
79+
}{
80+
{"invalid URL for remote service", remoteService, testutil.INFOLOG},
81+
{"invalid response from remote service", remoteService2, testutil.DEBUGLOG},
82+
}
83+
84+
for i, tc := range tests {
85+
level, err := fetchAndUpdateLogLevel(tc.remoteService, tc.currentLogLevel)
86+
87+
assert.Equal(t, tc.currentLogLevel, level, "TEST[%d], Failed.\n%s", i, tc.desc)
88+
89+
assert.NotNil(t, err)
90+
}
91+
}

pkg/gofr/subscriber_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ func (s mockSubscriber) Publish(_ context.Context, _ string, _ []byte) error {
4646
return nil
4747
}
4848

49-
type Message struct {
50-
Topic string
51-
Value []byte
52-
MetaData interface{}
53-
}
54-
5549
func (mockSubscriber) Subscribe(_ context.Context, topic string) (*pubsub.Message, error) {
5650
if topic == "test-topic" {
5751
return &pubsub.Message{

0 commit comments

Comments
 (0)