Skip to content

Commit ad24535

Browse files
FEATURE (storages): Add test for local storages
1 parent b09064a commit ad24535

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is test text

backend/internal/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type EnvVariables struct {
2828

2929
DataFolder string
3030
TempFolder string
31+
32+
TestFilePath string
3133
}
3234

3335
var (
@@ -118,5 +120,7 @@ func loadEnvVariables() {
118120
env.DataFolder = filepath.Join(filepath.Dir(backendRoot), "postgresus-data", "data")
119121
env.TempFolder = filepath.Join(filepath.Dir(backendRoot), "postgresus-data", "temp")
120122

123+
env.TestFilePath = filepath.Join(backendRoot, "assets", "file_for_storage_text.txt")
124+
121125
log.Info("Environment variables loaded successfully!")
122126
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package storages
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"os"
7+
"postgresus-backend/internal/config"
8+
local_storage "postgresus-backend/internal/features/storages/storages/local"
9+
"testing"
10+
11+
"github.com/google/uuid"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func Test_Storage_BasicOperations(t *testing.T) {
17+
testCases := []struct {
18+
name string
19+
storage StorageFileSaver
20+
}{
21+
{
22+
name: "LocalStorage",
23+
storage: &local_storage.LocalStorage{StorageID: uuid.New()},
24+
},
25+
}
26+
27+
for _, tc := range testCases {
28+
t.Run(tc.name, func(t *testing.T) {
29+
t.Run("Test_TestConnection_ConnectionSucceeds", func(t *testing.T) {
30+
err := tc.storage.TestConnection()
31+
assert.NoError(t, err, "TestConnection should succeed")
32+
})
33+
34+
t.Run("Test_TestValidation_ValidationSucceeds", func(t *testing.T) {
35+
err := tc.storage.Validate()
36+
assert.NoError(t, err, "Validate should succeed")
37+
})
38+
39+
t.Run("Test_TestSaveAndGetFile_ReturnsCorrectContent", func(t *testing.T) {
40+
testData, err := os.ReadFile(config.GetEnv().TestFilePath)
41+
require.NoError(t, err, "Should be able to read test file")
42+
43+
fileID := uuid.New()
44+
45+
err = tc.storage.SaveFile(fileID, bytes.NewReader(testData))
46+
require.NoError(t, err, "SaveFile should succeed")
47+
48+
file, err := tc.storage.GetFile(fileID)
49+
assert.NoError(t, err, "GetFile should succeed")
50+
defer file.Close()
51+
52+
content, err := io.ReadAll(file)
53+
assert.NoError(t, err, "Should be able to read file")
54+
assert.Equal(t, testData, content, "File content should match the original")
55+
})
56+
57+
t.Run("Test_TestDeleteFile_RemovesFileFromDisk", func(t *testing.T) {
58+
// Read test file
59+
testData, err := os.ReadFile(config.GetEnv().TestFilePath)
60+
require.NoError(t, err, "Should be able to read test file")
61+
62+
// Generate a unique file ID
63+
fileID := uuid.New()
64+
65+
// Save file first
66+
err = tc.storage.SaveFile(fileID, bytes.NewReader(testData))
67+
require.NoError(t, err, "SaveFile should succeed")
68+
69+
// Delete file
70+
err = tc.storage.DeleteFile(fileID)
71+
assert.NoError(t, err, "DeleteFile should succeed")
72+
73+
// Try to get the deleted file
74+
file, err := tc.storage.GetFile(fileID)
75+
assert.Error(t, err, "GetFile should fail for non-existent file")
76+
if file != nil {
77+
file.Close()
78+
}
79+
})
80+
81+
t.Run("Test_TestDeleteNonExistentFile_DoesNotError", func(t *testing.T) {
82+
// Try to delete a non-existent file
83+
nonExistentID := uuid.New()
84+
err := tc.storage.DeleteFile(nonExistentID)
85+
assert.NoError(t, err, "DeleteFile should not error for non-existent file")
86+
})
87+
})
88+
}
89+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package local_storage
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"os"
7+
"postgresus-backend/internal/config"
8+
"testing"
9+
10+
"github.com/google/uuid"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func Test_LocalStorage_BasicOperations(t *testing.T) {
16+
storage := &LocalStorage{
17+
StorageID: uuid.New(),
18+
}
19+
20+
t.Run("Test_TestConnection_ConnectionSucceeds", func(t *testing.T) {
21+
err := storage.TestConnection()
22+
assert.NoError(t, err, "TestConnection should succeed")
23+
})
24+
25+
t.Run("Test_TestValidation_ValidationSucceeds", func(t *testing.T) {
26+
err := storage.Validate()
27+
assert.NoError(t, err, "Validate should succeed")
28+
})
29+
30+
t.Run("Test_TestSaveAndGetFile_ReturnsCorrectContent", func(t *testing.T) {
31+
testData, err := os.ReadFile(config.GetEnv().TestFilePath)
32+
require.NoError(t, err, "Should be able to read test file")
33+
34+
fileID := uuid.New()
35+
36+
err = storage.SaveFile(fileID, bytes.NewReader(testData))
37+
require.NoError(t, err, "SaveFile should succeed")
38+
39+
file, err := storage.GetFile(fileID)
40+
assert.NoError(t, err, "GetFile should succeed")
41+
defer file.Close()
42+
43+
content, err := io.ReadAll(file)
44+
assert.NoError(t, err, "Should be able to read file")
45+
assert.Equal(t, testData, content, "File content should match the original")
46+
})
47+
48+
t.Run("Test_TestDeleteFile_RemovesFileFromDisk", func(t *testing.T) {
49+
// Read test file
50+
testData, err := os.ReadFile(config.GetEnv().TestFilePath)
51+
require.NoError(t, err, "Should be able to read test file")
52+
53+
// Generate a unique file ID
54+
fileID := uuid.New()
55+
56+
// Save file first
57+
err = storage.SaveFile(fileID, bytes.NewReader(testData))
58+
require.NoError(t, err, "SaveFile should succeed")
59+
60+
// Delete file
61+
err = storage.DeleteFile(fileID)
62+
assert.NoError(t, err, "DeleteFile should succeed")
63+
64+
file, err := storage.GetFile(fileID)
65+
assert.Error(t, err, "GetFile should fail for non-existent file")
66+
assert.Nil(t, file, "File should be nil")
67+
})
68+
69+
t.Run("Test_TestDeleteNonExistentFile_DoesNotError", func(t *testing.T) {
70+
// Try to delete a non-existent file
71+
nonExistentID := uuid.New()
72+
err := storage.DeleteFile(nonExistentID)
73+
assert.NoError(t, err, "DeleteFile should not error for non-existent file")
74+
})
75+
}

0 commit comments

Comments
 (0)