fixed tests
This commit is contained in:
parent
5caaae4ee2
commit
92a0b7997b
|
@ -196,7 +196,7 @@ func InitDefaultConfig() {
|
|||
RateLimitPeriod.setDefault(60)
|
||||
RateLimitStore.setDefault("memory")
|
||||
// Files
|
||||
FilesBasePath.setDefault("./files")
|
||||
FilesBasePath.setDefault("files")
|
||||
FilesMaxSize.setDefault(21474836480) // 20 MB
|
||||
}
|
||||
|
||||
|
|
|
@ -49,9 +49,13 @@ func initFixtures(t *testing.T) {
|
|||
err := db.LoadFixtures()
|
||||
assert.NoError(t, err)
|
||||
|
||||
InitTestFileFixtures(t)
|
||||
}
|
||||
|
||||
func InitTestFileFixtures(t *testing.T) {
|
||||
// Init fixture files
|
||||
filename := config.FilesBasePath.GetString() + "/1"
|
||||
err = afero.WriteFile(afs, filename, []byte("testfile1"), 0644)
|
||||
err := afero.WriteFile(afs, filename, []byte("testfile1"), 0644)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
|
@ -69,7 +73,8 @@ func InitTests() {
|
|||
}
|
||||
|
||||
config.InitDefaultConfig()
|
||||
// set r
|
||||
// We need to set the root path even if we're not using the config, otherwise fixtures are not loaded correctly
|
||||
config.ServiceRootpath.Set(os.Getenv("VIKUNJA_SERVICE_ROOTPATH"))
|
||||
|
||||
// Sync fixtures
|
||||
var fixturesHelper testfixtures.Helper = &testfixtures.SQLite{}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
- id: 1
|
||||
name: test
|
||||
size: 100
|
||||
inserted_unix: 0
|
||||
created_unix: 1570998791
|
||||
created_by_id: 1
|
||||
|
|
|
@ -31,6 +31,7 @@ import (
|
|||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -76,6 +77,8 @@ var (
|
|||
|
||||
func setupTestEnv() (e *echo.Echo, err error) {
|
||||
config.InitDefaultConfig()
|
||||
// We need to set the root path even if we're not using the config, otherwise fixtures are not loaded correctly
|
||||
config.ServiceRootpath.Set(os.Getenv("VIKUNJA_SERVICE_ROOTPATH"))
|
||||
// Some tests use the file engine, so we'll need to initialize that
|
||||
files.InitTests()
|
||||
models.SetupTests(config.ServiceRootpath.GetString())
|
||||
|
|
|
@ -27,6 +27,8 @@ func TestMain(m *testing.M) {
|
|||
|
||||
// Set default config
|
||||
config.InitDefaultConfig()
|
||||
// We need to set the root path even if we're not using the config, otherwise fixtures are not loaded correctly
|
||||
config.ServiceRootpath.Set(os.Getenv("VIKUNJA_SERVICE_ROOTPATH"))
|
||||
|
||||
// Some tests use the file engine, so we'll need to initialize that
|
||||
files.InitTests()
|
||||
|
|
|
@ -20,7 +20,6 @@ import (
|
|||
"code.vikunja.io/api/pkg/files"
|
||||
"code.vikunja.io/web"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -108,7 +107,7 @@ func (ta *TaskAttachment) ReadAll(s string, a web.Auth, page int) (interface{},
|
|||
fileIDs := make([]int64, 0, len(attachments))
|
||||
userIDs := make([]int64, 0, len(attachments))
|
||||
for _, r := range attachments {
|
||||
fileIDs = append(fileIDs, r.ID)
|
||||
fileIDs = append(fileIDs, r.FileID)
|
||||
userIDs = append(userIDs, r.CreatedByID)
|
||||
}
|
||||
|
||||
|
@ -125,6 +124,10 @@ func (ta *TaskAttachment) ReadAll(s string, a web.Auth, page int) (interface{},
|
|||
}
|
||||
|
||||
for _, r := range attachments {
|
||||
// If the actual file does not exist, don't try to load it as that would fail with nil panic
|
||||
if _, exists := fs[r.FileID]; !exists {
|
||||
continue
|
||||
}
|
||||
r.File = fs[r.FileID]
|
||||
r.File.Created = time.Unix(r.File.CreatedUnix, 0)
|
||||
r.CreatedBy = us[r.CreatedByID]
|
||||
|
@ -150,7 +153,7 @@ func (ta *TaskAttachment) Delete() error {
|
|||
// Delete the underlying file
|
||||
err = ta.File.Delete()
|
||||
// If the file does not exist, we don't want to error out
|
||||
if err != nil && os.IsNotExist(err) {
|
||||
if err != nil && files.IsErrFileDoesNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
|
|
|
@ -28,6 +28,7 @@ import (
|
|||
|
||||
func TestTaskAttachment_ReadOne(t *testing.T) {
|
||||
t.Run("Normal File", func(t *testing.T) {
|
||||
files.InitTestFileFixtures(t)
|
||||
ta := &TaskAttachment{
|
||||
ID: 1,
|
||||
}
|
||||
|
@ -47,6 +48,7 @@ func TestTaskAttachment_ReadOne(t *testing.T) {
|
|||
assert.Equal(t, []byte("testfile1"), content)
|
||||
})
|
||||
t.Run("Nonexisting Attachment", func(t *testing.T) {
|
||||
files.InitTestFileFixtures(t)
|
||||
ta := &TaskAttachment{
|
||||
ID: 9999,
|
||||
}
|
||||
|
@ -55,6 +57,7 @@ func TestTaskAttachment_ReadOne(t *testing.T) {
|
|||
assert.True(t, IsErrTaskAttachmentDoesNotExist(err))
|
||||
})
|
||||
t.Run("Existing Attachment, Nonexisting File", func(t *testing.T) {
|
||||
files.InitTestFileFixtures(t)
|
||||
ta := &TaskAttachment{
|
||||
ID: 2,
|
||||
}
|
||||
|
@ -83,6 +86,7 @@ func (t *testfile) Close() error {
|
|||
}
|
||||
|
||||
func TestTaskAttachment_NewAttachment(t *testing.T) {
|
||||
files.InitTestFileFixtures(t)
|
||||
// Assert the file is being stored correctly
|
||||
ta := TaskAttachment{
|
||||
TaskID: 1,
|
||||
|
@ -94,7 +98,8 @@ func TestTaskAttachment_NewAttachment(t *testing.T) {
|
|||
|
||||
err := ta.NewAttachment(tf, "testfile", 100, testuser)
|
||||
assert.NoError(t, err)
|
||||
_, err = files.FileStat("/2") // The new file has the id 2 since it's the second attachment
|
||||
_, err = files.FileStat("files/2")
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, os.IsNotExist(err))
|
||||
assert.Equal(t, testuser.ID, ta.CreatedByID)
|
||||
|
||||
|
@ -110,6 +115,7 @@ func TestTaskAttachment_NewAttachment(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestTaskAttachment_ReadAll(t *testing.T) {
|
||||
files.InitTestFileFixtures(t)
|
||||
ta := &TaskAttachment{TaskID: 1}
|
||||
as, err := ta.ReadAll("", &User{ID: 1}, 0)
|
||||
attachments, _ := as.([]*TaskAttachment)
|
||||
|
@ -119,6 +125,7 @@ func TestTaskAttachment_ReadAll(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestTaskAttachment_Delete(t *testing.T) {
|
||||
files.InitTestFileFixtures(t)
|
||||
t.Run("Normal", func(t *testing.T) {
|
||||
ta := &TaskAttachment{ID: 1}
|
||||
err := ta.Delete()
|
||||
|
@ -128,12 +135,14 @@ func TestTaskAttachment_Delete(t *testing.T) {
|
|||
assert.True(t, os.IsNotExist(err))
|
||||
})
|
||||
t.Run("Nonexisting", func(t *testing.T) {
|
||||
files.InitTestFileFixtures(t)
|
||||
ta := &TaskAttachment{ID: 9999}
|
||||
err := ta.Delete()
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrTaskAttachmentDoesNotExist(err))
|
||||
})
|
||||
t.Run("Existing attachment, nonexisting file", func(t *testing.T) {
|
||||
files.InitTestFileFixtures(t)
|
||||
ta := &TaskAttachment{ID: 2}
|
||||
err := ta.Delete()
|
||||
assert.NoError(t, err)
|
||||
|
|
Loading…
Reference in New Issue