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) 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.CreatedByID = u.ID
t.CreatedBy = u t.CreatedBy = u
if _, err = x.Insert(t); err != nil { if _, err = x.Insert(t); err != nil {

View file

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