Added tests to new AddTask, DeleteTask, AddParticipant, DeleteParticipant
This commit is contained in:
parent
f7dd1bc806
commit
e201b12db5
2 changed files with 100 additions and 3 deletions
|
@ -68,11 +68,11 @@ func (r *ContestRepository) DeleteContest(ctx context.Context, id int32) error {
|
||||||
return nil
|
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)
|
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 {
|
if err != nil {
|
||||||
return 0, handlePgErr(err)
|
return 0, handlePgErr(err)
|
||||||
}
|
}
|
||||||
|
@ -108,6 +108,7 @@ func (r *ContestRepository) AddParticipant(ctx context.Context, contestId int32,
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
var id int32
|
var id int32
|
||||||
|
rows.Next()
|
||||||
err = rows.Scan(&id)
|
err = rows.Scan(&id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
|
|
@ -56,3 +56,99 @@ func TestContestRepository_DeleteContest(t *testing.T) {
|
||||||
require.NoError(t, err)
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue