Skip to content

Commit

Permalink
add unit test : user repository
Browse files Browse the repository at this point in the history
  • Loading branch information
amalmadhu06 committed Apr 23, 2023
1 parent 9d18404 commit b14965c
Showing 1 changed file with 99 additions and 14 deletions.
113 changes: 99 additions & 14 deletions pkg/repository/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestCreateUser(t *testing.T) {
expectedErr: nil,
},
{ //test case for trying to insert a user with duplicate phone id
name: "duplicate phone",
name: "duplicate email",
input: model.UserDataInput{
FName: "Sujith",
LName: "S",
Expand All @@ -63,9 +63,9 @@ func TestCreateUser(t *testing.T) {
buildStub: func(mock sqlmock.Sqlmock) {
mock.ExpectQuery("^INSERT INTO users (.+)$").
WithArgs("Sujith", "S", "sujith@gmail.com", "7902638845", "sujith@123").
WillReturnError(errors.New("duplicate key value violates unique constraint 'phone'"))
WillReturnError(errors.New("duplicate key value violates unique constraint 'email'"))
},
expectedErr: errors.New("duplicate key value violates unique constraint 'phone'"),
expectedErr: errors.New("duplicate key value violates unique constraint 'email'"),
},
{ //test case for inserting a user with duplicate phone number
name: "duplicate phone",
Expand Down Expand Up @@ -159,17 +159,17 @@ func TestFindByEmail(t *testing.T) {
},
expectedErr: nil,
},
//{ //test case when user does not exists for the given mail
// name: "invalid phone",
// email: "nonexistinguser@gmail.com",
// expectedOutput: model.UserLoginVerifier{},
// buildStub: func(mock sqlmock.Sqlmock) {
// mock.ExpectQuery("SELECT users.id, users.f_name, users.l_name, users.phone, users.phone, users.password, infos.is_blocked, infos.is_verified FROM users as users FULL OUTER JOIN user_infos as infos ON users.id = infos.users_id WHERE users.phone (.+)$").
// WithArgs("nonexistinguser@gmail.com").
// WillReturnError(errors.New("no rows found"))
// },
// expectedErr: errors.New("no rows found"),
//},
{ //test case when user does not exists for the given mail
name: "invalid email",
email: "nonexistinguser@gmail.com",
expectedOutput: model.UserLoginVerifier{},
buildStub: func(mock sqlmock.Sqlmock) {
mock.ExpectQuery("SELECT users.id, users.f_name, users.l_name, users.email, users.phone, users.password, infos.is_blocked, infos.is_verified FROM users as users FULL OUTER JOIN user_infos as infos ON users.id = infos.users_id WHERE users.phone (.+)$").
WithArgs("nonexistinguser@gmail.com").
WillReturnError(errors.New("no rows found"))
},
expectedErr: errors.New("no rows found"),
},
}

for _, tt := range testData {
Expand Down Expand Up @@ -214,3 +214,88 @@ func TestFindByEmail(t *testing.T) {
})
}
}

func TestFindByPhone(t *testing.T) {
testData := []struct {
name string
phone string
expectedOutput model.UserLoginVerifier
buildStub func(mock sqlmock.Sqlmock)
expectedErr error
}{
{ //test case for finding a valid user
name: "valid phone",
phone: "7902638875",
expectedOutput: model.UserLoginVerifier{
ID: 3,
FName: "Sujith",
LName: "S",
Email: "sujith@gmail.com",
Phone: "7902638875",
Password: "sujith@123",
IsBlocked: false,
IsVerified: true,
},
buildStub: func(mock sqlmock.Sqlmock) {
columns := []string{"id", "f_name", "l_name", "email", "phone", "password", "is_blocked", "is_verified"}
mock.ExpectQuery("SELECT users.id, users.f_name, users.l_name, users.email, users.phone, users.password, infos.is_blocked, infos.is_verified FROM users as users FULL OUTER JOIN user_infos as infos ON users.id = infos.users_id WHERE users.phone (.+)$").
WithArgs("7902638875").
WillReturnRows(sqlmock.NewRows(columns).FromCSVString("3,Sujith,S,sujith@gmail.com,7902638875,sujith@123,f,t"))
},
expectedErr: nil,
},
{ //test case when user does not exists for the given mail
name: "invalid phone",
phone: "7902638875",
expectedOutput: model.UserLoginVerifier{},
buildStub: func(mock sqlmock.Sqlmock) {
mock.ExpectQuery("SELECT users.id, users.f_name, users.l_name, users.email, users.phone, users.password, infos.is_blocked, infos.is_verified FROM users as users FULL OUTER JOIN user_infos as infos ON users.id = infos.users_id WHERE users.phone (.+)$").
WithArgs("7902638875").
WillReturnError(errors.New("no rows found"))
},
expectedErr: errors.New("no rows found"),
},
}

for _, tt := range testData {
t.Run(tt.name, func(t *testing.T) {
// create an sqlmock database connection and mock to manage expectations
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}

// initialize a mock db session
gormDB, err := gorm.Open(postgres.New(postgres.Config{Conn: db}), &gorm.Config{})

if err != nil {
t.Fatalf("an error '%s' was not expected when initializing a mock db session", err)
}

//create NewUserRepository mock by passing a pointer to gorm.DB
userRepository := NewUserRepository(gormDB)

// before we actually execute our function, we need to expect required DB actions
tt.buildStub(mock)

//call the actual method
actualOutput, actualErr := userRepository.FindByPhone(context.TODO(), tt.phone)

// validate err is nil if we are not expecting to receive an error
if tt.expectedErr == nil {
assert.NoError(t, actualErr)
} else { //validate whether expected and actual errors are same
assert.Equal(t, tt.expectedErr, actualErr)
}

if !reflect.DeepEqual(tt.expectedOutput, actualOutput) {
t.Errorf("got %v, but want %v", actualOutput, tt.expectedOutput)
}

// Check that all expectations were met
if err = mock.ExpectationsWereMet(); err != nil {
t.Errorf("Unfulfilled expectations: %s", err)
}
})
}
}

0 comments on commit b14965c

Please sign in to comment.