Micro-service for file storage and processing written in Go
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.

143 lines
3.4 KiB

  1. package pipelines
  2. import (
  3. "github.com/geplauder/lithium/storage"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestImagePipelineDeserialization(t *testing.T) {
  10. const Payload string = `{
  11. "name": "example pipeline",
  12. "type": 0,
  13. "removeMetadata": false,
  14. "steps": [
  15. {
  16. "name": "resize image",
  17. "type": 0
  18. },
  19. {
  20. "name": "compress image",
  21. "type": 1
  22. }
  23. ]
  24. }`
  25. t.Run("Image pipeline deserialization is successful", func(t *testing.T) {
  26. values := DeserializePipelines([][]byte{[]byte(Payload)})
  27. assert.Equal(t, 1, len(values), "Output should contain one element")
  28. assert.Equal(t, "example pipeline", values[0].GetName())
  29. assert.Equal(t, Image, values[0].GetType())
  30. })
  31. }
  32. func TestVideoPipelineDeserialization(t *testing.T) {
  33. const Payload string = `{
  34. "name": "example pipeline",
  35. "type": 1,
  36. "removeMetadata": false,
  37. "steps": [
  38. {
  39. "name": "resize image",
  40. "type": 0
  41. },
  42. {
  43. "name": "compress image",
  44. "type": 1
  45. }
  46. ]
  47. }`
  48. t.Run("Video pipelines deserialization is successful", func(t *testing.T) {
  49. values := DeserializePipelines([][]byte{[]byte(Payload)})
  50. assert.Equal(t, 1, len(values), "Output should contain one element")
  51. assert.Equal(t, "example pipeline", values[0].GetName())
  52. assert.Equal(t, Video, values[0].GetType())
  53. })
  54. }
  55. func TestResizeImage(t *testing.T) {
  56. const Payload string = `{
  57. "name": "example pipeline",
  58. "type": 1,
  59. "removeMetadata": false,
  60. "steps": [
  61. {
  62. "name": "resize image",
  63. "type": 0,
  64. "options": {
  65. "width": 1280,
  66. "height": 720,
  67. "upscale": false
  68. }
  69. }
  70. ]
  71. }`
  72. const Bucket string = "pipeline_test_01"
  73. t.Run("Image resizing is successful", func(t *testing.T) {
  74. wd, _ := os.Getwd()
  75. pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
  76. storageProvider := storage.GetFileSystemStorageProvider("test", "..")
  77. _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg"))
  78. assert.Nil(t, err, "Test file should be readable")
  79. assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
  80. dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
  81. assert.Nil(t, err)
  82. assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
  83. // clean up
  84. os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
  85. os.Remove(storageProvider.GetPath(Bucket, dest))
  86. })
  87. }
  88. func TestEncodeImageWithJpegQuality(t *testing.T) {
  89. const Payload string = `{
  90. "name": "example pipeline",
  91. "type": 1,
  92. "removeMetadata": false,
  93. "steps": [
  94. {
  95. "name": "resize image",
  96. "type": 0,
  97. "options": {
  98. "width": 1280,
  99. "height": 720,
  100. "upscale": false
  101. }
  102. }
  103. ],
  104. "output": {
  105. "quality": 50
  106. }
  107. }`
  108. const Bucket string = "pipeline_test_02"
  109. t.Run("Image resizing is successful", func(t *testing.T) {
  110. wd, _ := os.Getwd()
  111. pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
  112. storageProvider := storage.GetFileSystemStorageProvider("test", "..")
  113. _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg"))
  114. assert.Nil(t, err, "Test file should be readable")
  115. assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
  116. dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
  117. assert.Nil(t, err)
  118. assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
  119. // clean up
  120. os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
  121. os.Remove(storageProvider.GetPath(Bucket, dest))
  122. })
  123. }