From e201b12db5249ec2e5d13cfa81af58efbd6f2400 Mon Sep 17 00:00:00 2001 From: OXYgen Date: Thu, 14 Nov 2024 20:25:47 +0500 Subject: [PATCH] Added tests to new AddTask, DeleteTask, AddParticipant, DeleteParticipant --- internal/contests/repository/pg_repository.go | 7 +- .../contests/repository/pg_repository_test.go | 96 +++++++++++++++++++ 2 files changed, 100 insertions(+), 3 deletions(-) diff --git a/internal/contests/repository/pg_repository.go b/internal/contests/repository/pg_repository.go index 8d28144..2777562 100644 --- a/internal/contests/repository/pg_repository.go +++ b/internal/contests/repository/pg_repository.go @@ -68,11 +68,11 @@ func (r *ContestRepository) DeleteContest(ctx context.Context, id int32) error { return nil } -const addTaskQuery = "INSERT INTO task (problem_id, contest_id,position) VALUES (?, ?,(SELECT COALESCE(MAX(position),0) + 1 FROM task)) RETURNING id" +const addTaskQuery = "INSERT INTO task (problem_id, contest_id, position) VALUES (?, ?,(SELECT COALESCE(MAX(position),0) + 1 FROM task)) RETURNING id" -func (r *ContestRepository) AddTask(ctx context.Context, contestId int32, taskId int32) (int32, error) { +func (r *ContestRepository) AddTask(ctx context.Context, contestId int32, problem_id int32) (int32, error) { query := r.db.Rebind(addTaskQuery) - rows, err := r.db.QueryxContext(ctx, query, taskId, contestId) + rows, err := r.db.QueryxContext(ctx, query, problem_id, contestId) if err != nil { return 0, handlePgErr(err) } @@ -108,6 +108,7 @@ func (r *ContestRepository) AddParticipant(ctx context.Context, contestId int32, } defer rows.Close() var id int32 + rows.Next() err = rows.Scan(&id) if err != nil { return 0, err diff --git a/internal/contests/repository/pg_repository_test.go b/internal/contests/repository/pg_repository_test.go index beee36a..2440713 100644 --- a/internal/contests/repository/pg_repository_test.go +++ b/internal/contests/repository/pg_repository_test.go @@ -56,3 +56,99 @@ func TestContestRepository_DeleteContest(t *testing.T) { require.NoError(t, err) }) } + +func TestContestRepository_AddTask(t *testing.T) { + t.Parallel() + + db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) + require.NoError(t, err) + defer db.Close() + + sqlxDB := sqlx.NewDb(db, "sqlmock") + defer sqlxDB.Close() + + contestRepo := NewContestRepository(sqlxDB, zap.NewNop()) + + t.Run("valid task additional", func(t *testing.T) { + taskId := int32(1) + contestId := int32(1) + + rows := sqlmock.NewRows([]string{"id"}).AddRow(1) + + mock.ExpectQuery(sqlxDB.Rebind(addTaskQuery)).WithArgs(taskId, contestId).WillReturnRows(rows) + + id, err := contestRepo.AddTask(context.Background(), contestId, taskId) + + require.NoError(t, err) + require.Equal(t, int32(1), id) + + }) +} + +func TestContestRepository_DeleteTask(t *testing.T) { + t.Parallel() + db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) + require.NoError(t, err) + defer db.Close() + + sqlxDB := sqlx.NewDb(db, "sqlmock") + defer sqlxDB.Close() + contestRepo := NewContestRepository(sqlxDB, zap.NewNop()) + t.Run("valid task deletion", func(t *testing.T) { + id := int32(1) + rows := sqlmock.NewResult(1, 1) + + mock.ExpectExec(sqlxDB.Rebind(deleteTaskQuery)).WithArgs(id).WillReturnResult(rows) + + err = contestRepo.DeleteTask(context.Background(), id) + require.NoError(t, err) + }) +} + +func TestContestRepository_AddParticipant(t *testing.T) { + t.Parallel() + db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) + require.NoError(t, err) + defer db.Close() + + sqlxDB := sqlx.NewDb(db, "sqlmock") + defer sqlxDB.Close() + contestRepo := NewContestRepository(sqlxDB, zap.NewNop()) + + t.Run("valid participant addition", func(t *testing.T) { + contestId := int32(1) + userId := int32(1) + name := "" + + rows := sqlmock.NewRows([]string{"id"}).AddRow(1) + + mock.ExpectQuery(sqlxDB.Rebind(addParticipantQuery)).WithArgs(contestId, userId, name).WillReturnRows(rows) + + id, err := contestRepo.AddParticipant(context.Background(), contestId, userId) + + require.NoError(t, err) + require.Equal(t, int32(1), id) + }) +} + +func TestContestRepository_DeleteParticipant(t *testing.T) { + t.Parallel() + db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) + require.NoError(t, err) + defer db.Close() + + sqlxDB := sqlx.NewDb(db, "sqlmock") + defer sqlxDB.Close() + + contestRepo := NewContestRepository(sqlxDB, zap.NewNop()) + + t.Run("valid participant deletion", func(t *testing.T) { + id := int32(1) + rows := sqlmock.NewResult(1, 1) + + mock.ExpectExec(sqlxDB.Rebind(deleteParticipantQuery)).WithArgs(id).WillReturnResult(rows) + + err = contestRepo.DeleteParticipant(context.Background(), id) + require.NoError(t, err) + }) +}