Skip to content

Commit

Permalink
refactor: use correct assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
Eyo Chen committed Nov 30, 2024
1 parent 1e96a72 commit 671ae35
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 26 deletions.
8 changes: 3 additions & 5 deletions internal/adapter/repository/icon/icon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,9 @@ func getByID_WithoutIcon_ReturnErr(s *IconSuite, desc string) {
_, err := s.f.InsertMany(mockCTX, 2)
s.Require().NoError(err, desc)

expRes := domain.DefaultIcon{}

res, err := s.repo.GetByID(mockCTX, 999)
s.Require().Equal(expRes, res, desc)
s.Require().Equal(domain.ErrIconNotFound, err, desc)
s.Require().Empty(res, desc)
s.Require().ErrorIs(err, domain.ErrIconNotFound, desc)
}

func (s *IconSuite) TestList() {
Expand Down Expand Up @@ -186,5 +184,5 @@ func getByIDs_WithoutIcon_ReturnErr(s *IconSuite, desc string) {
ids := []int64{999}
res, err := s.repo.GetByIDs(ids)
s.Require().Nil(res, desc)
s.Require().Equal(domain.ErrIconNotFound, err, desc)
s.Require().ErrorIs(err, domain.ErrIconNotFound, desc)
}
2 changes: 1 addition & 1 deletion internal/adapter/repository/maincateg/maincateg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func getByID_FindNoData_ReturnSuccessfully(s *MainCategSuite, desc string) {
s.Require().NoError(err, desc)

result, err := s.mainCategRepo.GetByID(0, user.ID)
s.Require().Equal(domain.ErrMainCategNotFound, err, desc)
s.Require().ErrorIs(err, domain.ErrMainCategNotFound, desc)
s.Require().Nil(result, desc)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,5 +347,5 @@ func getByUserIDAndMonthDate_DataNotFound_ReturnError(s *MonthlyTransSuite, desc

// assertion
s.Require().ErrorIs(err, domain.ErrDataNotFound, desc)
s.Require().Equal(domain.AccInfo{}, accInfo, desc)
s.Require().Empty(accInfo, desc)
}
10 changes: 5 additions & 5 deletions internal/adapter/repository/subcateg/subcateg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func create_DuplicateNameUserMainCateg_ReturnError(s *SubCategSuite, desc string

// action and check
err = s.subCategRepo.Create(inputSubCateg, user.ID)
s.Require().Equal(domain.ErrUniqueNameUserMainCateg, err, desc)
s.Require().ErrorIs(err, domain.ErrUniqueNameUserMainCateg, desc)
}

func (s *SubCategSuite) TestGetByMainCategID() {
Expand Down Expand Up @@ -334,7 +334,7 @@ func update_DuplicateName_ReturnError(s *SubCategSuite, desc string) {

// action and check
err = s.subCategRepo.Update(inputSubCateg)
s.Require().Equal(domain.ErrUniqueNameUserMainCateg, err, desc)
s.Require().ErrorIs(err, domain.ErrUniqueNameUserMainCateg, desc)
}

func (s *SubCategSuite) TestDelete() {
Expand All @@ -357,7 +357,7 @@ func (s *SubCategSuite) TestDelete() {
var result SubCateg
checkStmt := `SELECT id, name, main_category_id FROM sub_categories WHERE id = ?`
err = s.db.QueryRow(checkStmt, mainCategIDToSubCategs[mainCateg.ID][0].ID).Scan(&result.ID, &result.Name, &result.MainCategID)
s.Require().Equal(sql.ErrNoRows, err, "test delete")
s.Require().ErrorIs(err, sql.ErrNoRows, "test delete")

// check to see if the first main category still has the other sub categories
checkStmt = `SELECT id, name, main_category_id FROM sub_categories WHERE main_category_id = ?`
Expand Down Expand Up @@ -426,7 +426,7 @@ func getByID_FindNoData_ReturnError(s *SubCategSuite, desc string) {

// action
result, err := s.subCategRepo.GetByID(mainCateg.ID+999, user.ID)
s.Require().Equal(domain.ErrSubCategNotFound, err, desc)
s.Require().ErrorIs(err, domain.ErrSubCategNotFound, desc)
s.Require().Nil(result, desc)
}

Expand Down Expand Up @@ -606,7 +606,7 @@ func createBatch_AlreadyExistData_ReturnError(s *SubCategSuite, desc string) {

// action
err = s.subCategRepo.BatchCreate(mockCTX, subCategs, user.ID)
s.Require().Equal(domain.ErrUniqueNameUserMainCateg, err, desc)
s.Require().ErrorIs(err, domain.ErrUniqueNameUserMainCateg, desc)

// check
checkStmt := `SELECT COUNT(*)
Expand Down
6 changes: 3 additions & 3 deletions internal/adapter/repository/transaction/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ func delete_WithOneData_DeleteSuccessfully(s *TransactionSuite, desc string) {
var checkT Transaction
stmt := "SELECT id FROM transactions WHERE id = ?"
err = s.db.QueryRow(stmt, transactions[0].ID).Scan(&checkT.ID)
s.Require().Equal(sql.ErrNoRows, err, desc)
s.Require().ErrorIs(err, sql.ErrNoRows, desc)
}

func delete_WithMultipleData_DeleteSuccessfully(s *TransactionSuite, desc string) {
Expand All @@ -972,7 +972,7 @@ func delete_WithMultipleData_DeleteSuccessfully(s *TransactionSuite, desc string
var checkT Transaction
stmt := "SELECT id FROM transactions WHERE id = ?"
err = s.db.QueryRow(stmt, transactions[0].ID).Scan(&checkT.ID)
s.Require().Equal(sql.ErrNoRows, err, desc)
s.Require().ErrorIs(err, sql.ErrNoRows, desc)
}

func delete_WithMultipleUsers_DeleteSuccessfully(s *TransactionSuite, desc string) {
Expand All @@ -995,7 +995,7 @@ func delete_WithMultipleUsers_DeleteSuccessfully(s *TransactionSuite, desc strin
var checkT Transaction
stmt := "SELECT id FROM transactions WHERE id = ?"
err = s.db.QueryRow(stmt, transactions[0].ID).Scan(&checkT.ID)
s.Require().Equal(sql.ErrNoRows, err, desc)
s.Require().ErrorIs(err, sql.ErrNoRows, desc)

// check if other user's data still exists
var countUser2 int
Expand Down
4 changes: 2 additions & 2 deletions internal/usecase/transaction/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func getAll_NoError_ReturnTransactions(s *TransactionSuite, desc string) {
result, cursor, err := s.uc.GetAll(mockCtx, mockOpt, mockUser)
s.Require().NoError(err, desc)
s.Require().Equal(mockTrans, result, desc)
s.Require().Equal(domain.Cursor{}, cursor, desc)
s.Require().Empty(cursor, desc)
}

func getAll_GetTransFail_ReturnError(s *TransactionSuite, desc string) {
Expand All @@ -262,7 +262,7 @@ func getAll_GetTransFail_ReturnError(s *TransactionSuite, desc string) {
result, cursor, err := s.uc.GetAll(mockCtx, mockOpt, mockUser)
s.Require().Equal(errors.New("error"), err, desc)
s.Require().Nil(result, desc)
s.Require().Equal(domain.Cursor{}, cursor, desc)
s.Require().Empty(cursor, desc)
}

func getAll_InitPageWithSize_ReturnCorrectCursor(s *TransactionSuite, desc string) {
Expand Down
8 changes: 4 additions & 4 deletions internal/usecase/user/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func singup_EmailExists_ReturnError(s *UserSuite, desc string) {
s.mockUserRepo.On("FindByEmail", "email.com").Return(mockUser, nil).Once()

token, err := s.uc.Signup(mockCTX, mockUser)
s.Require().Equal(domain.ErrEmailAlreadyExists, err, desc)
s.Require().ErrorIs(err, domain.ErrEmailAlreadyExists, desc)
s.Require().Empty(token, desc)
}

Expand Down Expand Up @@ -154,7 +154,7 @@ func login_EmailNotExists_ReturnError(s *UserSuite, desc string) {
Password: "password",
}
token, err := s.uc.Login(mockCTX, input)
s.Require().Equal(domain.ErrAuthentication, err, desc)
s.Require().ErrorIs(err, domain.ErrAuthentication, desc)
s.Require().Empty(token, desc)
}

Expand All @@ -176,7 +176,7 @@ func login_PasswordNotMatch_ReturnError(s *UserSuite, desc string) {
Password: "password2", // wrong password
}
token, err := s.uc.Login(mockCTX, input)
s.Require().Equal(domain.ErrAuthentication, err, desc)
s.Require().ErrorIs(err, domain.ErrAuthentication, desc)
s.Require().Empty(token, desc)
}

Expand Down Expand Up @@ -325,6 +325,6 @@ func getInfo_GetFail_ReturnError(s *UserSuite, desc string) {
s.mockUserRepo.On("GetInfo", int64(1)).Return(domain.User{}, domain.ErrUserIDNotFound).Once()

user, err := s.uc.GetInfo(1)
s.Require().Equal(domain.ErrUserIDNotFound, err, desc)
s.Require().ErrorIs(err, domain.ErrUserIDNotFound, desc)
s.Require().Empty(user, desc)
}
10 changes: 5 additions & 5 deletions pkg/codeutil/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func decodeNextKeys_EncodedEmptyString_ReturnErr(s *CodeUtilSuite, desc string)
// action
result, err := codeutil.DecodeNextKeys(encodedString, nil)
s.Require().Nil(result, desc)
s.Require().Equal(codeutil.ErrEmptyEncodedString, err, desc)
s.Require().ErrorIs(err, codeutil.ErrEmptyEncodedString, desc)
}

func decodeNextKeys_DecodedEmptyString_ReturnErr(s *CodeUtilSuite, desc string) {
Expand All @@ -56,7 +56,7 @@ func decodeNextKeys_DecodedEmptyString_ReturnErr(s *CodeUtilSuite, desc string)
// action
result, err := codeutil.DecodeNextKeys(encodedString, nil)
s.Require().Nil(result, desc)
s.Require().Equal(codeutil.ErrEmptyEncodedString, err, desc)
s.Require().ErrorIs(err, codeutil.ErrEmptyEncodedString, desc)
}

func decodeNextKeys_InvalidFormatCursor_ReturnErr(s *CodeUtilSuite, desc string) {
Expand All @@ -67,7 +67,7 @@ func decodeNextKeys_InvalidFormatCursor_ReturnErr(s *CodeUtilSuite, desc string)
// action
result, err := codeutil.DecodeNextKeys(encodedString, nil)
s.Require().Nil(result, desc)
s.Require().Equal(codeutil.ErrInvalidFormatCursor, err, desc)
s.Require().ErrorIs(err, codeutil.ErrInvalidFormatCursor, desc)
}

func decodeNextKeys_SourceFieldNotFound_ReturnErr(s *CodeUtilSuite, desc string) {
Expand All @@ -83,7 +83,7 @@ func decodeNextKeys_SourceFieldNotFound_ReturnErr(s *CodeUtilSuite, desc string)
// action
result, err := codeutil.DecodeNextKeys(encodedString, fieldSource)
s.Require().Nil(result, desc)
s.Require().Equal(codeutil.ErrFieldNotFound, err, desc)
s.Require().ErrorIs(err, codeutil.ErrFieldNotFound, desc)
}

func decodeNextKeys_ValidEncodedString_ReturnCursorMap(s *CodeUtilSuite, desc string) {
Expand Down Expand Up @@ -154,7 +154,7 @@ func encodeNextKeys_FieldNotFound_ReturnErr(s *CodeUtilSuite, desc string) {
// action
result, err := codeutil.EncodeNextKeys(nextKeys, fieldSource)
s.Require().Empty(result, desc)
s.Require().Equal(codeutil.ErrFieldNotFound, err, desc)
s.Require().ErrorIs(err, codeutil.ErrFieldNotFound, desc)
}

func encodeNextKeys_ValidCursorMap_ReturnEncodedString(s *CodeUtilSuite, desc string) {
Expand Down

0 comments on commit 671ae35

Please sign in to comment.