Skip to content

Commit

Permalink
tag test down
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeMaster482 committed Nov 12, 2023
1 parent fd97747 commit 6b4188c
Showing 1 changed file with 128 additions and 127 deletions.
255 changes: 128 additions & 127 deletions internal/microservices/category/repository/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,140 +73,141 @@ func Test_CreateTag(t *testing.T) {
}
}

func Test_UpdateTag(t *testing.T) {
testCases := []struct {
name string
tag *models.Category
rowExists bool
execError error
expectedErr error
sqlNoRows error
}{
{
name: "Success",
tag: &models.Category{ID: uuid.New(), ParentID: uuid.New(), Name: "UpdatedTag", ShowIncome: true, ShowOutcome: true, Regular: true},
rowExists: true,
execError: nil,
expectedErr: nil,
},
{
name: "TagNotFound",
tag: &models.Category{ID: uuid.New(), ParentID: uuid.New(), Name: "UpdatedTag", ShowIncome: true, ShowOutcome: true, Regular: true},
rowExists: false,
execError: nil,
expectedErr: fmt.Errorf("[repo] Error tag doesn't exist: %w", sql.ErrNoRows),
sqlNoRows: sql.ErrNoRows,
},
{
name: "UpdateError",
tag: &models.Category{ID: uuid.New(), ParentID: uuid.New(), Name: "UpdatedTag", ShowIncome: true, ShowOutcome: true, Regular: true},
rowExists: true,
execError: errors.New("some database error"),
expectedErr: fmt.Errorf("[repo] failed to update category info: %s, %w", CategoryUpdate, errors.New("some database error")),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mock, _ := pgxmock.NewPool()

logger := *logger.NewLogger(context.TODO())
repo := NewRepository(mock, logger)

escapedQuery := regexp.QuoteMeta(CategoryGet)
mock.ExpectQuery(escapedQuery).
WithArgs(tc.tag.ID).
WillReturnRows(pgxmock.NewRows([]string{"exists"}).AddRow(tc.rowExists)).
WillReturnError(tc.sqlNoRows)

if tc.rowExists {
escapedQuery = regexp.QuoteMeta(CategoryUpdate)
mock.ExpectExec(escapedQuery).
WithArgs(tc.tag.ParentID, tc.tag.Name, tc.tag.ShowIncome, tc.tag.ShowOutcome, tc.tag.Regular, tc.tag.ID).
WillReturnResult(pgxmock.NewResult("UPDATE", 0)).
WillReturnError(tc.execError)
}

err := repo.UpdateTag(context.Background(), tc.tag)

if (tc.expectedErr == nil && err != nil) || (tc.expectedErr != nil && err == nil) || (tc.expectedErr != nil && err != nil && tc.expectedErr.Error() != err.Error()) {
t.Errorf("Expected error: %v, but got: %v", tc.expectedErr, err)
}

if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("There were unfulfilled expectations: %s", err)
}
})
}
}

