upbfs_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. "go-common/app/admin/main/appstatic/model"
  8. xtime "go-common/library/time"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. var (
  12. c = context.TODO()
  13. now = time.Now().Unix()
  14. )
  15. func TestService_AddFile(t *testing.T) {
  16. Convey("AddFile should return without err", t, WithService(func(svf *Service) {
  17. // simulate resource & file data
  18. res := &model.Resource{}
  19. res.ID = 1
  20. resFile := &model.ResourceFile{
  21. ID: 0,
  22. Name: "tName",
  23. Type: "image/png",
  24. Md5: "d41d8cd98f00b204e9800998ecf8427e",
  25. Size: 20,
  26. URL: "http://uat-i0.hdslb.com/bfs/app-static/timg.png",
  27. ResourceID: 2,
  28. Ctime: xtime.Time(now),
  29. Mtime: xtime.Time(now),
  30. }
  31. // DB insert
  32. err := svf.AddFile(c, resFile, 3)
  33. // testing
  34. So(err, ShouldBeNil)
  35. }))
  36. }
  37. func TestService_Upload(t *testing.T) {
  38. Convey("Upload should return without err", t, WithService(func(svf *Service) {
  39. // simulate file content
  40. str2 := "Testing File Content"
  41. data2 := []byte(str2)
  42. // upload to bfs
  43. location, err := svf.Upload(c, "", "image/png", now, data2)
  44. // testing
  45. So(err, ShouldBeNil)
  46. So(location, ShouldNotBeNil)
  47. fmt.Println(location)
  48. }))
  49. }
  50. func TestService_ParseFile(t *testing.T) {
  51. Convey("ParseFile can get file info", t, WithService(func(svf *Service) {
  52. // simulate file content
  53. str2 := "Testing File Content"
  54. data2 := []byte(str2)
  55. // upload to bfs
  56. fInfo, err := svf.ParseFile(data2)
  57. // testing
  58. So(err, ShouldBeNil)
  59. So(fInfo.Size, ShouldBeGreaterThan, 0)
  60. So(fInfo.Type, ShouldNotBeBlank)
  61. So(fInfo.Md5, ShouldNotBeBlank)
  62. }))
  63. }
  64. func TestService_TypeCheck(t *testing.T) {
  65. Convey("File Type Check", t, WithService(func(svf *Service) {
  66. So(svf.TypeCheck("application/zip"), ShouldBeTrue)
  67. So(svf.TypeCheck("application/xml"), ShouldBeTrue)
  68. So(svf.TypeCheck("application/json"), ShouldBeFalse)
  69. }))
  70. }