code.gitea.io/gitea@v1.21.7/models/repo/attachment_test.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"code.gitea.io/gitea/models/db"
    10  	repo_model "code.gitea.io/gitea/models/repo"
    11  	"code.gitea.io/gitea/models/unittest"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestIncreaseDownloadCount(t *testing.T) {
    17  	assert.NoError(t, unittest.PrepareTestDatabase())
    18  
    19  	attachment, err := repo_model.GetAttachmentByUUID(db.DefaultContext, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
    20  	assert.NoError(t, err)
    21  	assert.Equal(t, int64(0), attachment.DownloadCount)
    22  
    23  	// increase download count
    24  	err = attachment.IncreaseDownloadCount(db.DefaultContext)
    25  	assert.NoError(t, err)
    26  
    27  	attachment, err = repo_model.GetAttachmentByUUID(db.DefaultContext, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
    28  	assert.NoError(t, err)
    29  	assert.Equal(t, int64(1), attachment.DownloadCount)
    30  }
    31  
    32  func TestGetByCommentOrIssueID(t *testing.T) {
    33  	assert.NoError(t, unittest.PrepareTestDatabase())
    34  
    35  	// count of attachments from issue ID
    36  	attachments, err := repo_model.GetAttachmentsByIssueID(db.DefaultContext, 1)
    37  	assert.NoError(t, err)
    38  	assert.Len(t, attachments, 1)
    39  
    40  	attachments, err = repo_model.GetAttachmentsByCommentID(db.DefaultContext, 1)
    41  	assert.NoError(t, err)
    42  	assert.Len(t, attachments, 2)
    43  }
    44  
    45  func TestDeleteAttachments(t *testing.T) {
    46  	assert.NoError(t, unittest.PrepareTestDatabase())
    47  
    48  	count, err := repo_model.DeleteAttachmentsByIssue(db.DefaultContext, 4, false)
    49  	assert.NoError(t, err)
    50  	assert.Equal(t, 2, count)
    51  
    52  	count, err = repo_model.DeleteAttachmentsByComment(db.DefaultContext, 2, false)
    53  	assert.NoError(t, err)
    54  	assert.Equal(t, 2, count)
    55  
    56  	err = repo_model.DeleteAttachment(db.DefaultContext, &repo_model.Attachment{ID: 8}, false)
    57  	assert.NoError(t, err)
    58  
    59  	attachment, err := repo_model.GetAttachmentByUUID(db.DefaultContext, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a18")
    60  	assert.Error(t, err)
    61  	assert.True(t, repo_model.IsErrAttachmentNotExist(err))
    62  	assert.Nil(t, attachment)
    63  }
    64  
    65  func TestGetAttachmentByID(t *testing.T) {
    66  	assert.NoError(t, unittest.PrepareTestDatabase())
    67  
    68  	attach, err := repo_model.GetAttachmentByID(db.DefaultContext, 1)
    69  	assert.NoError(t, err)
    70  	assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID)
    71  }
    72  
    73  func TestAttachment_DownloadURL(t *testing.T) {
    74  	attach := &repo_model.Attachment{
    75  		UUID: "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11",
    76  		ID:   1,
    77  	}
    78  	assert.Equal(t, "https://try.gitea.io/attachments/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.DownloadURL())
    79  }
    80  
    81  func TestUpdateAttachment(t *testing.T) {
    82  	assert.NoError(t, unittest.PrepareTestDatabase())
    83  
    84  	attach, err := repo_model.GetAttachmentByID(db.DefaultContext, 1)
    85  	assert.NoError(t, err)
    86  	assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID)
    87  
    88  	attach.Name = "new_name"
    89  	assert.NoError(t, repo_model.UpdateAttachment(db.DefaultContext, attach))
    90  
    91  	unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{Name: "new_name"})
    92  }
    93  
    94  func TestGetAttachmentsByUUIDs(t *testing.T) {
    95  	assert.NoError(t, unittest.PrepareTestDatabase())
    96  
    97  	attachList, err := repo_model.GetAttachmentsByUUIDs(db.DefaultContext, []string{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", "not-existing-uuid"})
    98  	assert.NoError(t, err)
    99  	assert.Len(t, attachList, 2)
   100  	assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attachList[0].UUID)
   101  	assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", attachList[1].UUID)
   102  	assert.Equal(t, int64(1), attachList[0].IssueID)
   103  	assert.Equal(t, int64(5), attachList[1].IssueID)
   104  }