From f11e88910272fe27ee4085211c6368e302d9faf8 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 13 Oct 2019 13:21:16 +0200 Subject: [PATCH] Added test to check if the attachment file was inserted correctly --- pkg/models/task_attachment.go | 1 + pkg/models/task_attachment_test.go | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pkg/models/task_attachment.go b/pkg/models/task_attachment.go index 214924397..c97524905 100644 --- a/pkg/models/task_attachment.go +++ b/pkg/models/task_attachment.go @@ -56,6 +56,7 @@ func (ta *TaskAttachment) NewAttachment(f io.ReadCloser, realname string, realsi // Add an entry to the db ta.FileID = file.ID + ta.CreatedByID = a.GetID() _, err = x.Insert(ta) if err != nil { // remove the uploaded file if adding it to the db fails diff --git a/pkg/models/task_attachment_test.go b/pkg/models/task_attachment_test.go index 2bc02dbac..f309f1183 100644 --- a/pkg/models/task_attachment_test.go +++ b/pkg/models/task_attachment_test.go @@ -87,16 +87,24 @@ func TestTaskAttachment_NewAttachment(t *testing.T) { ta := TaskAttachment{ TaskID: 1, } - tf := &testfile{ content: []byte("testingstuff"), } - err := ta.NewAttachment(tf, "testfile", 100, &User{ID: 1}) - assert.NoError(t, err) - _, err = files.FileStat("/2") - assert.False(t, os.IsNotExist(err)) + testuser := &User{ID: 1} - // Also assert created_by_id is correct in the files table + 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 + assert.False(t, os.IsNotExist(err)) + assert.Equal(t, testuser.ID, ta.CreatedByID) + + // Check the file was inserted correctly + ta.File = &files.File{ID: ta.FileID} + err = ta.File.LoadFileMetaByID() + assert.NoError(t, err) + assert.Equal(t, testuser.ID, ta.File.CreatedByID) + assert.Equal(t, "testfile", ta.File.Name) + assert.Equal(t, int64(100), ta.File.Size) // Extra test for max size test }