Add tests for the usual crud methods
continuous-integration/drone/pr Build is failing Details

Signed-off-by: kolaente <k@knt.li>
This commit is contained in:
kolaente 2020-09-08 14:12:07 +02:00
parent 4175613164
commit e9c9bffccb
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 69 additions and 1 deletions

View File

@ -20,7 +20,10 @@ package db
import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/log"
"fmt"
"github.com/stretchr/testify/assert"
"os"
"testing"
"xorm.io/core"
"xorm.io/xorm"
)
@ -69,3 +72,19 @@ func InitTestFixtures(tablenames ...string) (err error) {
return nil
}
// AssertDBExists checks and asserts the existance of certain entries in the db
func AssertDBExists(t *testing.T, table string, values map[string]interface{}) {
v := make(map[string]interface{})
exists, err := x.Table(table).Where(values).Get(&v)
assert.NoError(t, err, fmt.Sprintf("Failed to assert entries exist in db, error was: %s", err))
assert.True(t, exists, fmt.Sprintf("Entries %v do not exist in table %s", values, table))
}
// AssertDBMissing checks and asserts the nonexistance of certain entries in the db
func AssertDBMissing(t *testing.T, table string, values map[string]interface{}) {
v := make(map[string]interface{})
exists, err := x.Table(table).Where(values).Exist(&v)
assert.NoError(t, err, fmt.Sprintf("Failed to assert entries don't exist in db, error was: %s", err))
assert.False(t, exists, fmt.Sprintf("Entries %v exist in table %s", values, table))
}

View File

@ -149,7 +149,14 @@ func (s *SavedFilter) ReadOne() error {
// @Failure 500 {object} models.Message "Internal error"
// @Router /filters/{id} [post]
func (s *SavedFilter) Update() error {
_, err := x.Where("id = ?", s.ID).Update(s)
_, err := x.
Where("id = ?", s.ID).
Cols(
"title",
"description",
"filters",
).
Update(s)
return err
}

View File

@ -17,6 +17,8 @@
package models
import (
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/user"
"github.com/stretchr/testify/assert"
"testing"
)
@ -40,6 +42,23 @@ func TestSavedFilter_getFilterIDFromListID(t *testing.T) {
}
func TestSavedFilter_Create(t *testing.T) {
db.LoadAndAssertFixtures(t)
sf := &SavedFilter{
Title: "test",
Description: "Lorem Ipsum dolor sit amet",
Filters: &TaskCollection{},
}
u := &user.User{ID: 1}
err := sf.Create(u)
assert.NoError(t, err)
assert.Equal(t, u.ID, sf.OwnerID)
db.AssertDBExists(t, "saved_filters", map[string]interface{}{
"title": "test",
"description": "Lorem Ipsum dolor sit amet",
"filters": "{\"sort_by\":null,\"order_by\":null,\"filter_by\":null,\"filter_value\":null,\"filter_comparator\":null,\"filter_concat\":\"\",\"filter_include_nulls\":false}",
"owner_id": 1,
})
}
func TestSavedFilter_ReadOne(t *testing.T) {
@ -49,7 +68,30 @@ func TestSavedFilter_ReadOne(t *testing.T) {
}
func TestSavedFilter_Update(t *testing.T) {
db.LoadAndAssertFixtures(t)
sf := &SavedFilter{
ID: 1,
Title: "NewTitle",
Description: "", // Explicitly reset the description
Filters: &TaskCollection{},
}
err := sf.Update()
assert.NoError(t, err)
db.AssertDBExists(t, "saved_filters", map[string]interface{}{
"id": 1,
"title": "NewTitle",
"description": "",
})
}
func TestSavedFilter_Delete(t *testing.T) {
db.LoadAndAssertFixtures(t)
sf := &SavedFilter{
ID: 1,
}
err := sf.Delete()
assert.NoError(t, err)
db.AssertDBMissing(t, "saved_filters", map[string]interface{}{
"id": 1,
})
}