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.

186 lines
4.5 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. // pipeline deserialization
  10. func TestImagePipelineDeserialization(t *testing.T) {
  11. const Payload string = `{
  12. "name": "example pipeline",
  13. "type": 0,
  14. "removeMetadata": false,
  15. "steps": [
  16. {
  17. "name": "resize image",
  18. "type": 0
  19. },
  20. {
  21. "name": "compress image",
  22. "type": 1
  23. }
  24. ]
  25. }`
  26. t.Run("Image pipeline deserialization is successful", func(t *testing.T) {
  27. values := DeserializePipelines([][]byte{[]byte(Payload)})
  28. assert.Equal(t, 1, len(values), "Output should contain one element")
  29. assert.Equal(t, "example pipeline", values[0].GetName())
  30. assert.Equal(t, Image, values[0].GetType())
  31. })
  32. }
  33. func TestVideoPipelineDeserialization(t *testing.T) {
  34. const Payload string = `{
  35. "name": "example pipeline",
  36. "type": 1,
  37. "removeMetadata": false,
  38. "steps": [
  39. {
  40. "name": "resize image",
  41. "type": 0
  42. },
  43. {
  44. "name": "compress image",
  45. "type": 1
  46. }
  47. ]
  48. }`
  49. t.Run("Video pipelines deserialization is successful", func(t *testing.T) {
  50. values := DeserializePipelines([][]byte{[]byte(Payload)})
  51. assert.Equal(t, 1, len(values), "Output should contain one element")
  52. assert.Equal(t, "example pipeline", values[0].GetName())
  53. assert.Equal(t, Video, values[0].GetType())
  54. })
  55. }
  56. // image pipeline steps
  57. func TestResizeImage(t *testing.T) {
  58. const Payload string = `{
  59. "name": "example pipeline",
  60. "type": 1,
  61. "removeMetadata": false,
  62. "steps": [
  63. {
  64. "name": "resize image",
  65. "type": 0,
  66. "options": {
  67. "width": 1280,
  68. "height": 720,
  69. "upscale": false
  70. }
  71. }
  72. ]
  73. }`
  74. const Bucket string = "pipeline_test_01"
  75. t.Run("Image resizing is successful", func(t *testing.T) {
  76. wd, _ := os.Getwd()
  77. pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
  78. storageProvider := storage.GetFileSystemStorageProvider("test", "..")
  79. _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg"))
  80. assert.Nil(t, err, "Test file should be readable")
  81. assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
  82. dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
  83. assert.Nil(t, err)
  84. assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
  85. // clean up
  86. os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
  87. os.Remove(storageProvider.GetPath(Bucket, dest))
  88. })
  89. }
  90. func TestRotateImage(t *testing.T) {
  91. const Payload string = `{
  92. "name": "example pipeline",
  93. "type": 1,
  94. "removeMetadata": false,
  95. "steps": [
  96. {
  97. "name": "rotate image",
  98. "type": 1,
  99. "options": {
  100. "angle": 90.0
  101. }
  102. }
  103. ]
  104. }`
  105. const Bucket string = "pipeline_test_03"
  106. t.Run("Image rotating is successful", func(t *testing.T) {
  107. wd, _ := os.Getwd()
  108. pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
  109. storageProvider := storage.GetFileSystemStorageProvider("test", "..")
  110. _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg"))
  111. assert.Nil(t, err, "Test file should be readable")
  112. assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
  113. dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
  114. assert.Nil(t, err)
  115. assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
  116. // clean up
  117. os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
  118. os.Remove(storageProvider.GetPath(Bucket, dest))
  119. })
  120. }
  121. // output options
  122. func TestEncodeImageWithJpegQuality(t *testing.T) {
  123. const Payload string = `{
  124. "name": "example pipeline",
  125. "type": 1,
  126. "removeMetadata": false,
  127. "steps": [
  128. {
  129. "name": "resize image",
  130. "type": 0,
  131. "options": {
  132. "width": 1280,
  133. "height": 720,
  134. "upscale": false
  135. }
  136. }
  137. ],
  138. "output": {
  139. "quality": 50
  140. }
  141. }`
  142. const Bucket string = "pipeline_test_02"
  143. t.Run("Image encoding with jpeg quality is successful", func(t *testing.T) {
  144. wd, _ := os.Getwd()
  145. pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
  146. storageProvider := storage.GetFileSystemStorageProvider("test", "..")
  147. _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg"))
  148. assert.Nil(t, err, "Test file should be readable")
  149. assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
  150. dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
  151. assert.Nil(t, err)
  152. assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
  153. // clean up
  154. os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
  155. os.Remove(storageProvider.GetPath(Bucket, dest))
  156. })
  157. }