Skip to content

[GoFr SOC] Improve Unit Test Coverage Across 6 Core Packages (zip, logger, pubsub/google, etc.) #2109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5a7d8c3
improving test coverage
Ashish-Kumar-Dash Jul 29, 2025
9231e0a
update
Ashish-Kumar-Dash Jul 29, 2025
027de6e
Merge branch 'development' into improve/test-coverage
Umang01-hash Jul 29, 2025
3fdb81c
fixed linting errors
Ashish-Kumar-Dash Jul 30, 2025
52910ba
Merge branch 'development' into improve/test-coverage
Ashish-Kumar-Dash Aug 1, 2025
a026f0e
fixing test issues
Ashish-Kumar-Dash Aug 1, 2025
938ce1d
fixed linting errors
Ashish-Kumar-Dash Aug 2, 2025
b53e6e6
Merge branch 'development' into improve/test-coverage
Ashish-Kumar-Dash Aug 4, 2025
812b09e
fixed t.context()
Ashish-Kumar-Dash Aug 4, 2025
332a3c5
Merge branch 'improve/test-coverage' of https://github.com/Ashish-Kum…
Ashish-Kumar-Dash Aug 4, 2025
dbf0e51
Merge branch 'development' into improve/test-coverage
Ashish-Kumar-Dash Aug 5, 2025
3e4af75
Merge branch 'development' into improve/test-coverage
Ashish-Kumar-Dash Aug 5, 2025
6564172
fixed gofmt errors
Ashish-Kumar-Dash Aug 6, 2025
75faded
fix db_test.go
Ashish-Kumar-Dash Aug 6, 2025
e37975f
fixing gofmt errors
Ashish-Kumar-Dash Aug 6, 2025
e6bb259
Merge branch 'development' into improve/test-coverage
Umang01-hash Aug 8, 2025
6b4a0f9
fix lint issues
Ashish-Kumar-Dash Aug 8, 2025
3c2823c
Merge branch 'improve/test-coverage' of https://github.com/Ashish-Kum…
Ashish-Kumar-Dash Aug 8, 2025
6822935
fix linter in migration/sql_test.go
Umang01-hash Aug 12, 2025
850abad
reverting go.work changes
coolwednesday Aug 12, 2025
791958a
Merge branch 'development' into improve/test-coverage
coolwednesday Aug 12, 2025
7f1c9bb
Merge branch 'development' into improve/test-coverage
coolwednesday Aug 12, 2025
832ab48
Merge branch 'development' into improve/test-coverage
Umang01-hash Aug 12, 2025
2f013a4
Merge branch 'development' into improve/test-coverage
Ashish-Kumar-Dash Aug 12, 2025
32c40ef
Merge branch 'development' into improve/test-coverage
Ashish-Kumar-Dash Aug 12, 2025
7c3d634
Merge branch 'development' into improve/test-coverage
Ashish-Kumar-Dash Aug 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 164 additions & 6 deletions pkg/gofr/datasource/pubsub/google/google_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
)

var (
errTopicExists = errors.New("topic already exists")
errTopicExists = errors.New("topic already exists")

errTestSentinel = errors.New("test-error")
)

