gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package attachment
  4. import (
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. "code.gitea.io/gitea/models/unittest"
  10. user_model "code.gitea.io/gitea/models/user"
  11. _ "code.gitea.io/gitea/models/actions"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestMain(m *testing.M) {
  15. unittest.MainTest(m)
  16. }
  17. func TestUploadAttachment(t *testing.T) {
  18. assert.NoError(t, unittest.PrepareTestDatabase())
  19. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
  20. fPath := "./attachment_test.go"
  21. f, err := os.Open(fPath)
  22. assert.NoError(t, err)
  23. defer f.Close()
  24. attach, err := NewAttachment(t.Context(), &repo_model.Attachment{
  25. RepoID: 1,
  26. UploaderID: user.ID,
  27. Name: filepath.Base(fPath),
  28. }, f, -1)
  29. assert.NoError(t, err)
  30. attachment, err := repo_model.GetAttachmentByUUID(t.Context(), attach.UUID)
  31. assert.NoError(t, err)
  32. assert.Equal(t, user.ID, attachment.UploaderID)
  33. assert.Equal(t, int64(0), attachment.DownloadCount)
  34. }