Skip to content

Commit ae9048e

Browse files
authored
Merge pull request #73 from ThreaditApp/tests
Test implementation
2 parents 0775bf7 + 3a47e19 commit ae9048e

File tree

21 files changed

+1257
-1
lines changed

21 files changed

+1257
-1
lines changed

.github/workflows/dev.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Build and Test
2+
3+
on:
4+
push:
5+
branches:
6+
- dev
7+
pull_request:
8+
branches:
9+
- dev
10+
11+
jobs:
12+
test:
13+
name: Run Unit Tests
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Set up Go
21+
uses: actions/setup-go@v5
22+
with:
23+
go-version: '1.23'
24+
25+
- name: Run unit tests for thread-service
26+
working-directory: code/services/thread-service
27+
run: go test ./test/...
28+
29+
- name: Run unit tests for vote-service
30+
working-directory: code/services/vote-service
31+
run: go test ./test/...
32+
33+
- name: Run unit tests for search-service
34+
working-directory: code/services/search-service
35+
run: go test ./test/...
36+
37+
- name: Run unit tests for popular-service
38+
working-directory: code/services/popular-service
39+
run: go test ./test/...
40+
41+
- name: Run unit tests for community-service
42+
working-directory: code/services/community-service
43+
run: go test ./test/...
44+
45+
- name: Run unit tests for comment-service
46+
working-directory: code/services/comment-service
47+
run: go test ./test/...

code/services/comment-service/go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@ toolchain go1.24.1
66

77
require (
88
gen v0.0.0-00010101000000-000000000000
9+
github.com/stretchr/testify v1.10.0
910
google.golang.org/grpc v1.71.0
1011
google.golang.org/protobuf v1.36.6
1112
)
1213

1314
require (
15+
github.com/davecgh/go-spew v1.1.1 // indirect
1416
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 // indirect
17+
github.com/kr/text v0.2.0 // indirect
18+
github.com/pmezard/go-difflib v1.0.0 // indirect
19+
github.com/rogpeppe/go-internal v1.14.1 // indirect
1520
golang.org/x/net v0.35.0 // indirect
1621
golang.org/x/sys v0.30.0 // indirect
1722
golang.org/x/text v0.22.0 // indirect
1823
google.golang.org/genproto/googleapis/api v0.0.0-20250303144028-a0af3efb3deb // indirect
1924
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb // indirect
25+
gopkg.in/yaml.v3 v3.0.1 // indirect
2026
)
2127

2228
replace gen => ../../gen