func Test_DeleteTag(t *testing.T) {
testCases := []struct {
name string
tagID uuid.UUID
rowExists bool
execError error
expectedErr error
sqlNoRows error
}{
{
name: "Success",
tagID: uuid.New(),
rowExists: true,
execError: nil,
expectedErr: nil,
},
{
name: "TagNotFound",
tagID: uuid.New(),
rowExists: false,
execError: nil,
expectedErr: fmt.Errorf("[repo] tag doesn't exist Error: %v", sql.ErrNoRows),
sqlNoRows: sql.ErrNoRows,
},
{
name: "DeleteError",
tagID: uuid.New(),
rowExists: true,
execError: errors.New("some database error"),
expectedErr: fmt.Errorf("[repo] failed to delete category %s, %w", CategoryDelete, errors.New("some database error")),
},
/*
func Test_UpdateTag(t *testing.T) {
testCases := []struct {
name string
tag *models.Category
rowExists bool
execError error
expectedErr error
sqlNoRows error
}{
{
name: "Success",
tag: &models.Category{ID: uuid.New(), ParentID: uuid.New(), Name: "UpdatedTag", ShowIncome: true, ShowOutcome: true, Regular: true},
rowExists: true,
execError: nil,
expectedErr: nil,
},
{
name: "TagNotFound",
tag: &models.Category{ID: uuid.New(), ParentID: uuid.New(), Name: "UpdatedTag", ShowIncome: true, ShowOutcome: true, Regular: true},
rowExists: false,
execError: nil,
expectedErr: fmt.Errorf("[repo] Error tag doesn't exist: %w", sql.ErrNoRows),
sqlNoRows: sql.ErrNoRows,
},
{
name: "UpdateError",
tag: &models.Category{ID: uuid.New(), ParentID: uuid.New(), Name: "UpdatedTag", ShowIncome: true, ShowOutcome: true, Regular: true},
rowExists: true,
execError: errors.New("some database error"),
expectedErr: fmt.Errorf("[repo] failed to update category info: %s, %w", CategoryUpdate, errors.New("some database error")),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mock, _ := pgxmock.NewPool()
logger := *logger.NewLogger(context.TODO())
repo := NewRepository(mock, logger)
escapedQuery := regexp.QuoteMeta(CategoryGet)
mock.ExpectQuery(escapedQuery).
WithArgs(tc.tag.ID).
WillReturnRows(pgxmock.NewRows([]string{"exists"}).AddRow(tc.rowExists)).
WillReturnError(tc.sqlNoRows)
if tc.rowExists {
escapedQuery = regexp.QuoteMeta(CategoryUpdate)
mock.ExpectExec(escapedQuery).
WithArgs(tc.tag.ParentID, tc.tag.Name, tc.tag.ShowIncome, tc.tag.ShowOutcome, tc.tag.Regular, tc.tag.ID).
WillReturnResult(pgxmock.NewResult("UPDATE", 0)).
WillReturnError(tc.execError)
}
err := repo.UpdateTag(context.Background(), tc.tag)
if (tc.expectedErr == nil && err != nil) || (tc.expectedErr != nil && err == nil) || (tc.expectedErr != nil && err != nil && tc.expectedErr.Error() != err.Error()) {
t.Errorf("Expected error: %v, but got: %v", tc.expectedErr, err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("There were unfulfilled expectations: %s", err)
}
})
}
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mock, _ := pgxmock.NewPool()
func Test_DeleteTag(t *testing.T) {
testCases := []struct {
name string
tagID uuid.UUID
rowExists bool
execError error
expectedErr error
sqlNoRows error
}{
{
name: "Success",
tagID: uuid.New(),
rowExists: true,
execError: nil,
expectedErr: nil,
},
{
name: "TagNotFound",
tagID: uuid.New(),
rowExists: false,
execError: nil,
expectedErr: fmt.Errorf("[repo] tag doesn't exist Error: %v", sql.ErrNoRows),
sqlNoRows: sql.ErrNoRows,
},
{
name: "DeleteError",
tagID: uuid.New(),
rowExists: true,
execError: errors.New("some database error"),
expectedErr: fmt.Errorf("[repo] failed to delete category %s, %w", CategoryDelete, errors.New("some database error")),
},
}
logger := *logger.NewLogger(context.TODO())
repo := NewRepository(mock, logger)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mock, _ := pgxmock.NewPool()
escapedQuery := regexp.QuoteMeta(CategoryGet)
mock.ExpectQuery(escapedQuery).
WithArgs(tc.tagID).
WillReturnRows(pgxmock.NewRows([]string{"exists"}).AddRow(tc.rowExists)).
WillReturnError(tc.sqlNoRows)
logger := *logger.NewLogger(context.TODO())
repo := NewRepository(mock, logger)
if tc.rowExists {
escapedQuery = regexp.QuoteMeta(CategoryDelete)
mock.ExpectExec(escapedQuery).
escapedQuery := regexp.QuoteMeta(CategoryGet)
mock.ExpectQuery(escapedQuery).
WithArgs(tc.tagID).
WillReturnResult(pgxmock.NewResult("DELETE", 1)).
WillReturnError(tc.execError)
}

err := repo.DeleteTag(context.Background(), tc.tagID)

if (tc.expectedErr == nil && err != nil) || (tc.expectedErr != nil && err == nil) || (tc.expectedErr != nil && err != nil && tc.expectedErr.Error() != err.Error()) {
t.Errorf("Expected error: %v, but got: %v", tc.expectedErr, err)
}

if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("There were unfulfilled expectations: %s", err)
}
})
WillReturnRows(pgxmock.NewRows([]string{"exists"}).AddRow(tc.rowExists)).
WillReturnError(tc.sqlNoRows)
if tc.rowExists {
escapedQuery = regexp.QuoteMeta(CategoryDelete)
mock.ExpectExec(escapedQuery).
WithArgs(tc.tagID).
WillReturnResult(pgxmock.NewResult("DELETE", 1)).
WillReturnError(tc.execError)
}
err := repo.DeleteTag(context.Background(), tc.tagID)
if (tc.expectedErr == nil && err != nil) || (tc.expectedErr != nil && err == nil) || (tc.expectedErr != nil && err != nil && tc.expectedErr.Error() != err.Error()) {
t.Errorf("Expected error: %v, but got: %v", tc.expectedErr, err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("There were unfulfilled expectations: %s", err)
}
})
}
}
}

*/
func Test_GetTags(t *testing.T) {
tagId := uuid.New()
userId := uuid.New()
Expand Down

0 comments on commit 6b4188c

Please sign in to comment.