Expand All @@ -47,6 +48,7 @@ func TestGoogleClient_New_InvalidConfig(t *testing.T) {
var g *googleClient

ctrl := gomock.NewController(t)

defer ctrl.Finish()

out := testutil.StderrOutputForFunc(func() {
Expand All @@ -56,33 +58,41 @@ func TestGoogleClient_New_InvalidConfig(t *testing.T) {
})

assert.Nil(t, g)

assert.Contains(t, out, "could not configure google pubsub")
}

func TestGoogleClient_New_EmptyClient(t *testing.T) {
ctrl := gomock.NewController(t)

defer ctrl.Finish()

mockMetrics := NewMockMetrics(ctrl)

logger := logging.NewMockLogger(logging.DEBUG)

config := Config{ProjectID: "test", SubscriptionName: "test"}

client := New(config, logger, mockMetrics)

require.Nil(t, client.client, "TestGoogleClient_New_EmptyClient Failed!")

require.Equal(t, config, client.Config, "TestGoogleClient_New_EmptyClient Failed!")
}

func TestGoogleClient_Publish_Success(t *testing.T) {
client := getGoogleClient(t)

defer client.Close()

ctrl := gomock.NewController(t)

defer ctrl.Finish()

mockMetrics := NewMockMetrics(ctrl)

topic := "test-topic"

message := []byte("test message")

out := testutil.StdoutOutputForFunc(func() {
Expand All @@ -97,6 +107,7 @@ func TestGoogleClient_Publish_Success(t *testing.T) {
}

mockMetrics.EXPECT().IncrementCounter(gomock.Any(), "app_pubsub_publish_total_count", "topic", topic)

mockMetrics.EXPECT().IncrementCounter(gomock.Any(), "app_pubsub_publish_success_count", "topic", topic)

err := g.Publish(t.Context(), topic, message)
Expand All @@ -113,6 +124,7 @@ func TestGoogleClient_Publish_Success(t *testing.T) {

func TestGoogleClient_PublishTopic_Error(t *testing.T) {
ctrl := gomock.NewController(t)

defer ctrl.Finish()

mockMetrics := NewMockMetrics(ctrl)
Expand All @@ -121,6 +133,7 @@ func TestGoogleClient_PublishTopic_Error(t *testing.T) {
ProjectID: "test",
SubscriptionName: "sub",
}, metrics: mockMetrics, logger: logging.NewMockLogger(logging.DEBUG)}

defer g.client.Close()

ctx, cancel := context.WithCancel(t.Context())
Expand All @@ -130,6 +143,7 @@ func TestGoogleClient_PublishTopic_Error(t *testing.T) {
mockMetrics.EXPECT().IncrementCounter(gomock.Any(), "app_pubsub_publish_total_count", "topic", "test-topic")

err := g.Publish(ctx, "test-topic", []byte(""))

require.ErrorContains(t, err, "context canceled")
}

Expand All @@ -138,27 +152,32 @@ func TestGoogleClient_getTopic_Success(t *testing.T) {
ProjectID: "test",
SubscriptionName: "sub",
}}

defer g.client.Close()

topic, err := g.getTopic(t.Context(), "test-topic")

require.NoError(t, err)

assert.Equal(t, "test-topic", topic.ID())
}

func TestGoogleClient_getTopic_Error(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())

cancel()

g := &googleClient{client: getGoogleClient(t), Config: Config{
ProjectID: "test",
SubscriptionName: "sub",
}}

defer g.client.Close()

topic, err := g.getTopic(ctx, "test-topic")

assert.Nil(t, topic)

require.ErrorContains(t, err, "context canceled")
}

Expand All @@ -167,13 +186,15 @@ func TestGoogleClient_getSubscription(t *testing.T) {
ProjectID: "test",
SubscriptionName: "sub",
}}

defer g.client.Close()

topic, _ := g.client.CreateTopic(t.Context(), "test-topic")

sub, err := g.getSubscription(t.Context(), topic)

require.NoError(t, err)

assert.NotNil(t, sub)
}

Expand All @@ -190,7 +211,6 @@ func Test_validateConfigs(t *testing.T) {

for _, tc := range testCases {
err := validateConfigs(tc.input)

require.ErrorIs(t, err, tc.expErr)
}
}
Expand All @@ -215,9 +235,11 @@ func TestGoogleClient_CloseReturnsError(t *testing.T) {

func TestGoogleClient_CreateTopic_Success(t *testing.T) {
ctrl := gomock.NewController(t)

defer ctrl.Finish()

mockClient := NewMockClient(ctrl)

g := &googleClient{client: mockClient, Config: Config{ProjectID: "test", SubscriptionName: "sub"}}

tests := []struct {
Expand Down Expand Up @@ -247,19 +269,19 @@ func TestGoogleClient_CreateTopic_Success(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.mockBehavior()

err := g.CreateTopic(t.Context(), tt.topicName)

require.ErrorIs(t, err, tt.expectedErr, "expected no error, but got one")
})
}
}

