vikunja-api/pkg/models/tasks_test.go

121 lines
3.2 KiB
Go
Raw Normal View History

// Vikunja is a todo-list application to facilitate your life.
// Copyright 2018-2019 Vikunja and contributors. All rights reserved.
2018-11-26 21:17:33 +01:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2018-11-26 21:17:33 +01:00
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
2018-11-26 21:17:33 +01:00
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-11-26 21:17:33 +01:00
2018-07-27 14:47:52 +02:00
package models
import (
"github.com/stretchr/testify/assert"
"testing"
)
2019-08-14 22:19:04 +02:00
func TestTask_Create(t *testing.T) {
2019-04-21 20:18:17 +02:00
//assert.NoError(t, LoadFixtures())
2018-07-27 14:47:52 +02:00
// TODO: This test needs refactoring
2018-08-30 08:09:17 +02:00
// Fake list task
2019-08-14 22:19:04 +02:00
listtask := Task{
2018-07-27 14:47:52 +02:00
Text: "Lorem",
Description: "Lorem Ipsum BACKERY",
ListID: 1,
}
// Add one point to a list
2018-08-30 19:14:02 +02:00
doer, err := GetUserByID(1)
2018-07-27 14:47:52 +02:00
assert.NoError(t, err)
2019-08-14 21:59:31 +02:00
allowed, _ := listtask.CanCreate(doer)
2019-03-24 13:35:50 +01:00
assert.True(t, allowed)
2018-07-27 14:47:52 +02:00
2019-08-14 21:59:31 +02:00
err = listtask.Create(doer)
2018-07-27 14:47:52 +02:00
assert.NoError(t, err)
// Update it
2018-08-30 08:09:17 +02:00
listtask.Text = "Test34"
2019-08-14 21:59:31 +02:00
allowed, _ = listtask.CanUpdate(doer)
2019-03-24 13:35:50 +01:00
assert.True(t, allowed)
2018-08-30 08:09:17 +02:00
err = listtask.Update()
2018-07-27 14:47:52 +02:00
assert.NoError(t, err)
2018-08-30 08:09:17 +02:00
// Delete the task
2019-08-14 21:59:31 +02:00
allowed, _ = listtask.CanDelete(doer)
2019-03-24 13:35:50 +01:00
assert.True(t, allowed)
2018-08-30 08:09:17 +02:00
err = listtask.Delete()
2018-07-27 14:47:52 +02:00
assert.NoError(t, err)
2018-08-30 08:09:17 +02:00
// Delete a nonexistant task
listtask.ID = 0
_, err = listtask.CanDelete(doer) // The check if the task exists happens in CanDelete
2018-07-27 14:47:52 +02:00
assert.Error(t, err)
2019-08-14 22:19:04 +02:00
assert.True(t, IsErrTaskDoesNotExist(err))
2018-07-27 14:47:52 +02:00
2018-08-30 08:09:17 +02:00
// Try adding a list task with an empty text
listtask.Text = ""
2019-08-14 21:59:31 +02:00
err = listtask.Create(doer)
2018-07-27 14:47:52 +02:00
assert.Error(t, err)
2019-08-14 22:19:04 +02:00
assert.True(t, IsErrTaskCannotBeEmpty(err))
2018-07-27 14:47:52 +02:00
// Try adding one to a nonexistant list
2018-08-30 08:09:17 +02:00
listtask.ListID = 99993939
listtask.Text = "Lorem Ipsum"
2019-08-14 21:59:31 +02:00
err = listtask.Create(doer)
2018-07-27 14:47:52 +02:00
assert.Error(t, err)
assert.True(t, IsErrListDoesNotExist(err))
2018-08-30 08:09:17 +02:00
// Try updating a nonexistant task
listtask.ID = 94829352
err = listtask.Update()
2018-07-27 14:47:52 +02:00
assert.Error(t, err)
2019-08-14 22:19:04 +02:00
assert.True(t, IsErrTaskDoesNotExist(err))
2018-07-27 14:47:52 +02:00
2018-08-30 08:09:17 +02:00
// Try inserting an task with a nonexistant user
2018-07-27 14:47:52 +02:00
nUser := &User{ID: 9482385}
2018-08-30 08:09:17 +02:00
listtask.ListID = 1
err = listtask.Create(nUser)
2018-07-27 14:47:52 +02:00
assert.Error(t, err)
assert.True(t, IsErrUserDoesNotExist(err))
}
2019-05-22 19:48:48 +02:00
func TestUpdateDone(t *testing.T) {
t.Run("marking a task as done", func(t *testing.T) {
2019-08-14 22:19:04 +02:00
oldTask := &Task{Done: false}
newTask := &Task{Done: true}
2019-05-22 19:48:48 +02:00
updateDone(oldTask, newTask)
assert.NotEqual(t, int64(0), oldTask.DoneAtUnix)
})
t.Run("unmarking a task as done", func(t *testing.T) {
2019-08-14 22:19:04 +02:00
oldTask := &Task{Done: true}
newTask := &Task{Done: false}
2019-05-22 19:48:48 +02:00
updateDone(oldTask, newTask)
assert.Equal(t, int64(0), oldTask.DoneAtUnix)
})
}
func TestTask_ReadOne(t *testing.T) {
t.Run("default", func(t *testing.T) {
task := &Task{ID: 1}
err := task.ReadOne()
assert.NoError(t, err)
assert.Equal(t, "task #1", task.Text)
})
t.Run("nonexisting", func(t *testing.T) {
task := &Task{ID: 99999}
err := task.ReadOne()
assert.Error(t, err)
assert.True(t, IsErrTaskDoesNotExist(err))
})
}