2020-04-19 09:27:28 +02:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2020-04-19 09:27:28 +02:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2020-04-19 09:27:28 +02:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// 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
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2020-04-19 09:27:28 +02:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2020-04-19 09:27:28 +02:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2020-10-11 22:10:03 +02:00
|
|
|
"testing"
|
2021-01-31 13:51:23 +01:00
|
|
|
|
2021-01-31 12:40:02 +01:00
|
|
|
"xorm.io/xorm"
|
2020-10-11 22:10:03 +02:00
|
|
|
|
2020-04-19 09:27:28 +02:00
|
|
|
"code.vikunja.io/api/pkg/db"
|
|
|
|
"code.vikunja.io/api/pkg/user"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBucket_ReadAll(t *testing.T) {
|
2020-12-22 12:38:05 +01:00
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
2020-12-22 12:38:05 +01:00
|
|
|
|
|
|
|
testuser := &user.User{ID: 1}
|
|
|
|
b := &Bucket{ListID: 1}
|
2020-12-23 16:32:28 +01:00
|
|
|
bucketsInterface, _, _, err := b.ReadAll(s, testuser, "", 0, 0)
|
2020-12-22 12:38:05 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
buckets, is := bucketsInterface.([]*Bucket)
|
|
|
|
assert.True(t, is)
|
2020-04-19 09:27:28 +02:00
|
|
|
|
2020-12-22 12:38:05 +01:00
|
|
|
// Assert that we have a user for each bucket
|
|
|
|
assert.Equal(t, testuser.ID, buckets[0].CreatedBy.ID)
|
|
|
|
assert.Equal(t, testuser.ID, buckets[1].CreatedBy.ID)
|
|
|
|
assert.Equal(t, testuser.ID, buckets[2].CreatedBy.ID)
|
2020-04-19 09:27:28 +02:00
|
|
|
|
2020-12-22 12:38:05 +01:00
|
|
|
// Assert our three test buckets
|
|
|
|
assert.Len(t, buckets, 3)
|
2020-04-19 09:27:28 +02:00
|
|
|
|
2020-12-22 12:38:05 +01:00
|
|
|
// Assert all tasks are in the right bucket
|
|
|
|
assert.Len(t, buckets[0].Tasks, 12)
|
|
|
|
assert.Len(t, buckets[1].Tasks, 3)
|
|
|
|
assert.Len(t, buckets[2].Tasks, 3)
|
2020-04-19 09:27:28 +02:00
|
|
|
|
2020-12-22 12:38:05 +01:00
|
|
|
// Assert we have bucket 0, 1, 2, 3 but not 4 (that belongs to a different list)
|
|
|
|
assert.Equal(t, int64(1), buckets[0].ID)
|
|
|
|
assert.Equal(t, int64(2), buckets[1].ID)
|
|
|
|
assert.Equal(t, int64(3), buckets[2].ID)
|
2020-04-19 09:27:28 +02:00
|
|
|
|
2020-12-22 12:38:05 +01:00
|
|
|
// Kinda assert all tasks are in the right buckets
|
|
|
|
assert.Equal(t, int64(1), buckets[0].Tasks[0].BucketID)
|
|
|
|
assert.Equal(t, int64(1), buckets[0].Tasks[1].BucketID)
|
|
|
|
assert.Equal(t, int64(2), buckets[1].Tasks[0].BucketID)
|
|
|
|
assert.Equal(t, int64(2), buckets[1].Tasks[1].BucketID)
|
|
|
|
assert.Equal(t, int64(2), buckets[1].Tasks[2].BucketID)
|
|
|
|
assert.Equal(t, int64(3), buckets[2].Tasks[0].BucketID)
|
|
|
|
assert.Equal(t, int64(3), buckets[2].Tasks[1].BucketID)
|
|
|
|
assert.Equal(t, int64(3), buckets[2].Tasks[2].BucketID)
|
|
|
|
})
|
|
|
|
t.Run("filtered", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
2020-04-19 09:27:28 +02:00
|
|
|
|
2020-12-22 12:38:05 +01:00
|
|
|
testuser := &user.User{ID: 1}
|
|
|
|
b := &Bucket{
|
|
|
|
ListID: 1,
|
|
|
|
TaskCollection: TaskCollection{
|
|
|
|
FilterBy: []string{"title"},
|
|
|
|
FilterComparator: []string{"like"},
|
|
|
|
FilterValue: []string{"done"},
|
|
|
|
},
|
|
|
|
}
|
2021-03-10 11:59:10 +01:00
|
|
|
bucketsInterface, _, _, err := b.ReadAll(s, testuser, "", -1, 0)
|
2020-12-22 12:38:05 +01:00
|
|
|
assert.NoError(t, err)
|
2020-04-19 09:27:28 +02:00
|
|
|
|
2020-12-22 12:38:05 +01:00
|
|
|
buckets := bucketsInterface.([]*Bucket)
|
|
|
|
assert.Len(t, buckets, 3)
|
|
|
|
assert.Equal(t, int64(2), buckets[0].Tasks[0].ID)
|
|
|
|
})
|
2020-04-25 22:32:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBucket_Delete(t *testing.T) {
|
2021-02-02 23:48:37 +01:00
|
|
|
user := &user.User{ID: 1}
|
|
|
|
|
2020-04-25 22:32:02 +02:00
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-04-25 22:32:02 +02:00
|
|
|
b := &Bucket{
|
|
|
|
ID: 2, // The second bucket only has 3 tasks
|
|
|
|
ListID: 1,
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := b.Delete(s, user)
|
2020-12-23 16:32:28 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
2020-04-25 22:32:02 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Assert all tasks have been moved to bucket 1 as that one is the first
|
|
|
|
tasks := []*Task{}
|
2020-12-23 16:32:28 +01:00
|
|
|
err = s.Where("bucket_id = ?", 1).Find(&tasks)
|
2020-04-25 22:32:02 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, tasks, 15)
|
2020-09-27 17:45:17 +02:00
|
|
|
db.AssertMissing(t, "buckets", map[string]interface{}{
|
|
|
|
"id": 2,
|
|
|
|
"list_id": 1,
|
|
|
|
})
|
2020-04-25 22:32:02 +02:00
|
|
|
})
|
|
|
|
t.Run("last bucket in list", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-04-25 22:32:02 +02:00
|
|
|
b := &Bucket{
|
|
|
|
ID: 34,
|
|
|
|
ListID: 18,
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := b.Delete(s, user)
|
2020-04-25 22:32:02 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrCannotRemoveLastBucket(err))
|
2020-12-23 16:32:28 +01:00
|
|
|
err = s.Commit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-09-27 17:45:17 +02:00
|
|
|
db.AssertExists(t, "buckets", map[string]interface{}{
|
|
|
|
"id": 34,
|
|
|
|
"list_id": 18,
|
|
|
|
}, false)
|
2020-04-25 22:32:02 +02:00
|
|
|
})
|
2020-04-19 09:27:28 +02:00
|
|
|
}
|
2021-01-31 12:40:02 +01:00
|
|
|
|
|
|
|
func TestBucket_Update(t *testing.T) {
|
|
|
|
|
|
|
|
testAndAssertBucketUpdate := func(t *testing.T, b *Bucket, s *xorm.Session) {
|
2021-02-02 23:48:37 +01:00
|
|
|
err := b.Update(s, &user.User{ID: 1})
|
2021-01-31 12:40:02 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = s.Commit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
db.AssertExists(t, "buckets", map[string]interface{}{
|
|
|
|
"id": 1,
|
|
|
|
"title": b.Title,
|
|
|
|
"limit": b.Limit,
|
|
|
|
}, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
b := &Bucket{
|
|
|
|
ID: 1,
|
|
|
|
Title: "New Name",
|
|
|
|
Limit: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
testAndAssertBucketUpdate(t, b, s)
|
|
|
|
})
|
|
|
|
t.Run("reset limit", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
b := &Bucket{
|
|
|
|
ID: 1,
|
|
|
|
Title: "testbucket1",
|
|
|
|
Limit: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
testAndAssertBucketUpdate(t, b, s)
|
|
|
|
})
|
|
|
|
}
|