func TestGoogleClient_CreateTopic_Error(t *testing.T) {
ctrl := gomock.NewController(t)

defer ctrl.Finish()

mockClient := NewMockClient(ctrl)

g := &googleClient{client: mockClient, Config: Config{ProjectID: "test", SubscriptionName: "sub"}}

mockClient.EXPECT().CreateTopic(t.Context(), "test-topic").
Expand All @@ -272,6 +294,7 @@ func TestGoogleClient_CreateTopic_Error(t *testing.T) {

func TestGoogleClient_CreateTopic_EmptyClient(t *testing.T) {
ctrl := gomock.NewController(t)

defer ctrl.Finish()

g := &googleClient{client: nil, Config: Config{ProjectID: "test", SubscriptionName: "sub"}}
Expand All @@ -285,11 +308,12 @@ func TestGoogleClient_DeleteTopic(t *testing.T) {
ctx := t.Context()

client := getGoogleClient(t)

defer client.Close()

g := &googleClient{client: client, Config: Config{ProjectID: "test", SubscriptionName: "sub"}}

// Test successful topic creation
// Test successful topic creation
t.Run("DeleteTopic_Success", func(t *testing.T) {
err := g.CreateTopic(ctx, "test-topic")
require.NoError(t, err)
Expand All @@ -301,13 +325,13 @@ func TestGoogleClient_DeleteTopic(t *testing.T) {
// Test topic deletion with topic not found
t.Run("DeleteTopic_NotFound", func(t *testing.T) {
err := g.DeleteTopic(ctx, "test-topic")

require.ErrorContains(t, err, "NotFound", "expected NotFound error for non existing topic deletion")
})
}

func TestGoogleClient_DeleteTopic_EmptyClient(t *testing.T) {
ctrl := gomock.NewController(t)

defer ctrl.Finish()

g := &googleClient{client: nil, Config: Config{ProjectID: "test", SubscriptionName: "sub"}}
Expand All @@ -319,15 +343,19 @@ func TestGoogleClient_DeleteTopic_EmptyClient(t *testing.T) {

func TestGoogleClient_Query(t *testing.T) {
client := getGoogleClient(t)

defer client.Close()

ctrl := gomock.NewController(t)

defer ctrl.Finish()

mockMetrics := NewMockMetrics(ctrl)

logger := logging.NewMockLogger(logging.DEBUG)

topic := "test-topic-query"

message := []byte("test message")

g := &googleClient{
Expand All @@ -341,25 +369,155 @@ func TestGoogleClient_Query(t *testing.T) {
}

topicObj, err := client.CreateTopic(t.Context(), topic)

require.NoError(t, err)

subName := "sub-query-" + topic

_, err = client.CreateSubscription(t.Context(), subName, gcPubSub.SubscriptionConfig{
Topic: topicObj,
})

require.NoError(t, err)

result := topicObj.Publish(t.Context(), &gcPubSub.Message{Data: message})

_, err = result.Get(t.Context())

require.NoError(t, err)

ctx, cancel := context.WithTimeout(t.Context(), 1*time.Second)

defer cancel()

queryResult, err := g.Query(ctx, topic)

require.NoError(t, err)

assert.Equal(t, message, queryResult)

err = topicObj.Delete(t.Context())

require.NoError(t, err)
}

func TestGoogleClient_getSubscription_TopicNil(t *testing.T) {
g := &googleClient{client: getGoogleClient(t)}

defer g.client.Close()

sub, err := g.getSubscription(t.Context(), nil)

require.Error(t, err)

assert.Nil(t, sub)
}

func TestClose_ClientNil(t *testing.T) {
g := &googleClient{
receiveChan: map[string]chan *pubsub.Message{
"test-topic": make(chan *pubsub.Message),
},
}

err := g.Close()
require.NoError(t, err)
}

func TestClose_MultipleReceiveChans_ClientNil(t *testing.T) {
g := &googleClient{
receiveChan: map[string]chan *pubsub.Message{
"topic1": make(chan *pubsub.Message),
"topic2": make(chan *pubsub.Message),
},
// client is nil
}

err := g.Close()
require.NoError(t, err)
}

func TestSubscribe_ClientNil(t *testing.T) {
g := &googleClient{}

msg, err := g.Subscribe(t.Context(), "test-topic")
require.Nil(t, msg)
require.NoError(t, err)
}

func TestGetTopic_ClientNil(t *testing.T) {
g := &googleClient{}

_, err := g.getTopic(t.Context(), "any-topic")
require.Equal(t, errClientNotConnected, err)
}

func TestIsConnected_WhenClientNil(t *testing.T) {
g := &googleClient{}
require.False(t, g.isConnected())
}

func TestGoogleClient_getTopic_CreateFailure(t *testing.T) {
client := getGoogleClient(t)

defer client.Close()

// Delete the server to simulate failure in CreateTopic
client.Close()

g := &googleClient{client: client, Config: Config{
ProjectID: "test",
SubscriptionName: "sub",
}}

_, err := g.getTopic(t.Context(), "test-topic")

require.Error(t, err)
}

func TestGoogleClient_collectMessages_LimitReached(t *testing.T) {
logger := logging.NewMockLogger(logging.DEBUG)

g := &googleClient{
logger: logger,
}

msgChan := make(chan []byte, 3)

msgChan <- []byte("message1")
msgChan <- []byte("message2")

close(msgChan)

ctx := t.Context()

result := g.collectMessages(ctx, msgChan, 2)

expected := []byte("message1\nmessage2")

assert.Equal(t, expected, result)
}

func TestGoogleClient_getQuerySubscription_CreateFails(t *testing.T) {
client := getGoogleClient(t)

defer client.Close()

g := &googleClient{
client: client,
Config: Config{ProjectID: "test", SubscriptionName: "sub"},
}

topic, err := client.CreateTopic(t.Context(), "test-topic-bad")

require.NoError(t, err)

// simulate failure by closing client
client.Close()

sub, err := g.getQuerySubscription(t.Context(), topic)

require.Error(t, err)

assert.Nil(t, sub)
}
Loading