59 lines
1.4 KiB
Go
59 lines
1.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)
|
|
})
|
|
}
|