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.

53 lines
1.4 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. afero.WriteFile(fileSystem, "/tmp/existing.bin", dummyData, os.ModePerm)
  27. provider := FileSystemStorageProvider{
  28. fileSystem: fileSystem,
  29. basePath: "/tmp/foo/bar",
  30. }
  31. finalPath, err := provider.StoreExisting("test", "test.bin", "/tmp/existing.bin")
  32. assert.Nil(t, err)
  33. assert.Equal(t, "/tmp/foo/bar/test/test.bin", finalPath)
  34. exists, _ := afero.Exists(fileSystem, "/tmp/foo/bar/test/test.bin")
  35. assert.True(t, exists)
  36. content, _ := afero.ReadFile(fileSystem, "/tmp/foo/bar/test/test.bin")
  37. assert.Equal(t, dummyData, content)
  38. })
  39. }