Test: tasks pkg
This commit is contained in:
@@ -2,6 +2,7 @@ package task
|
||||
|
||||
import (
|
||||
"errors"
|
||||
testMock "github.com/stretchr/testify/mock"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
@@ -21,15 +22,46 @@ func TestRecord(t *testing.T) {
|
||||
asserts.NoError(err)
|
||||
}
|
||||
|
||||
type taskPoolMock struct {
|
||||
testMock.Mock
|
||||
}
|
||||
|
||||
func (t taskPoolMock) Add(num int) {
|
||||
t.Called(num)
|
||||
}
|
||||
|
||||
func (t taskPoolMock) Submit(job Job) {
|
||||
t.Called(job)
|
||||
}
|
||||
|
||||
func TestResume(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
mockPool := taskPoolMock{}
|
||||
|
||||
// 没有任务
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(Queued).WillReturnRows(sqlmock.NewRows([]string{"type"}))
|
||||
Resume()
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(Queued, Processing).WillReturnRows(sqlmock.NewRows([]string{"type"}))
|
||||
Resume(mockPool)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
// 有任务, 类型未知
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(Queued, Processing).WillReturnRows(sqlmock.NewRows([]string{"type"}).AddRow(233))
|
||||
Resume(mockPool)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
// 有任务
|
||||
{
|
||||
mockPool.On("Submit", testMock.Anything)
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(Queued, Processing).WillReturnRows(sqlmock.NewRows([]string{"type", "props"}).AddRow(CompressTaskType, "{}"))
|
||||
mock.ExpectQuery("SELECT(.+)users").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
mock.ExpectQuery("SELECT(.+)policies").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
Resume(mockPool)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
mockPool.AssertExpectations(t)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetJobFromModel(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user