Added tests for namespaces

This commit is contained in:
kolaente 2018-07-27 15:52:50 +02:00
parent ba606515d1
commit 628f3eee64
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
5 changed files with 96 additions and 6 deletions

View file

@ -7,7 +7,7 @@ import (
func TestList_Create(t *testing.T) { func TestList_Create(t *testing.T) {
// Create test database // Create test database
assert.NoError(t, PrepareTestDatabase()) //assert.NoError(t, PrepareTestDatabase())
// Get our doer // Get our doer
doer, _, err := GetUserByID(1) doer, _, err := GetUserByID(1)

View file

@ -1,13 +1,12 @@
package models package models
import ( import (
"fmt"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing" "testing"
) )
func TestListItem_Create(t *testing.T) { func TestListItem_Create(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) //assert.NoError(t, PrepareTestDatabase())
// Fake list item // Fake list item
listitem := ListItem{ listitem := ListItem{
@ -71,6 +70,5 @@ func TestListItem_Create(t *testing.T) {
listitem.ListID = 1 listitem.ListID = 1
err = listitem.Create(nUser) err = listitem.Create(nUser)
assert.Error(t, err) assert.Error(t, err)
fmt.Println(err)
assert.True(t, IsErrUserDoesNotExist(err)) assert.True(t, IsErrUserDoesNotExist(err))
} }

View file

@ -8,7 +8,7 @@ import (
func TestList_ReadAll(t *testing.T) { func TestList_ReadAll(t *testing.T) {
// Create test database // Create test database
assert.NoError(t, PrepareTestDatabase()) //assert.NoError(t, PrepareTestDatabase())
// Get all lists for our namespace // Get all lists for our namespace
lists, err := GetListsByNamespaceID(1) lists, err := GetListsByNamespaceID(1)

92
models/namespace_test.go Normal file
View file

@ -0,0 +1,92 @@
package models
import (
"testing"
"github.com/stretchr/testify/assert"
"reflect"
)
func TestNamespace_Create(t *testing.T) {
// Create test database
//assert.NoError(t, PrepareTestDatabase())
// Dummy namespace
dummynamespace := Namespace{
Name:"Test",
Description: "Lorem Ipsum",
}
// Doer
doer, _, err := GetUserByID(1)
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))
newOne := Namespace{ID:dummynamespace.ID}
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
n := Namespace{ID:284729, Name:"Lorem"}
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)
}

View file

@ -7,7 +7,7 @@ import (
func TestCreateUser(t *testing.T) { func TestCreateUser(t *testing.T) {
// Create test database // Create test database
assert.NoError(t, PrepareTestDatabase()) //assert.NoError(t, PrepareTestDatabase())
// Get our doer // Get our doer
doer, _, err := GetUserByID(1) doer, _, err := GetUserByID(1)