Skip to content

Commit d072cb1

Browse files
authored
Fix Test Coverage (#2437)
* use atomic mode and relative paths in workflow * add publisher migration test * fix http server examples test * fix http server tests
1 parent 2cdddef commit d072cb1

File tree

4 files changed

+158
-11
lines changed

4 files changed

+158
-11
lines changed

.github/workflows/go.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ jobs:
102102
command: |
103103
export APP_ENV=test
104104
# Run tests for the examples directory with coverage
105-
go test gofr.dev/examples/... -v -short -coverprofile packageWithpbgo.cov -coverpkg=gofr.dev/examples/...
105+
go test ./examples/... -v -short -covermode=atomic -coverprofile packageWithpbgo.cov -coverpkg=./examples/...
106106
# Filter out auto-generated files by protobuf and gofr framework from coverage report
107107
grep -vE '(/client/|grpc-.+-client/main\.go|_client\.go|_gofr\.go|_grpc\.pb\.go|\.pb\.go|\.proto|health_.*\.go)' packageWithpbgo.cov > profile.cov
108108
# Display coverage statistics
@@ -211,8 +211,8 @@ jobs:
211211
- name: Merge Coverage Files
212212
working-directory: artifacts
213213
run: |
214-
awk '!/^mode: / && FNR==1{print "mode: set"} {print}' ./Example-Test-Report/profile.cov > merged_profile.cov
215-
tail -n +2 ./PKG-Coverage-Report/profile.cov >> merged_profile.cov
214+
echo "mode: atomic" > merged_profile.cov
215+
grep -h -v "mode:" ./Example-Test-Report/profile.cov ./PKG-Coverage-Report/profile.cov >> merged_profile.cov
216216
217217
# Calculate and output the total code coverage percentage
218218
- name: Parse code-coverage value
@@ -222,6 +222,7 @@ jobs:
222222
codeCoverage=${codeCoverage%?}
223223
echo "CODE_COVERAGE=$codeCoverage" >> $GITHUB_ENV
224224
echo "✅ Total Code Coverage: $codeCoverage%"
225+
225226
# - name: Check if code-coverage is greater than threshold
226227
# run: |
227228
# codeCoverage=${{ env.CODE_COVERAGE }}
@@ -332,9 +333,8 @@ jobs:
332333
- name: Merge Coverage Files
333334
working-directory: artifacts
334335
run: |
335-
echo "mode: set" > merged_profile.cov
336-
tail -n +2 ./Example-Test-Report/profile.cov >> merged_profile.cov
337-
tail -n +2 ./PKG-Coverage-Report/profile.cov >> merged_profile.cov
336+
echo "mode: atomic" > merged_profile.cov
337+
grep -h -v "mode:" ./Example-Test-Report/profile.cov ./PKG-Coverage-Report/profile.cov >> merged_profile.cov
338338
339339
# Generate and print total coverage percentage
340340
echo "Total Coverage:"

examples/http-server/main_test.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"io"
78
"net/http"
89
"net/http/httptest"
@@ -32,11 +33,14 @@ func TestMain(m *testing.M) {
3233
}
3334

3435
func TestIntegration_SimpleAPIServer(t *testing.T) {
35-
host := "http://localhost:9000"
36-
36+
httpPort := testutil.GetFreePort(t)
3737
port := testutil.GetFreePort(t)
38+
39+
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
3840
t.Setenv("METRICS_PORT", strconv.Itoa(port))
3941

42+
host := fmt.Sprintf("http://localhost:%d", httpPort)
43+
4044
go main()
4145
time.Sleep(100 * time.Millisecond) // Giving some time to start the server
4246

@@ -48,7 +52,6 @@ func TestIntegration_SimpleAPIServer(t *testing.T) {
4852
{"hello handler", "/hello", "Hello World!"},
4953
{"hello handler with query parameter", "/hello?name=gofr", "Hello gofr!"},
5054
{"redis handler", "/redis", ""},
51-
{"trace handler", "/trace", ""},
5255
{"mysql handler", "/mysql", float64(4)},
5356
}
5457

@@ -80,7 +83,16 @@ func TestIntegration_SimpleAPIServer(t *testing.T) {
8083
}
8184

8285
func TestIntegration_SimpleAPIServer_Errors(t *testing.T) {
83-
host := "http://localhost:9000"
86+
httpPort := testutil.GetFreePort(t)
87+
port := testutil.GetFreePort(t)
88+
89+
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
90+
t.Setenv("METRICS_PORT", strconv.Itoa(port))
91+
92+
host := fmt.Sprintf("http://localhost:%d", httpPort)
93+
94+
go main()
95+
time.Sleep(100 * time.Millisecond) // Giving some time to start the server
8496

8597
tests := []struct {
8698
desc string
@@ -136,7 +148,16 @@ func TestIntegration_SimpleAPIServer_Errors(t *testing.T) {
136148
}
137149

138150
func TestIntegration_SimpleAPIServer_Health(t *testing.T) {
139-
host := "http://localhost:9000"
151+
httpPort := testutil.GetFreePort(t)
152+
port := testutil.GetFreePort(t)
153+
154+
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
155+
t.Setenv("METRICS_PORT", strconv.Itoa(port))
156+
157+
host := fmt.Sprintf("http://localhost:%d", httpPort)
158+
159+
go main()
160+
time.Sleep(100 * time.Millisecond) // Giving some time to start the server
140161

141162
tests := []struct {
142163
desc string
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
"errors"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"gofr.dev/pkg/gofr/migration"
10+
)
11+
12+
// MockPubSub implements the PubSub interface for testing
13+
type MockPubSub struct {
14+
Calls []string
15+
ErrOnTopic map[string]error
16+
}
17+
18+
func (*MockPubSub) Query(_ context.Context, _ string, _ ...any) ([]byte, error) {
19+
return []byte{}, nil
20+
}
21+
22+
func (*MockPubSub) DeleteTopic(_ context.Context, _ string) error {
23+
return nil
24+
}
25+
26+
func (m *MockPubSub) CreateTopic(_ context.Context, topic string) error {
27+
m.Calls = append(m.Calls, topic)
28+
if err, ok := m.ErrOnTopic[topic]; ok {
29+
return err
30+
}
31+
return nil
32+
}
33+
34+
func TestCreateTopics(t *testing.T) {
35+
tests := []struct {
36+
name string
37+
errOnProduct error
38+
errOnOrder error
39+
expectedErr error
40+
expectedCalls []string
41+
}{
42+
{"success", nil, nil, nil, []string{"products", "order-logs"}},
43+
{"error on products", errors.New("fail products"), nil,
44+
errors.New("fail products"), []string{"products"}},
45+
{"error on order-logs", nil, errors.New("fail order"), errors.New("fail order"), []string{"products", "order-logs"}},
46+
}
47+
48+
for _, tt := range tests {
49+
mockPubSub := &MockPubSub{
50+
ErrOnTopic: map[string]error{
51+
"products": tt.errOnProduct,
52+
"order-logs": tt.errOnOrder,
53+
},
54+
}
55+
ds := migration.Datasource{PubSub: mockPubSub}
56+
57+
err := createTopics().UP(ds)
58+
59+
assert.Equal(t, tt.expectedErr, err, tt.name)
60+
assert.Equal(t, tt.expectedCalls, mockPubSub.Calls, tt.name)
61+
}
62+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
"errors"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
10+
"gofr.dev/pkg/gofr/migration"
11+
)
12+
13+
// MockPubSub implements the PubSub interface for testing
14+
type MockPubSub struct {
15+
Calls []string
16+
ErrOnTopic map[string]error
17+
}
18+
19+
func (*MockPubSub) Query(_ context.Context, _ string, _ ...any) ([]byte, error) {
20+
return []byte{}, nil
21+
}
22+
23+
func (*MockPubSub) DeleteTopic(_ context.Context, _ string) error {
24+
return nil
25+
}
26+
27+
func (m *MockPubSub) CreateTopic(_ context.Context, topic string) error {
28+
m.Calls = append(m.Calls, topic)
29+
if err, ok := m.ErrOnTopic[topic]; ok {
30+
return err
31+
}
32+
return nil
33+
}
34+
35+
func TestCreateTopics(t *testing.T) {
36+
tests := []struct {
37+
name string
38+
errOnProduct error
39+
errOnOrder error
40+
expectedErr error
41+
expectedCalls []string
42+
}{
43+
{"success", nil, nil, nil, []string{"products", "order-logs"}},
44+
{"error on products", errors.New("fail products"), nil,
45+
errors.New("fail products"), []string{"products"}},
46+
{"error on order-logs", nil, errors.New("fail order"),
47+
errors.New("fail order"), []string{"products", "order-logs"}},
48+
}
49+
50+
for _, tt := range tests {
51+
mockPubSub := &MockPubSub{
52+
ErrOnTopic: map[string]error{
53+
"products": tt.errOnProduct,
54+
"order-logs": tt.errOnOrder,
55+
},
56+
}
57+
ds := migration.Datasource{PubSub: mockPubSub}
58+
59+
err := createTopics().UP(ds)
60+
61+
assert.Equal(t, tt.expectedErr, err, tt.name)
62+
assert.Equal(t, tt.expectedCalls, mockPubSub.Calls, tt.name)
63+
}
64+
}

0 commit comments

Comments
 (0)