Skip to content

Commit

Permalink
test(user): complete corner condition
Browse files Browse the repository at this point in the history
  • Loading branch information
linehk committed Feb 26, 2024
1 parent b1c6d0b commit 23e4312
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ module github.com/linehk/go-microservices-blogger
go 1.22.0

require (
github.com/emicklei/go-restful/v3 v3.11.0
github.com/google/uuid v1.6.0
github.com/jinzhu/copier v0.4.0
github.com/stretchr/testify v1.8.4
github.com/zeromicro/go-zero v1.6.2
go.uber.org/mock v0.4.0
google.golang.org/grpc v1.61.1
Expand All @@ -19,6 +20,7 @@ require (
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -32,7 +34,6 @@ require (
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
Expand All @@ -49,6 +50,7 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/openzipkin/zipkin-go v0.4.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
Expand Down
67 changes: 57 additions & 10 deletions service/user/rpc/internal/test/get_logic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package test

import (
"context"
"database/sql"
"errors"
"flag"
"log"
"net"
"testing"
"time"

"github.com/google/uuid"
"github.com/linehk/go-microservices-blogger/errcode"
Expand All @@ -16,6 +18,7 @@ import (
"github.com/linehk/go-microservices-blogger/service/user/rpc/internal/svc"
"github.com/linehk/go-microservices-blogger/service/user/rpc/model"
"github.com/linehk/go-microservices-blogger/service/user/rpc/user"
"github.com/stretchr/testify/require"
"github.com/zeromicro/go-zero/core/conf"
"go.uber.org/mock/gomock"
"google.golang.org/grpc"
Expand Down Expand Up @@ -105,27 +108,71 @@ func IntegrationTestGet(t *testing.T) {
}
}

func newCtrl(t *testing.T) (*gomock.Controller, *model.MockAppUserModel, context.Context, *logic.GetLogic) {
func newCtrl(t *testing.T) (
*gomock.Controller,
context.Context,
*logic.GetLogic,
*model.MockAppUserModel,
*model.MockLocaleModel) {

ctrl := gomock.NewController(t)
repo := model.NewMockAppUserModel(ctrl)
ctx := context.Background()
appUserRepo := model.NewMockAppUserModel(ctrl)
localeRepo := model.NewMockLocaleModel(ctrl)
logicService := logic.NewGetLogic(ctx, &svc.ServiceContext{
AppUserModel: repo,
AppUserModel: appUserRepo,
LocaleModel: localeRepo,
})
return ctrl, repo, ctx, logicService
return ctrl, ctx, logicService, appUserRepo, localeRepo
}

func TestGetUserNotExist(t *testing.T) {
ctrl, repo, ctx, logicService := newCtrl(t)
ctrl, ctx, logicService, appUserRepo, _ := newCtrl(t)
defer ctrl.Finish()

userId := uuid.New().String()

appUserRepo.EXPECT().FindOneByUuid(ctx, userId).Return(nil, model.ErrNotFound)

_, actual := logicService.Get(&user.GetReq{UserId: userId})
expected := errcode.Wrap(errcode.UserNotExist)
require.Equal(t, expected, actual)
}

func TestGetAppUserDatabase(t *testing.T) {
ctrl, ctx, logicService, appUserRepo, _ := newCtrl(t)
defer ctrl.Finish()

userId := uuid.New().String()

repo.EXPECT().FindOneByUuid(ctx, userId).Return(nil, model.ErrNotFound)
expected := errcode.Wrap(errcode.Database)
appUserRepo.EXPECT().FindOneByUuid(ctx, userId).Return(nil, expected)

_, gotErr := logicService.Get(&user.GetReq{UserId: userId})
wantErr := errcode.Wrap(errcode.UserNotExist)
if !errors.Is(gotErr, wantErr) {
t.Errorf("got %v, want %v", gotErr, wantErr)
_, actual := logicService.Get(&user.GetReq{UserId: userId})
require.Equal(t, actual, expected)
}

func TestGetLocaleNotExist(t *testing.T) {
ctrl, ctx, logicService, appUserRepo, localeRepo := newCtrl(t)
defer ctrl.Finish()

userId := uuid.New().String()

appUserModel := &model.AppUser{
Id: 1,
Uuid: userId,
Created: sql.NullTime{Time: time.Now(), Valid: true},
Url: sql.NullString{String: "Url", Valid: true},
SelfLink: sql.NullString{String: "SelfLink", Valid: true},
DisplayName: sql.NullString{String: "DisplayName", Valid: true},
About: sql.NullString{String: "About", Valid: true},
}

appUserRepo.EXPECT().FindOneByUuid(ctx, userId).Return(appUserModel, nil)
localeRepo.EXPECT().FindOneByAppUserUuid(ctx, userId).Return(nil, model.ErrNotFound)

expected := errcode.Wrap(errcode.LocaleNotExist)

_, actual := logicService.Get(&user.GetReq{UserId: userId})
require.Equal(t, expected, actual)
}

0 comments on commit 23e4312

Please sign in to comment.