code/services/comment-service/go.sum

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
25
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
36
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
@@ -10,6 +13,16 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
1013
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1114
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 h1:5ZPtiqj0JL5oKWmcsq4VMaAW5ukBEgSGXEN89zeH1Jo=
1215
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3/go.mod h1:ndYquD05frm2vACXE1nsccT4oJzjhw2arTS2cpUD1PI=
16+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
17+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
18+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
19+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
20+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
21+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
22+
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
23+
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
24+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
25+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1326
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
1427
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
1528
go.opentelemetry.io/otel v1.34.0 h1:zRLXxLCgL1WyKsPVrgbSdMN4c0FMkDAskSTQP+0hdUY=
@@ -36,3 +49,8 @@ google.golang.org/grpc v1.71.0 h1:kF77BGdPTQ4/JZWMlb9VpJ5pa25aqvVqogsxNHHdeBg=
3649
google.golang.org/grpc v1.71.0/go.mod h1:H0GRtasmQOh9LkFoCPDu3ZrwUtD1YGE+b2vYBYd/8Ec=
3750
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
3851
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
52+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
53+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
54+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
55+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
56+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
src "comment-service/src"
8+
commentpb "gen/comment-service/pb"
9+
dbpb "gen/db-service/pb"
10+
models "gen/models/pb"
11+
threadpb "gen/thread-service/pb"
12+
13+
"github.com/stretchr/testify/assert"
14+
"google.golang.org/grpc"
15+
"google.golang.org/grpc/codes"
16+
"google.golang.org/grpc/status"
17+
"google.golang.org/protobuf/types/known/emptypb"
18+
)
19+
20+
type MockDBClient struct {
21+
dbpb.DBServiceClient
22+
ListCommentsFunc func(ctx context.Context, req *dbpb.ListCommentsRequest, opts ...grpc.CallOption) (*dbpb.ListCommentsResponse, error)
23+
CreateCommentFunc func(ctx context.Context, req *dbpb.CreateCommentRequest, opts ...grpc.CallOption) (*dbpb.CreateCommentResponse, error)
24+
GetCommentFunc func(ctx context.Context, req *dbpb.GetCommentRequest, opts ...grpc.CallOption) (*models.Comment, error)
25+
UpdateCommentFunc func(ctx context.Context, req *dbpb.UpdateCommentRequest, opts ...grpc.CallOption) (*emptypb.Empty, error)
26+
DeleteCommentFunc func(ctx context.Context, req *dbpb.DeleteCommentRequest, opts ...grpc.CallOption) (*emptypb.Empty, error)
27+
}
28+
29+
func (m *MockDBClient) ListComments(ctx context.Context, req *dbpb.ListCommentsRequest, opts ...grpc.CallOption) (*dbpb.ListCommentsResponse, error) {
30+
return m.ListCommentsFunc(ctx, req, opts...)
31+
}
32+
33+
func (m *MockDBClient) CreateComment(ctx context.Context, req *dbpb.CreateCommentRequest, opts ...grpc.CallOption) (*dbpb.CreateCommentResponse, error) {
34+
return m.CreateCommentFunc(ctx, req, opts...)
35+
}
36+
37+
func (m *MockDBClient) GetComment(ctx context.Context, req *dbpb.GetCommentRequest, opts ...grpc.CallOption) (*models.Comment, error) {
38+
return m.GetCommentFunc(ctx, req, opts...)
39+
}
40+
41+
func (m *MockDBClient) UpdateComment(ctx context.Context, req *dbpb.UpdateCommentRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) {
42+
return m.UpdateCommentFunc(ctx, req, opts...)
43+
}
44+
45+
func (m *MockDBClient) DeleteComment(ctx context.Context, req *dbpb.DeleteCommentRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) {
46+
return m.DeleteCommentFunc(ctx, req, opts...)
47+
}
48+
49+
type MockThreadClient struct {
50+
threadpb.ThreadServiceClient
51+
UpdateThreadFunc func(ctx context.Context, req *threadpb.UpdateThreadRequest, opts ...grpc.CallOption) (*emptypb.Empty, error)
52+
}
53+
54+
func (m *MockThreadClient) UpdateThread(ctx context.Context, req *threadpb.UpdateThreadRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) {
55+
return m.UpdateThreadFunc(ctx, req, opts...)
56+
}
57+
58+
func TestCreateComment_Validation(t *testing.T) {
59+
tests := []struct {
60+
name string
61+
req *commentpb.CreateCommentRequest
62+
wantErr error
63+
}{
64+
{
65+
name: "missing parent id",
66+
req: &commentpb.CreateCommentRequest{},
67+
wantErr: status.Error(codes.InvalidArgument, "Parent id is required"),
68+
},
69+
{
70+
name: "missing content",
71+
req: &commentpb.CreateCommentRequest{
72+
ParentId: "123",
73+
},
74+
wantErr: status.Error(codes.InvalidArgument, "Content is required"),
75+
},
76+
{
77+
name: "content too long",
78+
req: &commentpb.CreateCommentRequest{
79+
ParentId: "123",
80+
Content: string(make([]byte, 501)),
81+
},
82+
wantErr: status.Error(codes.InvalidArgument, "Content exceeds maximum length of 500 characters"),
83+
},
84+
{
85+
name: "valid request",
86+
req: &commentpb.CreateCommentRequest{
87+
ParentId: "123",
88+
Content: "test comment",
89+
ParentType: models.CommentParentType_THREAD,
90+
},
91+
wantErr: nil,
92+
},
93+
}
94+
95+
for _, tt := range tests {
96+
t.Run(tt.name, func(t *testing.T) {
97+
server := &src.CommentServer{
98+
DBClient: &MockDBClient{
99+
CreateCommentFunc: func(ctx context.Context, req *dbpb.CreateCommentRequest, opts ...grpc.CallOption) (*dbpb.CreateCommentResponse, error) {
100+
return &dbpb.CreateCommentResponse{
101+
Id: "123",
102+
}, nil
103+
},
104+
},
105+
ThreadClient: &MockThreadClient{
106+
UpdateThreadFunc: func(ctx context.Context, req *threadpb.UpdateThreadRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) {
107+
return &emptypb.Empty{}, nil
108+
},
109+
},
110+
}
111+
112+
_, err := server.CreateComment(context.Background(), tt.req)
113+
if tt.wantErr != nil {
114+
assert.Equal(t, tt.wantErr.Error(), err.Error())
115+
} else {
116+
assert.NoError(t, err)
117+
}
118+
})
119+
}
120+
}
121+
122+
func TestGetComment_Validation(t *testing.T) {
123+
tests := []struct {
124+
name string
125+
req *commentpb.GetCommentRequest
126+
wantErr error
127+
}{
128+
{
129+
name: "missing id",
130+
req: &commentpb.GetCommentRequest{},
131+
wantErr: status.Error(codes.InvalidArgument, "Comment id is required"),
132+
},
133+
{
134+
name: "valid request",
135+
req: &commentpb.GetCommentRequest{
136+
Id: "123",
137+
},
138+
wantErr: nil,
139+
},
140+
}
141+
142+
for _, tt := range tests {
143+
t.Run(tt.name, func(t *testing.T) {
144+
server := &src.CommentServer{
145+
DBClient: &MockDBClient{
146+
GetCommentFunc: func(ctx context.Context, req *dbpb.GetCommentRequest, opts ...grpc.CallOption) (*models.Comment, error) {
147+
return &models.Comment{
148+
Id: "123",
149+
Content: "test comment",
150+
}, nil
151+
},
152+
},
153+
ThreadClient: &MockThreadClient{},
154+
}
155+
156+
_, err := server.GetComment(context.Background(), tt.req)
157+
if tt.wantErr != nil {
158+
assert.Equal(t, tt.wantErr.Error(), err.Error())
159+
} else {
160+
assert.NoError(t, err)
161+
}
162+
})
163+
}
164+
}

