Skip to content

Commit

Permalink
Merge pull request #4 from solumD/Task-4
Browse files Browse the repository at this point in the history
Task 4
  • Loading branch information
solumD authored Nov 8, 2024
2 parents 2cd0503 + 51ac85a commit 85fbb41
Show file tree
Hide file tree
Showing 23 changed files with 3,521 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
/bin/
/bin//coverage.out
/coverage.out
33 changes: 23 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ get-deps:
go get -u google.golang.org/protobuf/cmd/protoc-gen-go
go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc

local-migration-status:
${LOCAL_BIN}/goose -dir ${LOCAL_MIGRATION_DIR} postgres ${LOCAL_MIGRATION_DSN} status -v

local-migration-up:
${LOCAL_BIN}/goose -dir ${LOCAL_MIGRATION_DIR} postgres ${LOCAL_MIGRATION_DSN} up -v

local-migration-down:
${LOCAL_BIN}/goose -dir ${LOCAL_MIGRATION_DIR} postgres ${LOCAL_MIGRATION_DSN} down -v

generate:
make generate-chat-server-api

Expand All @@ -39,4 +30,26 @@ generate-chat-server-api:
--plugin=protoc-gen-go=bin/protoc-gen-go \
--go-grpc_out=pkg/chat_v1 --go-grpc_opt=paths=source_relative \
--plugin=protoc-gen-go-grpc=bin/protoc-gen-go-grpc \
api/chat_v1/chat.proto
api/chat_v1/chat.proto

local-migration-status:
${LOCAL_BIN}/goose -dir ${LOCAL_MIGRATION_DIR} postgres ${LOCAL_MIGRATION_DSN} status -v

local-migration-up:
${LOCAL_BIN}/goose -dir ${LOCAL_MIGRATION_DIR} postgres ${LOCAL_MIGRATION_DSN} up -v

local-migration-down:
${LOCAL_BIN}/goose -dir ${LOCAL_MIGRATION_DIR} postgres ${LOCAL_MIGRATION_DSN} down -v

test:
go clean -testcache
go test ./... -covermode count -coverpkg=github.com/solumD/chat-server/internal/service/...,github.com/solumD/chat-server/internal/api/... -count 5

