vikunja-api/models/namespace_test.go

93 lines
2.3 KiB
Go
Raw Normal View History

2018-07-27 15:52:50 +02:00
package models
import (
"github.com/stretchr/testify/assert"
"reflect"
2018-07-27 15:53:01 +02:00
"testing"
2018-07-27 15:52:50 +02:00
)
func TestNamespace_Create(t *testing.T) {
// Create test database
//assert.NoError(t, PrepareTestDatabase())
// Dummy namespace
dummynamespace := Namespace{
2018-07-27 15:53:01 +02:00
Name: "Test",
2018-07-27 15:52:50 +02:00
Description: "Lorem Ipsum",
}
// Doer
2018-08-30 19:14:02 +02:00
doer, err := GetUserByID(1)
2018-07-27 15:52:50 +02:00
assert.NoError(t, err)
// Try creating it
assert.True(t, dummynamespace.CanCreate(&doer))
err = dummynamespace.Create(&doer)
assert.NoError(t, err)
// check if it really exists
assert.True(t, dummynamespace.CanRead(&doer))
2018-07-27 15:53:01 +02:00
newOne := Namespace{ID: dummynamespace.ID}
2018-07-27 15:52:50 +02:00
err = newOne.ReadOne()
assert.NoError(t, err)
assert.Equal(t, newOne.Name, "Test")
// Try creating one without a name
n2 := Namespace{}
err = n2.Create(&doer)
assert.Error(t, err)
assert.True(t, IsErrNamespaceNameCannotBeEmpty(err))
// Try inserting one with a nonexistant user
nUser := &User{ID: 9482385}
err = dummynamespace.Create(nUser)
assert.Error(t, err)
assert.True(t, IsErrUserDoesNotExist(err))
// Update it
assert.True(t, dummynamespace.CanUpdate(&doer))
dummynamespace.Description = "Dolor sit amet."
err = dummynamespace.Update()
assert.NoError(t, err)
// Try updating one with a nonexistant owner
dummynamespace.OwnerID = 94829838572
err = dummynamespace.Update()
assert.Error(t, err)
assert.True(t, IsErrUserDoesNotExist(err))
// Try updating without a name
dummynamespace.Name = ""
err = dummynamespace.Update()
assert.Error(t, err)
assert.True(t, IsErrNamespaceNameCannotBeEmpty(err))
// Try updating a nonexistant one
2018-07-27 15:53:01 +02:00
n := Namespace{ID: 284729, Name: "Lorem"}
2018-07-27 15:52:50 +02:00
err = n.Update()
assert.Error(t, err)
assert.True(t, IsErrNamespaceDoesNotExist(err))
// Delete it
assert.True(t, dummynamespace.CanDelete(&doer))
err = dummynamespace.Delete()
assert.NoError(t, err)
// Try deleting a nonexistant one
err = n.Delete()
assert.Error(t, err)
assert.True(t, IsErrNamespaceDoesNotExist(err))
// Check if it was successfully deleted
err = dummynamespace.ReadOne()
assert.Error(t, err)
assert.True(t, IsErrNamespaceDoesNotExist(err))
// Get all namespaces of a user
nsps, err := n.ReadAll(&doer)
assert.NoError(t, err)
assert.Equal(t, reflect.TypeOf(nsps).Kind(), reflect.Slice)
s := reflect.ValueOf(nsps)
assert.Equal(t, s.Len(), 2)
}