code/services/community-service/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ toolchain go1.24.1
66

77
require (
88
gen v0.0.0-00010101000000-000000000000
9+
github.com/stretchr/testify v1.10.0
910
google.golang.org/grpc v1.71.0
1011
google.golang.org/protobuf v1.36.6
1112
)
1213

1314
require (
15+
github.com/davecgh/go-spew v1.1.1 // indirect
1416
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 // indirect
17+
github.com/pmezard/go-difflib v1.0.0 // indirect
1518
golang.org/x/net v0.35.0 // indirect
1619
golang.org/x/sys v0.30.0 // indirect
1720
golang.org/x/text v0.22.0 // indirect
1821
google.golang.org/genproto/googleapis/api v0.0.0-20250303144028-a0af3efb3deb // indirect
1922
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb // indirect
23+
gopkg.in/yaml.v3 v3.0.1 // indirect
2024
)
2125

2226
replace gen => ../../gen

code/services/community-service/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
24
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
35
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
@@ -10,6 +12,11 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
1012
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1113
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 h1:5ZPtiqj0JL5oKWmcsq4VMaAW5ukBEgSGXEN89zeH1Jo=
1214
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3/go.mod h1:ndYquD05frm2vACXE1nsccT4oJzjhw2arTS2cpUD1PI=
15+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
16+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
17+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
18+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
19+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1320
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
1421
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
1522
go.opentelemetry.io/otel v1.34.0 h1:zRLXxLCgL1WyKsPVrgbSdMN4c0FMkDAskSTQP+0hdUY=
@@ -36,3 +43,6 @@ google.golang.org/grpc v1.71.0 h1:kF77BGdPTQ4/JZWMlb9VpJ5pa25aqvVqogsxNHHdeBg=
3643
google.golang.org/grpc v1.71.0/go.mod h1:H0GRtasmQOh9LkFoCPDu3ZrwUtD1YGE+b2vYBYd/8Ec=
3744
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
3845
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
46+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
47+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
48+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)