You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

attachment_test.go 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestIncreaseDownloadCount(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. attachment, err := GetAttachmentByUUID("1234567890")
  12. assert.NoError(t, err)
  13. assert.Equal(t, int64(0), attachment.DownloadCount)
  14. // increase download count
  15. err = attachment.IncreaseDownloadCount()
  16. assert.NoError(t, err)
  17. attachment, err = GetAttachmentByUUID("1234567890")
  18. assert.NoError(t, err)
  19. assert.Equal(t, int64(1), attachment.DownloadCount)
  20. }
  21. func TestGetByCommentOrIssueID(t *testing.T) {
  22. assert.NoError(t, PrepareTestDatabase())
  23. // count of attachments from issue ID
  24. attachments, err := GetAttachmentsByIssueID(1)
  25. assert.NoError(t, err)
  26. assert.Equal(t, 2, len(attachments))
  27. attachments, err = GetAttachmentsByCommentID(1)
  28. assert.NoError(t, err)
  29. assert.Equal(t, 2, len(attachments))
  30. }
  31. func TestDeleteAttachments(t *testing.T) {
  32. assert.NoError(t, PrepareTestDatabase())
  33. count, err := DeleteAttachmentsByIssue(4, false)
  34. assert.NoError(t, err)
  35. assert.Equal(t, 1, count)
  36. count, err = DeleteAttachmentsByComment(2, false)
  37. assert.NoError(t, err)
  38. assert.Equal(t, 2, count)
  39. err = DeleteAttachment(&Attachment{ID: 8}, false)
  40. assert.NoError(t, err)
  41. attachment, err := GetAttachmentByUUID("test-12345")
  42. assert.Error(t, err)
  43. assert.True(t, IsErrAttachmentNotExist(err))
  44. assert.Nil(t, attachment)
  45. }