-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataFile_test.go
93 lines (76 loc) · 2.44 KB
/
dataFile_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package storage
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestValidatePathFormat(t *testing.T) {
tests := []struct {
path string
hasErr bool
}{
{"", true},
{"path", true},
{"path/", false},
}
for _, test := range tests {
err := validatePathFormat(test.path)
if test.hasErr {
assert.Error(t, err, "Expected an error for path '%s'", test.path)
} else {
assert.NoError(t, err, "Expected no error for path '%s'", test.path)
}
}
}
func TestEnsureDataDirectoryExists(t *testing.T) {
tempDir, err := os.MkdirTemp("", "test_storage")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
path := filepath.Join(tempDir, "data/")
err = ensureDataDirectoryExists(path)
require.NoError(t, err, "Failed to ensure directory exists: %v", err)
_, err = os.Stat(path)
require.NoError(t, err, "Directory was not created: %v", err)
assert.True(t, isDir(path), "Path is not a directory")
}
func TestValidateWriteAccess(t *testing.T) {
tempDir, err := os.MkdirTemp("", "test_storage")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
err = validateWriteAccess(tempDir + "/")
assert.NoError(t, err, "Failed to test write access: %v", err)
}
func TestDataFileExists(t *testing.T) {
tempDir, err := os.MkdirTemp("", "test_storage")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
dataFilePath := filepath.Join(tempDir, "data"+dataFileFormatSuffix)
dataFile, err := os.OpenFile(dataFilePath, os.O_CREATE|os.O_WRONLY, 0o644)
_, err = dataFile.Write([]byte("test"))
require.NoError(t, err, "Failed to write to test .dat file: %v", err)
files, err := extractDatafiles(tempDir)
exists := len(files) > 0
require.NoError(t, err, "Failed to check if data file exists: %v", err)
assert.True(t, exists, "Expected data file to exist")
}
func TestDataFileNotExists(t *testing.T) {
tempDir, err := os.MkdirTemp("", "test_storage")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
dataFilePath := filepath.Join(tempDir, "data"+dataFileFormatSuffix)
_, err = os.Create(dataFilePath)
require.NoError(t, err, "Failed to create test .dat file: %v", err)
files, err := extractDatafiles(tempDir)
exists := len(files) > 0
require.NoError(t, err, "Failed to check if data file exists: %v", err)
assert.False(t, exists, "Expected data file to exist")
}
func isDir(path string) bool {
stat, err := os.Stat(path)
if err != nil {
return false
}
return stat.IsDir()
}