diff --git a/pkg/models/list_create_test.go b/pkg/models/list_create_test.go deleted file mode 100644 index 230265911..000000000 --- a/pkg/models/list_create_test.go +++ /dev/null @@ -1,97 +0,0 @@ -// Vikunja is a todo-list application to facilitate your life. -// Copyright 2018-2019 Vikunja and contributors. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// 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 -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -package models - -import ( - "github.com/stretchr/testify/assert" - "testing" -) - -func TestList_Create(t *testing.T) { - // Create test database - //assert.NoError(t, LoadFixtures()) - - // Get our doer - doer, err := GetUserByID(1) - assert.NoError(t, err) - - // Dummy list for testing - dummylist := List{ - Title: "test", - Description: "Lorem Ipsum", - NamespaceID: 1, - } - - // Check if the user can create - allowed, _ := dummylist.CanCreate(doer) - assert.True(t, allowed) - - // Create it - err = dummylist.Create(doer) - assert.NoError(t, err) - - // Get the list - newdummy := List{ID: dummylist.ID} - canRead, err := newdummy.CanRead(doer) - assert.NoError(t, err) - assert.True(t, canRead) - err = newdummy.ReadOne() - assert.NoError(t, err) - assert.Equal(t, dummylist.Title, newdummy.Title) - assert.Equal(t, dummylist.Description, newdummy.Description) - assert.Equal(t, dummylist.OwnerID, doer.ID) - - // Check if the user can see it - allowed, _ = dummylist.CanRead(doer) - assert.True(t, allowed) - - // Try updating a list - allowed, _ = dummylist.CanUpdate(doer) - assert.True(t, allowed) - dummylist.Description = "Lorem Ipsum dolor sit amet." - err = dummylist.Update() - assert.NoError(t, err) - - // Delete it - allowed, _ = dummylist.CanDelete(doer) - assert.True(t, allowed) - - err = dummylist.Delete() - assert.NoError(t, err) - - // Try updating a nonexistant list - err = dummylist.Update() - assert.Error(t, err) - assert.True(t, IsErrListDoesNotExist(err)) - - // Check creation with a nonexistant namespace - list3 := List{ - Title: "test", - Description: "Lorem Ipsum", - NamespaceID: 876694, - } - - err = list3.Create(doer) - assert.Error(t, err) - assert.True(t, IsErrNamespaceDoesNotExist(err)) - - // Try creating with a nonexistant owner - nUser := &User{ID: 9482385} - err = dummylist.Create(nUser) - assert.Error(t, err) - assert.True(t, IsErrUserDoesNotExist(err)) -} diff --git a/pkg/models/list_read_test.go b/pkg/models/list_read_test.go deleted file mode 100644 index 359a52684..000000000 --- a/pkg/models/list_read_test.go +++ /dev/null @@ -1,50 +0,0 @@ -// Vikunja is a todo-list application to facilitate your life. -// Copyright 2018-2019 Vikunja and contributors. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// 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 -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -package models - -import ( - "github.com/stretchr/testify/assert" - "reflect" - "testing" -) - -func TestList_ReadAll(t *testing.T) { - // Create test database - //assert.NoError(t, LoadFixtures()) - - // Get all lists for our namespace - lists, err := GetListsByNamespaceID(1, &User{}) - assert.NoError(t, err) - assert.Equal(t, len(lists), 2) - - // Get all lists our user has access to - u, err := GetUserByID(1) - assert.NoError(t, err) - - lists2 := List{} - lists3, _, _, err := lists2.ReadAll(u, "", 1, 50) - - assert.NoError(t, err) - assert.Equal(t, reflect.TypeOf(lists3).Kind(), reflect.Slice) - s := reflect.ValueOf(lists3) - assert.Equal(t, 16, s.Len()) - - // Try getting lists for a nonexistant user - _, _, _, err = lists2.ReadAll(&User{ID: 984234}, "", 1, 50) - assert.Error(t, err) - assert.True(t, IsErrUserDoesNotExist(err)) -} diff --git a/pkg/models/list_test.go b/pkg/models/list_test.go new file mode 100644 index 000000000..dd6cdef32 --- /dev/null +++ b/pkg/models/list_test.go @@ -0,0 +1,158 @@ +// Vikunja is a todo-list application to facilitate your life. +// Copyright 2018-2019 Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// 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 +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package models + +import ( + "github.com/stretchr/testify/assert" + "reflect" + "testing" +) + +func TestList_CreateOrUpdate(t *testing.T) { + user := &User{ + ID: 1, + Username: "user1", + Email: "user1@example.com", + } + + t.Run("create", func(t *testing.T) { + t.Run("normal", func(t *testing.T) { + initFixtures(t) + list := List{ + Title: "test", + Description: "Lorem Ipsum", + NamespaceID: 1, + } + err := list.Create(user) + assert.NoError(t, err) + }) + t.Run("nonexistant namespace", func(t *testing.T) { + initFixtures(t) + list := List{ + Title: "test", + Description: "Lorem Ipsum", + NamespaceID: 999999, + } + + err := list.Create(user) + assert.Error(t, err) + assert.True(t, IsErrNamespaceDoesNotExist(err)) + }) + t.Run("nonexistant owner", func(t *testing.T) { + initFixtures(t) + user := &User{ID: 9482385} + list := List{ + Title: "test", + Description: "Lorem Ipsum", + NamespaceID: 1, + } + err := list.Create(user) + assert.Error(t, err) + assert.True(t, IsErrUserDoesNotExist(err)) + }) + t.Run("existing identifier", func(t *testing.T) { + initFixtures(t) + list := List{ + Title: "test", + Description: "Lorem Ipsum", + Identifier: "test", + NamespaceID: 1, + } + + err := list.Create(user) + assert.Error(t, err) + assert.True(t, IsErrListIdentifierIsNotUnique(err)) + }) + }) + + t.Run("update", func(t *testing.T) { + t.Run("normal", func(t *testing.T) { + initFixtures(t) + list := List{ + ID: 1, + Title: "test", + Description: "Lorem Ipsum", + NamespaceID: 1, + } + list.Description = "Lorem Ipsum dolor sit amet." + err := list.Update() + assert.NoError(t, err) + + }) + t.Run("nonexistant", func(t *testing.T) { + initFixtures(t) + list := List{ + ID: 99999999, + Title: "test", + } + err := list.Update() + assert.Error(t, err) + assert.True(t, IsErrListDoesNotExist(err)) + + }) + t.Run("existing identifier", func(t *testing.T) { + initFixtures(t) + list := List{ + Title: "test", + Description: "Lorem Ipsum", + Identifier: "test", + NamespaceID: 1, + } + + err := list.Create(user) + assert.Error(t, err) + assert.True(t, IsErrListIdentifierIsNotUnique(err)) + }) + }) +} + +func TestList_Delete(t *testing.T) { + initFixtures(t) + list := List{ + ID: 1, + } + err := list.Delete() + assert.NoError(t, err) +} + +func TestList_ReadAll(t *testing.T) { + initFixtures(t) + // Create test database + //assert.NoError(t, LoadFixtures()) + + // Get all lists for our namespace + lists, err := GetListsByNamespaceID(1, &User{}) + assert.NoError(t, err) + assert.Equal(t, len(lists), 2) + + // Get all lists our user has access to + u, err := GetUserByID(1) + assert.NoError(t, err) + + lists2 := List{} + lists3, _, _, err := lists2.ReadAll(u, "", 1, 50) + + assert.NoError(t, err) + assert.Equal(t, reflect.TypeOf(lists3).Kind(), reflect.Slice) + s := reflect.ValueOf(lists3) + assert.Equal(t, 16, s.Len()) + + // Try getting lists for a nonexistant user + _, _, _, err = lists2.ReadAll(&User{ID: 984234}, "", 1, 50) + assert.Error(t, err) + assert.True(t, IsErrUserDoesNotExist(err)) +} diff --git a/pkg/models/unit_tests.go b/pkg/models/unit_tests.go index 2fcb5345a..72c96f494 100644 --- a/pkg/models/unit_tests.go +++ b/pkg/models/unit_tests.go @@ -24,9 +24,11 @@ import ( "code.vikunja.io/api/pkg/mail" "fmt" "github.com/go-xorm/xorm" + "github.com/stretchr/testify/assert" "gopkg.in/testfixtures.v2" "os" "path/filepath" + "testing" ) // SetupTests takes care of seting up the db, fixtures etc. @@ -84,3 +86,9 @@ func createTestEngine(fixturesDir string) error { func initSchema(tx *xorm.Engine) error { return tx.Sync2(GetTables()...) } + +func initFixtures(t *testing.T) { + // Init db fixtures + err := db.LoadFixtures() + assert.NoError(t, err) +}