Added tests for listitems

This commit is contained in:
kolaente 2018-07-27 14:47:52 +02:00
parent 2c12797111
commit f7bc88c825
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
6 changed files with 92 additions and 2 deletions

3
.gitignore vendored
View file

@ -4,4 +4,5 @@ config.ini
*.db
Run
vikunja
dist/
dist/
cover.*

View file

@ -183,6 +183,7 @@ doch auch in einer Funktion machbar sein.
* [ ] Validation der ankommenden structs
* [ ] Viper für config einbauen und ini rauswerfen
* [ ] Docs für installationsanleitung
* [ ] Tests für Rechtekram
* [ ] "Apiformat" Methoden, damit in der Ausgabe zb kein Passwort drin ist..., oder created/updated von Nutzern... oder ownerID nicht drin ist sondern nur das ownerobject
* [x] Rechte überprüfen:

View file

@ -54,7 +54,7 @@ clean:
.PHONY: test
test:
go test -cover $(PACKAGES)
go test -cover -coverprofile cover.out $(PACKAGES)
required-gofmt-version:
@go version | grep -q '\(1.7\|1.8\|1.9\|1.10\)' || { echo "We require go version 1.7, 1.8, 1.9 or 1.10 to format code" >&2 && exit 1; }

6
models/fixtures/list.yml Normal file
View file

@ -0,0 +1,6 @@
-
id: 1
title: Test1
description: Lorem Ipsum
owner_id: 1
namespace_id: 1

View file

@ -27,6 +27,12 @@ func createOrUpdateListItem(i *ListItem, doer *User) (err error) {
return ErrListItemCannotBeEmpty{}
}
// Check if the list exists
_, err = GetListByID(i.ListID)
if err != nil {
return
}
// Do the update
if i.ID != 0 {
_, err = x.ID(i.ID).Update(i)

76
models/list_items_test.go Normal file
View file

@ -0,0 +1,76 @@
package models
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestListItem_Create(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
// Fake list item
listitem := ListItem{
Text: "Lorem",
Description: "Lorem Ipsum BACKERY",
ListID: 1,
}
// Add one point to a list
doer, _, err := GetUserByID(1)
assert.NoError(t, err)
assert.True(t, listitem.CanCreate(&doer))
err = listitem.Create(&doer)
assert.NoError(t, err)
// Update it
listitem.Text = "Test34"
assert.True(t, listitem.CanUpdate(&doer))
err = listitem.Update()
assert.NoError(t, err)
// Check if it was updated
li, err := GetListItemByID(listitem.ID)
assert.NoError(t, err)
assert.Equal(t, li.Text, "Test34")
// Delete the item
assert.True(t, listitem.CanDelete(&doer))
err = listitem.Delete()
assert.NoError(t, err)
// Delete a nonexistant item
listitem.ID = 0
err = listitem.Delete()
assert.Error(t, err)
assert.True(t, IsErrListItemDoesNotExist(err))
// Try adding a list item with an empty text
listitem.Text = ""
err = listitem.Create(&doer)
assert.Error(t, err)
assert.True(t, IsErrListItemCannotBeEmpty(err))
// Try adding one to a nonexistant list
listitem.ListID = 99993939
listitem.Text = "Lorem Ipsum"
err = listitem.Create(&doer)
assert.Error(t, err)
assert.True(t, IsErrListDoesNotExist(err))
// Try updating a nonexistant item
listitem.ID = 94829352
err = listitem.Update()
assert.Error(t, err)
assert.True(t, IsErrListItemDoesNotExist(err))
// Try inserting an item with a nonexistant user
nUser := &User{ID: 9482385}
listitem.ListID = 1
err = listitem.Create(nUser)
assert.Error(t, err)
fmt.Println(err)
assert.True(t, IsErrUserDoesNotExist(err))
}