Skip to content

Conversation

@MayurDeshmukh10
Copy link
Contributor

No description provided.

db/db.go Outdated

type Storer interface {
ListUsers(context.Context) ([]User, error)
CreateNewUser(context.Context, User) (User, error)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename CreateNewUser to CreateUser

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

db/user.go Outdated

// CreateNewUser = creates a new user in database
func (s *pgStore) CreateNewUser(ctx context.Context, u User) (newUser User, err error) {
tx, err := s.db.Beginx()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to use transaction. we can use s.db to run the query

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed transaction and instead used Exec

db/db.go Outdated
type Storer interface {
ListUsers(context.Context) ([]User, error)
CreateNewUser(context.Context, User) (User, error)
CheckUserByEmail(context.Context, string) (bool, User, error)
Copy link

@mohitpm mohitpm Sep 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think CheckUserByEmail(context.Context, string) (bool, User, error) is quite ambiguous.
if error occurred then ideally bool value should be true &
if error not occurred then bool value should be false

In CheckUserByEmail method we are using bool & err variable to denote if user is present or not, which i think is confusing

So, instead

Rename the method
From:- CheckUserByEmail(context.Context, string) (bool, User, error)
To :- GetUserByEmail(context.Context, string) (User, error)

so in GetUserByEmail method
if err == sql.NoRows then user is not present &
if err != nil && err != sql.NoRows then db error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

func (s *pgStore) CheckUserByEmail(ctx context.Context, email string) (check bool, user User, err error) {
err = s.db.Get(&user, getUserByEmailQuery, email)
if err != nil {
if err == sql.ErrNoRows {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to add
if err == sql.ErrNoRows condition in this func
caller of the func should handle it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have used if err == sql.ErrNoRows condition to avoid logging no row found errors and to log only database related errors

}

func (suite *UsersHandlerTestSuite) TestRegisterUserSuccess() {
suite.dbMock.On("CreateNewUser", mock.Anything, mock.Anything).Return(db.User{}, nil)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid using mock.Anything instead use actual User object
for context you can use mock.Anything

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't use mock.Anything for user then we have to manually fill the user object

db/db.go Outdated
type Storer interface {
ListUsers(context.Context) ([]User, error)
CreateUser(context.Context, User) (User, error)
// CheckUserByEmail(context.Context, string) (bool, User, error)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove commented code

// For checking if user already registered
_, err = deps.Store.GetUserByEmail(req.Context(), user.Email)

// If check true then user is already registered
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update the comment

_, err = deps.Store.GetUserByEmail(req.Context(), user.Email)

// If check true then user is already registered
if err != sql.ErrNoRows && err == nil {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only err == nil is enough

}

func (suite *UsersHandlerTestSuite) TestRegisterUserFailure() {
suite.dbMock.On("CreateUser", mock.Anything, mock.Anything).Return(db.User{}, nil)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid using mock.Anything.

you can you mock.Anything for context.Context

logger.WithField("err", err.Error()).Error("Error while preparing user insert query")
return
}
_, err = stmt.Exec(u)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s.db.NamedExec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants