155 lines
4 KiB
Go
155 lines
4 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
"github.com/jmoiron/sqlx"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uber.org/zap"
|
|
"testing"
|
|
)
|
|
|
|
func TestContestRepository_CreateContest(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 contest creation", func(t *testing.T) {
|
|
title := "Contest title"
|
|
|
|
rows := sqlmock.NewRows([]string{"id"}).AddRow(1)
|
|
|
|
mock.ExpectQuery(sqlxDB.Rebind(createContestQuery)).WithArgs(title).WillReturnRows(rows)
|
|
|
|
id, err := contestRepo.CreateContest(context.Background(), title)
|
|
require.NoError(t, err)
|
|
require.Equal(t, int32(1), id)
|
|
})
|
|
}
|
|
|
|
func TestContestRepository_DeleteContest(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 contest deletion", func(t *testing.T) {
|
|
id := int32(1)
|
|
rows := sqlmock.NewResult(1, 1)
|
|
|
|
mock.ExpectExec(sqlxDB.Rebind(deleteContestQuery)).WithArgs(id).WillReturnResult(rows)
|
|
|
|
err = contestRepo.DeleteContest(context.Background(), id)
|
|
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)
|
|
})
|
|
}
|