test-coverage:
go clean -testcache
go test ./... -coverprofile=coverage.tmp.out -covermode count -coverpkg=github.com/solumD/chat-server/internal/service/...,github.com/solumD/chat-server/internal/api/... -count 5
grep -v 'mocks\|config' coverage.tmp.out > coverage.out
rm coverage.tmp.out
go tool cover -html=coverage.out;
go tool cover -func=./coverage.out | grep "total";
grep -sqFx "/coverage.out" .gitignore || echo "/coverage.out" >> .gitignore
Binary file added bin/golangci-lint
Binary file not shown.
Binary file added bin/goose
Binary file not shown.
Binary file added bin/protoc-gen-go
Binary file not shown.
Binary file added bin/protoc-gen-go-grpc
Binary file not shown.
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ require (
require (
github.com/Masterminds/squirrel v1.5.4
github.com/georgysavva/scany v1.2.2
github.com/gojuno/minimock/v3 v3.4.1
github.com/jackc/pgx/v4 v4.18.3
github.com/joho/godotenv v1.5.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
)

require gopkg.in/yaml.v3 v3.0.1 // indirect

require (
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.3
Expand All @@ -30,6 +34,9 @@ require (
)

require (
github.com/brianvoe/gofakeit/v7 v7.1.2
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
Expand Down
16 changes: 13 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8afzqM=
github.com/Masterminds/squirrel v1.5.4/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
github.com/brianvoe/gofakeit/v7 v7.1.2 h1:vSKaVScNhWVpf1rlyEKSvO8zKZfuDtGqoIHT//iNNb8=
github.com/brianvoe/gofakeit/v7 v7.1.2/go.mod h1:QXuPeBw164PJCzCUZVmgpgHJ3Llj49jSLVkKPMtxtxA=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/cockroachdb/cockroach-go/v2 v2.2.0 h1:/5znzg5n373N/3ESjHF5SMLxiW4RKB05Ql//KWfeTFs=
Expand All @@ -23,6 +25,8 @@ github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14j
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gojuno/minimock/v3 v3.4.1 h1:Flf735K7TT45TKCUMG4fz1vwadW/cW0Q0wH8x7eJKos=
github.com/gojuno/minimock/v3 v3.4.1/go.mod h1:mpNkl275+w8a6CYjeCHIRfN8QzN2R7ejT6jEDUdweuo=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
Expand Down Expand Up @@ -98,10 +102,13 @@ github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwA
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk=
Expand Down Expand Up @@ -140,15 +147,16 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
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.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
Expand Down Expand Up @@ -234,6 +242,8 @@ google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFyt
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
4 changes: 4 additions & 0 deletions internal/api/chat/delete_chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chat

import (
"context"
"fmt"
"log"

desc "github.com/solumD/chat-server/pkg/chat_v1"
Expand All @@ -11,6 +12,9 @@ import (

// DeleteChat отправляет запрос на удаление чата в сервисный слой
func (i *API) DeleteChat(ctx context.Context, req *desc.DeleteChatRequest) (*emptypb.Empty, error) {
if req == nil {
return nil, fmt.Errorf("req is nil")
}
_, err := i.chatService.DeleteChat(ctx, req.GetId())
if err != nil {
log.Println(err)
Expand Down
119 changes: 119 additions & 0 deletions internal/api/chat/tests/create_chat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package tests

import (
"context"
"fmt"
"testing"

"github.com/solumD/chat-server/internal/api/chat"
"github.com/solumD/chat-server/internal/api/chat/errors"
"github.com/solumD/chat-server/internal/model"
"github.com/solumD/chat-server/internal/service"
serviceMocks "github.com/solumD/chat-server/internal/service/mocks"
desc "github.com/solumD/chat-server/pkg/chat_v1"

"github.com/brianvoe/gofakeit/v7"
"github.com/gojuno/minimock/v3"
"github.com/stretchr/testify/require"
)

func TestCreateChat(t *testing.T) {
t.Parallel()

type chatServiceMockFunc func(mc *minimock.Controller) service.ChatService

type args struct {
ctx context.Context
req *desc.CreateChatRequest
}

var (
ctx = context.Background()
mc = minimock.NewController(t)

name = gofakeit.Name()
usernames = []string{gofakeit.Name(), gofakeit.Name(), gofakeit.Name(), gofakeit.Name()}
id = gofakeit.Int64()

serviceErr = fmt.Errorf("service err")

req = &desc.CreateChatRequest{
Name: name,
Usernames: usernames,
}

info = &model.Chat{
Name: name,
Usernames: usernames,
}

res = &desc.CreateChatResponse{
Id: id,
}
)
defer t.Cleanup(mc.Finish)

tests := []struct {
name string
args args
want *desc.CreateChatResponse
err error
chatServiceMock chatServiceMockFunc
}{
{
name: "success case",
args: args{
ctx: ctx,
req: req,
},
want: res,
err: nil,
chatServiceMock: func(mc *minimock.Controller) service.ChatService {
mock := serviceMocks.NewChatServiceMock(mc)
mock.CreateChatMock.Expect(ctx, info).Return(id, nil)
return mock
},
},
{
name: "error case",
args: args{
ctx: ctx,
req: req,
},
want: nil,
err: serviceErr,
chatServiceMock: func(mc *minimock.Controller) service.ChatService {
mock := serviceMocks.NewChatServiceMock(mc)
mock.CreateChatMock.Expect(ctx, info).Return(0, serviceErr)
return mock
},
},
{
name: "error req is nil",
args: args{
ctx: ctx,
req: nil,
},
want: nil,
err: errors.ErrDescChatIsNil,
chatServiceMock: func(mc *minimock.Controller) service.ChatService {
mock := serviceMocks.NewChatServiceMock(mc)
return mock
},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

chatServiceMock := tt.chatServiceMock(mc)
api := chat.NewAPI(chatServiceMock)

res, err := api.CreateChat(tt.args.ctx, tt.args.req)
require.Equal(t, tt.err, err)
require.Equal(t, tt.want, res)
})
}
}
109 changes: 109 additions & 0 deletions internal/api/chat/tests/delete_chat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package tests

import (
"context"
"fmt"
"testing"

"github.com/solumD/chat-server/internal/api/chat"
"github.com/solumD/chat-server/internal/service"
serviceMocks "github.com/solumD/chat-server/internal/service/mocks"
desc "github.com/solumD/chat-server/pkg/chat_v1"

"github.com/brianvoe/gofakeit/v7"
"github.com/gojuno/minimock/v3"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/emptypb"
)

func TestDeleteChat(t *testing.T) {
t.Parallel()

type chatServiceMockFunc func(mc *minimock.Controller) service.ChatService

type args struct {
ctx context.Context
req *desc.DeleteChatRequest
}

var (
ctx = context.Background()
mc = minimock.NewController(t)

id = gofakeit.Int64()

serviceErr = fmt.Errorf("service err")
reqIsNilErr = fmt.Errorf("req is nil")

req = &desc.DeleteChatRequest{
Id: id,
}

res = &emptypb.Empty{}
)
defer t.Cleanup(mc.Finish)

tests := []struct {
name string
args args
want *emptypb.Empty
err error
chatServiceMock chatServiceMockFunc
}{
{
name: "success case",
args: args{
ctx: ctx,
req: req,
},
want: res,
err: nil,
chatServiceMock: func(mc *minimock.Controller) service.ChatService {
mock := serviceMocks.NewChatServiceMock(mc)
mock.DeleteChatMock.Expect(ctx, id).Return(&emptypb.Empty{}, nil)
return mock
},
},
{
name: "service error",
args: args{
ctx: ctx,
req: req,
},
want: nil,
err: serviceErr,
chatServiceMock: func(mc *minimock.Controller) service.ChatService {
mock := serviceMocks.NewChatServiceMock(mc)
mock.DeleteChatMock.Expect(ctx, id).Return(nil, serviceErr)
return mock
},
},
{
name: "error req is nil",
args: args{
ctx: ctx,
req: nil,
},
want: nil,
err: reqIsNilErr,
chatServiceMock: func(mc *minimock.Controller) service.ChatService {
mock := serviceMocks.NewChatServiceMock(mc)
return mock
},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

chatServiceMock := tt.chatServiceMock(mc)
api := chat.NewAPI(chatServiceMock)

res, err := api.DeleteChat(tt.args.ctx, tt.args.req)
require.Equal(t, tt.err, err)
require.Equal(t, tt.want, res)
})
}
}
Loading

0 comments on commit 85fbb41

Please sign in to comment.