Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more test cases for the /database/sql/users.go #108

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 81 additions & 2 deletions database/sql/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,40 @@ package sql

import (
"context"
"flag"
"fmt"
"regexp"
"testing"

dbCommon "github.com/cloudbase/garm/database/common"
garmTesting "github.com/cloudbase/garm/internal/testing"
"github.com/cloudbase/garm/params"
"github.com/stretchr/testify/suite"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)

type UserTestFixtures struct {
Users []params.User
NewUserParams params.NewUserParams
UpdateUserParams params.UpdateUserParams
SQLMock sqlmock.Sqlmock
}

type UserTestSuite struct {
suite.Suite
Store dbCommon.Store
Fixtures *UserTestFixtures
Store dbCommon.Store
StoreSQLMocked *sqlDatabase
Fixtures *UserTestFixtures
}

func (s *UserTestSuite) assertSQLMockExpectations() {
err := s.Fixtures.SQLMock.ExpectationsWereMet()
if err != nil {
s.FailNow(fmt.Sprintf("failed to meet sqlmock expectations, got error: %v", err))
}
}

func (s *UserTestSuite) SetupTest() {
Expand Down Expand Up @@ -64,6 +79,29 @@ func (s *UserTestSuite) SetupTest() {
users = append(users, user)
}

// create store with mocked sql connection
sqlDB, sqlMock, err := sqlmock.New()
if err != nil {
s.FailNow(fmt.Sprintf("failed to run 'sqlmock.New()', got error: %v", err))
}
s.T().Cleanup(func() { sqlDB.Close() })
mysqlConfig := mysql.Config{
Conn: sqlDB,
SkipInitializeWithVersion: true,
}
gormConfig := &gorm.Config{}
if flag.Lookup("test.v").Value.String() == "false" {
gormConfig.Logger = logger.Default.LogMode(logger.Silent)
}
gormConn, err := gorm.Open(mysql.New(mysqlConfig), gormConfig)
if err != nil {
s.FailNow(fmt.Sprintf("fail to open gorm connection: %v", err))
}
s.StoreSQLMocked = &sqlDatabase{
conn: gormConn,
cfg: garmTesting.GetTestSqliteDBConfig(s.T()),
}

// setup test fixtures
var enabled bool
fixtures := &UserTestFixtures{
Expand All @@ -79,6 +117,7 @@ func (s *UserTestSuite) SetupTest() {
Password: "test-update-password",
Enabled: &enabled,
},
SQLMock: sqlMock,
}
s.Fixtures = fixtures
}
Expand Down Expand Up @@ -126,6 +165,28 @@ func (s *UserTestSuite) TestCreateUserEmailAlreadyExist() {
s.Require().Equal(("email already exists"), err.Error())
}

func (s *UserTestSuite) TestCreateUserDBCreateErr() {
s.Fixtures.SQLMock.
ExpectQuery(regexp.QuoteMeta("SELECT * FROM `users` WHERE username = ? AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1")).
WithArgs(s.Fixtures.NewUserParams.Username).
WillReturnRows(sqlmock.NewRows([]string{"id"}))
s.Fixtures.SQLMock.
ExpectQuery(regexp.QuoteMeta("SELECT * FROM `users` WHERE email = ? AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1")).
WithArgs(s.Fixtures.NewUserParams.Email).
WillReturnRows(sqlmock.NewRows([]string{"id"}))
s.Fixtures.SQLMock.ExpectBegin()
s.Fixtures.SQLMock.
ExpectExec("INSERT INTO `users`").
WillReturnError(fmt.Errorf("creating user mock error"))
s.Fixtures.SQLMock.ExpectRollback()

_, err := s.StoreSQLMocked.CreateUser(context.Background(), s.Fixtures.NewUserParams)

s.assertSQLMockExpectations()
s.Require().NotNil(err)
s.Require().Equal("creating user: creating user mock error", err.Error())
}

func (s *UserTestSuite) TestHasAdminUserNoAdmin() {
hasAdmin := s.Store.HasAdminUser(context.Background())

Expand Down Expand Up @@ -194,6 +255,24 @@ func (s *UserTestSuite) TestUpdateUserNotFound() {
s.Require().Equal("fetching user: not found", err.Error())
}

func (s *UserTestSuite) TestUpdateUserDBSaveErr() {
s.Fixtures.SQLMock.
ExpectQuery(regexp.QuoteMeta("SELECT * FROM `users` WHERE username = ? AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1")).
WithArgs(s.Fixtures.Users[0].ID).
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(s.Fixtures.Users[0].ID))
s.Fixtures.SQLMock.ExpectBegin()
s.Fixtures.SQLMock.
ExpectExec(("UPDATE `users` SET")).
WillReturnError(fmt.Errorf("saving user mock error"))
s.Fixtures.SQLMock.ExpectRollback()

_, err := s.StoreSQLMocked.UpdateUser(context.Background(), s.Fixtures.Users[0].ID, s.Fixtures.UpdateUserParams)

s.assertSQLMockExpectations()
s.Require().NotNil(err)
s.Require().Equal("saving user: saving user mock error", err.Error())
}

func TestUserTestSuite(t *testing.T) {
suite.Run(t, new(UserTestSuite))
}