Skip to content

Commit

Permalink
chat-server: basic unit testing for api and service layer.
Browse files Browse the repository at this point in the history
  • Loading branch information
aywan committed Nov 18, 2023
1 parent a5b3f79 commit a0d8828
Show file tree
Hide file tree
Showing 7 changed files with 419 additions and 0 deletions.
1 change: 1 addition & 0 deletions chat-server/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/bin/
*.gen.go
14 changes: 14 additions & 0 deletions chat-server/.mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
quiet: False
with-expecter: True
mockname: "Mock{{.InterfaceName}}"
filename: "{{.InterfaceName}}.mock.gen.go"
dir: "{{.InterfaceDir}}/mocks"
outpkg: "mocks"
packages:
github.com/aywan/balun_miserv_s2/chat-server/internal/repository:
interfaces:
Chat:
github.com/aywan/balun_miserv_s2/chat-server/internal/service:
interfaces:
Chat:

5 changes: 5 additions & 0 deletions chat-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ tests:
.PHONY: generate
generate:
make generate-api-chat-v1
make mocks

.PHONY: mocks
mocks:
$(LOCAL_BIN)/mockery

.PHONY: generate-api-chat-v1
generate-api-chat-v1:
Expand Down
2 changes: 2 additions & 0 deletions chat-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/aywan/balun_miserv_s2/shared/lib/db v0.0.0
github.com/aywan/balun_miserv_s2/shared/lib/logger v0.0.0
github.com/aywan/balun_miserv_s2/shared/lib/runutil v0.0.0
github.com/brianvoe/gofakeit/v6 v6.24.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/stretchr/testify v1.8.4
go.uber.org/fx v1.20.1
Expand Down Expand Up @@ -34,6 +35,7 @@ require (
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
go.uber.org/dig v1.17.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions chat-server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8
github.com/Masterminds/squirrel v1.5.4/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/brianvoe/gofakeit/v6 v6.24.0 h1:74yq7RRz/noddscZHRS2T84oHZisW9muwbb8sRnU52A=
github.com/brianvoe/gofakeit/v6 v6.24.0/go.mod h1:Ow6qC71xtwm79anlwKRlWZW6zVq9D2XHE4QSSMP/rU8=
github.com/cockroachdb/cockroach-go/v2 v2.2.0 h1:/5znzg5n373N/3ESjHF5SMLxiW4RKB05Ql//KWfeTFs=
github.com/cockroachdb/cockroach-go/v2 v2.2.0/go.mod h1:u3MiKYGupPPjkn3ozknpMUpxPaNLTFWAya419/zv6eI=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -46,11 +48,14 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
Expand Down
158 changes: 158 additions & 0 deletions chat-server/internal/api/chat/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package chat

import (
"context"
"database/sql"
"testing"

"github.com/aywan/balun_miserv_s2/chat-server/internal/model"
"github.com/aywan/balun_miserv_s2/chat-server/internal/service/chat/dto"
"github.com/aywan/balun_miserv_s2/chat-server/internal/service/mocks"
desc "github.com/aywan/balun_miserv_s2/chat-server/pkg/grpc/v1/chat_v1"
"github.com/brianvoe/gofakeit/v6"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

func TestApi_Create(t *testing.T) {
t.Parallel()
log := zaptest.NewLogger(t)
ctx := context.Background()
var err error

req := &desc.CreateRequest{
Users: []int64{1, 2, 3},
OwnerId: 1,
Name: gofakeit.BeerStyle(),
}
chatId := gofakeit.Int64()

chatService := mocks.NewMockChat(t)
chatService.EXPECT().
Create(ctx, dto.NewChatDTO{
OwnerID: req.OwnerId,
Name: req.Name,
Users: req.Users,
}).
Return(chatId, nil)

api := New(log, chatService)

rsp, err := api.Create(ctx, req)
require.NoError(t, err)
require.Equal(t, chatId, rsp.Id)
}

func TestApi_Delete(t *testing.T) {
t.Parallel()
log := zaptest.NewLogger(t)
ctx := context.Background()
var err error

req := &desc.ChatIdRequest{
Id: gofakeit.Int64(),
}

chatService := mocks.NewMockChat(t)
chatService.EXPECT().
Delete(ctx, req.Id).
Return(nil)

api := New(log, chatService)

_, err = api.Delete(ctx, req)
require.NoError(t, err)
}

func TestApi_GetMessages(t *testing.T) {
t.Parallel()
log := zaptest.NewLogger(t)
ctx := context.Background()
var err error

req := &desc.GetMessagesRequest{
ChatId: gofakeit.Int64(),
Limit: gofakeit.Int64(),
AfterMessageId: gofakeit.Int64(),
BeforeMessageId: gofakeit.Int64(),
}

messages := dto.MessagesResultDTO{
Items: model.MessageList{
{
ID: gofakeit.Int64(),
CreatedAt: gofakeit.Date(),
UserID: sql.NullInt64{},
MsgType: model.MsgTypeSystem,
Text: gofakeit.BeerStyle(),
},
{
ID: gofakeit.Int64(),
CreatedAt: gofakeit.Date(),
UserID: sql.NullInt64{gofakeit.Int64(), true},
MsgType: model.MsgTypeUser,
Text: gofakeit.BeerAlcohol() + " " + gofakeit.BeerIbu(),
},
},
HasNext: gofakeit.Bool(),
NextId: gofakeit.Int64(),
}

chatService := mocks.NewMockChat(t)
chatService.EXPECT().
GetMessages(ctx, dto.GetMessagesDTO{
ChatID: req.ChatId,
Limit: req.Limit,
AfterMessageId: req.AfterMessageId,
BeforeMessageId: req.BeforeMessageId,
}).
Return(messages, nil)

api := New(log, chatService)

rsp, err := api.GetMessages(ctx, req)
require.NoError(t, err)

require.Equal(t, messages.HasNext, rsp.HasNext)
require.Equal(t, messages.NextId, rsp.NextId)

require.Equal(t, messages.Items[0].ID, rsp.Items[0].Id)
require.Equal(t, messages.Items[0].CreatedAt, rsp.Items[0].CreatedAt.AsTime())
require.Nil(t, rsp.Items[0].UserId)
require.Equal(t, desc.MessageType_SYSTEM, rsp.Items[0].Type)
require.Equal(t, messages.Items[0].Text, rsp.Items[0].Text)

require.Equal(t, messages.Items[1].ID, rsp.Items[1].Id)
require.Equal(t, messages.Items[1].CreatedAt, rsp.Items[1].CreatedAt.AsTime())
require.Equal(t, messages.Items[1].UserID.Int64, *rsp.Items[1].UserId)
require.Equal(t, desc.MessageType_USER, rsp.Items[1].Type)
require.Equal(t, messages.Items[1].Text, rsp.Items[1].Text)
}

func TestApi_SendMessage(t *testing.T) {
t.Parallel()
log := zaptest.NewLogger(t)
ctx := context.Background()
var err error

req := &desc.SendMessageRequest{
ChatId: gofakeit.Int64(),
Type: desc.MessageType_USER,
UserId: gofakeit.Int64(),
Text: gofakeit.BeerStyle(),
}

chatService := mocks.NewMockChat(t)
chatService.EXPECT().
SendMessage(ctx, dto.SendMessageDTO{
ChatID: req.ChatId,
UserID: req.UserId,
Text: req.Text,
}).
Return(nil)

api := New(log, chatService)

_, err = api.SendMessage(ctx, req)
require.NoError(t, err)
}
Loading

0 comments on commit a0d8828

Please sign in to comment.