integration_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package integration_test
  2. import (
  3. "embed"
  4. "os"
  5. "testing"
  6. "github.com/aws/aws-sdk-go/aws/session"
  7. "github.com/aws/aws-sdk-go/service/s3control"
  8. "github.com/qor/oss/s3"
  9. "github.com/qor5/admin/media"
  10. "github.com/qor5/admin/media/media_library"
  11. "github.com/qor5/admin/media/oss"
  12. "github.com/qor5/web/multipartestutils"
  13. "gorm.io/driver/postgres"
  14. "gorm.io/gorm"
  15. )
  16. //go:embed *.png
  17. var box embed.FS
  18. func setup() (db *gorm.DB) {
  19. var err error
  20. db, err = gorm.Open(postgres.Open(os.Getenv("DBURL")), &gorm.Config{})
  21. if err != nil {
  22. panic(err)
  23. }
  24. db = db.Debug()
  25. // db.Logger = db.Logger.LogMode(logger.Info)
  26. if err = db.AutoMigrate(
  27. &media_library.MediaLibrary{},
  28. ); err != nil {
  29. panic(err)
  30. }
  31. sess := session.Must(session.NewSession())
  32. oss.Storage = s3.New(&s3.Config{
  33. Bucket: os.Getenv("S3_Bucket"),
  34. Region: os.Getenv("S3_Region"),
  35. ACL: s3control.S3CannedAccessControlListBucketOwnerFullControl,
  36. Session: sess,
  37. })
  38. return
  39. }
  40. func TestUpload(t *testing.T) {
  41. db := setup()
  42. f, err := box.ReadFile("testfile.png")
  43. if err != nil {
  44. panic(err)
  45. }
  46. fh := multipartestutils.CreateMultipartFileHeader("test.png", f)
  47. m := media_library.MediaLibrary{}
  48. err = m.File.Scan(fh)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. err = media.SaveUploadAndCropImage(db, &m)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. }