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.

68 lines
1.8 KiB

  1. package storage
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/spf13/afero"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestFileSystemStorageProvider(t *testing.T) {
  9. dummyData := []byte{0x13, 0x37}
  10. t.Run("storeRaw method stores files in filesystem", func(t *testing.T) {
  11. fileSystem := afero.NewMemMapFs()
  12. provider := FileSystemStorageProvider{
  13. fileSystem: fileSystem,
  14. basePath: "/tmp/foo/bar",
  15. }
  16. finalPath, err := provider.StoreRaw("test", "test.bin", dummyData)
  17. assert.Nil(t, err)
  18. assert.Equal(t, "/tmp/foo/bar/test/test.bin", finalPath)
  19. exists, _ := afero.Exists(fileSystem, "/tmp/foo/bar/test/test.bin")
  20. assert.True(t, exists)
  21. content, _ := afero.ReadFile(fileSystem, "/tmp/foo/bar/test/test.bin")
  22. assert.Equal(t, dummyData, content)
  23. })
  24. t.Run("storeExisting method stores files in filesystem", func(t *testing.T) {
  25. fileSystem := afero.NewMemMapFs()
  26. err := os.WriteFile("/tmp/existing.bin", dummyData, os.ModePerm)
  27. assert.Nil(t, err)
  28. provider := FileSystemStorageProvider{
  29. fileSystem: fileSystem,
  30. basePath: "/tmp/foo/bar",
  31. }
  32. finalPath, err := provider.StoreExisting("test", "test.bin", "/tmp/existing.bin")
  33. assert.Nil(t, err)
  34. assert.Equal(t, "/tmp/foo/bar/test/test.bin", finalPath)
  35. exists, _ := afero.Exists(fileSystem, "/tmp/foo/bar/test/test.bin")
  36. assert.True(t, exists)
  37. content, _ := afero.ReadFile(fileSystem, "/tmp/foo/bar/test/test.bin")
  38. assert.Equal(t, dummyData, content)
  39. })
  40. t.Run("getPath method returns correct path", func(t *testing.T) {
  41. fileSystem := afero.NewMemMapFs()
  42. provider := FileSystemStorageProvider{
  43. fileSystem: fileSystem,
  44. basePath: "/tmp/foo/bar",
  45. }
  46. _, err := provider.StoreRaw("test", "test.bin", dummyData)
  47. assert.Nil(t, err)
  48. assert.Equal(t, "files/tmp/foo/bar/test/test.bin", provider.GetPath("test", "test.bin"))
  49. })
  50. }