|
| 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 | +} |
0 commit comments