Skip to content

Commit

Permalink
Merge pull request #34 from kripsy/implement-unit-test-09112023
Browse files Browse the repository at this point in the history
Implement unit test 09112023
  • Loading branch information
kripsy authored Nov 10, 2023
2 parents 2b6d7e8 + b14b0fa commit 0d25494
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 1 deletion.
7 changes: 6 additions & 1 deletion MakeFile
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ lint-all:
golangci-lint run ./internal/utils/...

lint-one:
golangci-lint run --no-config --disable-all --enable goerr113 ./internal/utils/...
golangci-lint run --no-config --disable-all --enable goerr113 ./internal/utils/...


cover:
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
143 changes: 143 additions & 0 deletions internal/client/usecase/menu_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
//nolint:staticcheck,testpackage,nolintlint
package usecase

import (
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/kripsy/GophKeeper/internal/client/grpc"
"github.com/kripsy/GophKeeper/internal/client/infrastrucrure/filemanager"
"github.com/kripsy/GophKeeper/internal/client/infrastrucrure/ui"
mock_ui "github.com/kripsy/GophKeeper/internal/client/infrastrucrure/ui/mocks"
"github.com/kripsy/GophKeeper/internal/models"
"github.com/rs/zerolog"
)

func TestClientUsecaseInMenu(t *testing.T) {
type fields struct {
dataPath string
uploadPath string
aboutMsg string
userData *models.UserData
grpc grpc.Client
fileManager filemanager.FileStorage
//nolint:unused
ui ui.UserInterface
log zerolog.Logger
}
tests := []struct {
name string
fields fields
menuInput string
expectedCalls int
}{
{
name: "Test Add Secret",
fields: fields{
userData: &models.UserData{
Meta: models.UserMeta{
IsSyncStorage: false,
},
},
},
menuInput: ui.SyncSecrets,
expectedCalls: 1,
},
{
name: "Test Sync Secrets",
fields: fields{
userData: &models.UserData{
Meta: models.UserMeta{
IsSyncStorage: false,
},
},
},
menuInput: ui.AddSecretKey,
expectedCalls: 1,
},
{
name: "Test SecretsKey",
fields: fields{
userData: &models.UserData{
Meta: models.UserMeta{
IsSyncStorage: false,
},
},
},
menuInput: ui.SecretsKey,
expectedCalls: 1,
},
// {
// name: "Test ExitKey",
// fields: fields{
// userData: &models.UserData{
// Meta: models.UserMeta{
// IsSyncStorage: false,
// },
// },
// },
// menuInput: ui.ExitKey,
// expectedCalls: 1,
// },
{
name: "Test About",
fields: fields{
userData: &models.UserData{
Meta: models.UserMeta{
IsSyncStorage: false,
},
},
},
menuInput: ui.About,
expectedCalls: 1,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockUI := mock_ui.NewMockUserInterface(ctrl)
c := &ClientUsecase{
dataPath: tt.fields.dataPath,
uploadPath: tt.fields.uploadPath,
aboutMsg: tt.fields.aboutMsg,
userData: tt.fields.userData,
grpc: tt.fields.grpc,
fileManager: tt.fields.fileManager,
ui: mockUI,
log: tt.fields.log,
}
ok, val := getPositionMenu(tt.menuInput)

if ok {
mockUI.EXPECT().Menu(gomock.Any()).Return(val).AnyTimes()
mockUI.EXPECT().ChooseSecretType().Return(1, false).AnyTimes()
mockUI.EXPECT().GetSecret(gomock.Any()).Return("a", false).AnyTimes()
mockUI.EXPECT().UpdateSecret(gomock.Any()).Return("a", 1, true).AnyTimes()
mockUI.EXPECT().AddMetaInfo().Return(models.DataInfo{
Name: "asd",
DataID: "aaa",
DataType: 1,
Description: "asd",
Hash: "asd",
UpdatedAt: time.Now(),
}, nil).AnyTimes()

go c.InMenu()
}
time.Sleep(2 * time.Second)
})
}
}

func getPositionMenu(str string) (bool, int) {
for k, v := range ui.MenuTable {
if v == str {
return true, k
}
}

return false, 0
}
25 changes: 25 additions & 0 deletions internal/client/usecase/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,31 @@ func TestClientUsecaseDownloadServerMeta(t *testing.T) {
},
want: nil,
},

{
name: "failed unmarshall data",
args: args{
ctx: context.Background(),
syncKey: syncKey,
},
setup: func() {
dataChan := make(chan []byte, 1)

encryptedData, err := utils.Encrypt([]byte("bar"), key)
assert.NoError(t, err)
dataChan <- encryptedData
close(dataChan)
mockClient.EXPECT().DownloadFile(gomock.Any(),
gomock.Any(), gomock.Any(),
syncKey).Return(dataChan, nil).Times(1)
},
wantErr: true,
fields: fields{
grpc: mockClient,
userData: userData,
},
want: nil,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 0d25494

Please sign in to comment.