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)
assert.NoError(t, err)
allowed, _ := listtask.CanCreate(doer) t.Run("normal", func(t *testing.T) {
assert.True(t, allowed) 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)
err = listtask.Create(doer) })
assert.NoError(t, err) 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))
})
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))
})
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))
})
}
// Update it func TestTask_Update(t *testing.T) {
listtask.Text = "Test34" t.Run("normal", func(t *testing.T) {
allowed, _ = listtask.CanUpdate(doer) initFixtures(t)
assert.True(t, allowed) task := &Task{
err = listtask.Update() ID: 1,
assert.NoError(t, err) 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))
})
}
// Delete the task func TestTask_Delete(t *testing.T) {
allowed, _ = listtask.CanDelete(doer) t.Run("normal", func(t *testing.T) {
assert.True(t, allowed) initFixtures(t)
err = listtask.Delete() task := &Task{
assert.NoError(t, err) ID: 1,
}
// Delete a nonexistant task err := task.Delete()
listtask.ID = 0 assert.NoError(t, err)
_, 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.True(t, IsErrTaskCannotBeEmpty(err))
// Try adding one to a nonexistant list
listtask.ListID = 99993939
listtask.Text = "Lorem Ipsum"
err = listtask.Create(doer)
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)
assert.Error(t, err)
assert.True(t, IsErrUserDoesNotExist(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)