Fix new tasks not getting a new task index (#116)

This commit is contained in:
konrad 2019-12-08 15:10:34 +00:00
parent 720df3cbed
commit 7cdc997191
2 changed files with 99 additions and 59 deletions

View file

@ -497,6 +497,14 @@ func (t *Task) Create(a web.Auth) (err error) {
t.UID = utils.MakeRandomString(40)
}
// Get the index for this task
latestTask := &Task{}
_, err = x.Where("list_id = ?", t.ListID).OrderBy("id desc").Get(latestTask)
if err != nil {
return err
}
t.Index = latestTask.Index + 1
t.CreatedByID = u.ID
t.CreatedBy = u
if _, err = x.Insert(t); err != nil {

View file

@ -22,71 +22,101 @@ import (
)
func TestTask_Create(t *testing.T) {
//assert.NoError(t, LoadFixtures())
// TODO: This test needs refactoring
// Fake list task
listtask := Task{
Text: "Lorem",
Description: "Lorem Ipsum BACKERY",
ListID: 1,
user := &User{
ID: 1,
Username: "user1",
Email: "user1@example.com",
}
// Add one point to a list
doer, err := GetUserByID(1)
// We only test creating a task here, the rights are all well tested in the integration tests.
t.Run("normal", func(t *testing.T) {
initFixtures(t)
task := &Task{
Text: "Lorem",
Description: "Lorem Ipsum Dolor",
ListID: 1,
}
err := task.Create(user)
assert.NoError(t, err)
// Assert getting a uid
assert.NotEmpty(t, task.UID)
// Assert getting a new index
assert.NotEmpty(t, task.Index)
assert.Equal(t, int64(18), task.Index)
allowed, _ := listtask.CanCreate(doer)
assert.True(t, allowed)
err = listtask.Create(doer)
assert.NoError(t, err)
// Update it
listtask.Text = "Test34"
allowed, _ = listtask.CanUpdate(doer)
assert.True(t, allowed)
err = listtask.Update()
assert.NoError(t, err)
// Delete the task
allowed, _ = listtask.CanDelete(doer)
assert.True(t, allowed)
err = listtask.Delete()
assert.NoError(t, err)
// Delete a nonexistant task
listtask.ID = 0
_, err = listtask.CanDelete(doer) // The check if the task exists happens in CanDelete
assert.Error(t, err)
assert.True(t, IsErrTaskDoesNotExist(err))
// Try adding a list task with an empty text
listtask.Text = ""
err = listtask.Create(doer)
})
t.Run("empty text", func(t *testing.T) {
initFixtures(t)
task := &Task{
Text: "",
Description: "Lorem Ipsum Dolor",
ListID: 1,
}
err := task.Create(user)
assert.Error(t, err)
assert.True(t, IsErrTaskCannotBeEmpty(err))
// Try adding one to a nonexistant list
listtask.ListID = 99993939
listtask.Text = "Lorem Ipsum"
err = listtask.Create(doer)
})
t.Run("nonexistant list", func(t *testing.T) {
initFixtures(t)
task := &Task{
Text: "Test",
Description: "Lorem Ipsum Dolor",
ListID: 9999999,
}
err := task.Create(user)
assert.Error(t, err)
assert.True(t, IsErrListDoesNotExist(err))
// Try updating a nonexistant task
listtask.ID = 94829352
err = listtask.Update()
assert.Error(t, err)
assert.True(t, IsErrTaskDoesNotExist(err))
// Try inserting an task with a nonexistant user
nUser := &User{ID: 9482385}
listtask.ListID = 1
err = listtask.Create(nUser)
})
t.Run("noneixtant user", func(t *testing.T) {
initFixtures(t)
nUser := &User{ID: 99999999}
task := &Task{
Text: "Test",
Description: "Lorem Ipsum Dolor",
ListID: 1,
}
err := task.Create(nUser)
assert.Error(t, err)
assert.True(t, IsErrUserDoesNotExist(err))
})
}
func TestTask_Update(t *testing.T) {
t.Run("normal", func(t *testing.T) {
initFixtures(t)
task := &Task{
ID: 1,
Text: "test10000",
Description: "Lorem Ipsum Dolor",
ListID: 1,
}
err := task.Update()
assert.NoError(t, err)
})
t.Run("nonexistant task", func(t *testing.T) {
initFixtures(t)
task := &Task{
ID: 9999999,
Text: "test10000",
Description: "Lorem Ipsum Dolor",
ListID: 1,
}
err := task.Update()
assert.Error(t, err)
assert.True(t, IsErrTaskDoesNotExist(err))
})
}
func TestTask_Delete(t *testing.T) {
t.Run("normal", func(t *testing.T) {
initFixtures(t)
task := &Task{
ID: 1,
}
err := task.Delete()
assert.NoError(t, err)
})
}
func TestUpdateDone(t *testing.T) {
@ -106,12 +136,14 @@ func TestUpdateDone(t *testing.T) {
func TestTask_ReadOne(t *testing.T) {
t.Run("default", func(t *testing.T) {
initFixtures(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) {
initFixtures(t)
task := &Task{ID: 99999}
err := task.ReadOne()
assert.Error(t, err)