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 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
  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("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
  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("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a18")
  42. assert.Error(t, err)
  43. assert.True(t, IsErrAttachmentNotExist(err))
  44. assert.Nil(t, attachment)
  45. }
  46. func TestGetAttachmentByID(t *testing.T) {
  47. assert.NoError(t, PrepareTestDatabase())
  48. attach, err := GetAttachmentByID(1)
  49. assert.NoError(t, err)
  50. assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID)
  51. }
  52. func TestAttachment_DownloadURL(t *testing.T) {
  53. attach := &Attachment{
  54. UUID: "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11",
  55. ID: 1,
  56. }
  57. assert.Equal(t, "https://try.gitea.io/attachments/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.DownloadURL())
  58. }
  59. func TestUpdateAttachment(t *testing.T) {
  60. assert.NoError(t, PrepareTestDatabase())
  61. attach, err := GetAttachmentByID(1)
  62. assert.NoError(t, err)
  63. assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID)
  64. attach.Name = "new_name"
  65. assert.NoError(t, UpdateAttachment(attach))
  66. AssertExistsAndLoadBean(t, &Attachment{Name: "new_name"})
  67. }