2018-07-27 14:47:52 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-08-30 08:09:17 +02:00
|
|
|
func TestListTask_Create(t *testing.T) {
|
2018-07-27 15:52:50 +02:00
|
|
|
//assert.NoError(t, PrepareTestDatabase())
|
2018-07-27 14:47:52 +02:00
|
|
|
|
2018-08-30 08:09:17 +02:00
|
|
|
// Fake list task
|
|
|
|
listtask := ListTask{
|
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)
|
|
|
|
|
2018-08-30 08:09:17 +02:00
|
|
|
assert.True(t, listtask.CanCreate(&doer))
|
2018-07-27 14:47:52 +02:00
|
|
|
|
2018-08-30 08:09:17 +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"
|
|
|
|
assert.True(t, listtask.CanUpdate(&doer))
|
|
|
|
err = listtask.Update()
|
2018-07-27 14:47:52 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Check if it was updated
|
2018-08-30 08:09:17 +02:00
|
|
|
li, err := GetListTaskByID(listtask.ID)
|
2018-07-27 14:47:52 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, li.Text, "Test34")
|
|
|
|
|
2018-08-30 08:09:17 +02:00
|
|
|
// Delete the task
|
|
|
|
assert.True(t, listtask.CanDelete(&doer))
|
|
|
|
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.Delete()
|
2018-07-27 14:47:52 +02:00
|
|
|
assert.Error(t, err)
|
2018-08-30 08:09:17 +02:00
|
|
|
assert.True(t, IsErrListTaskDoesNotExist(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 = ""
|
|
|
|
err = listtask.Create(&doer)
|
2018-07-27 14:47:52 +02:00
|
|
|
assert.Error(t, err)
|
2018-08-30 08:09:17 +02:00
|
|
|
assert.True(t, IsErrListTaskCannotBeEmpty(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"
|
|
|
|
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)
|
2018-08-30 08:09:17 +02:00
|
|
|
assert.True(t, IsErrListTaskDoesNotExist(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))
|
|
|
|
}
|