-
Notifications
You must be signed in to change notification settings - Fork 2
/
database_test.go
122 lines (102 loc) · 3.01 KB
/
database_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"io"
"os"
"path/filepath"
"testing"
"time"
logrus "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
// MockConfig returns a configuration suitable for testing.
func MockConfig(databaseDirPath string) *Config {
return &Config{
Database: DatabaseConfig{
DatabaseDirPath: databaseDirPath,
DatabaseFile: "test.db",
FileLockTimeout: 1 * time.Second,
MaxBatchDelay: 1 * time.Millisecond,
MaxBatchSize: 100,
},
}
}
// TestSetupDatabase tests the setupDatabase function.
func TestSetupDatabase(t *testing.T) {
// Suppress log output by setting the output to io.Discard.
logrus.SetOutput(io.Discard)
// Case 1: Successful database setup.
t.Run("Successful Database Setup", func(t *testing.T) {
tempDir := t.TempDir()
config := MockConfig(tempDir)
db, err := setupDatabase(config)
assert.NoError(
t, err, "setupDatabase should not return an error",
)
assert.NotNil(t, db, "Database instance should not be nil")
// Clean up.
cleanupDB(db)
assert.FileExists(
t, filepath.Join(tempDir, "test.db"),
"Database file should exist",
)
})
// Case 2: Directory creation failure.
t.Run("Directory Creation Failure", func(t *testing.T) {
invalidDir := "/invalid-directory"
config := MockConfig(invalidDir)
_, err := setupDatabase(config)
assert.Error(
t, err, "setupDatabase should return an error for "+
"invalid directory",
)
})
// Case 3: Database file creation failure.
t.Run("Database File Creation Failure", func(t *testing.T) {
tempDir := t.TempDir()
config := MockConfig(tempDir)
// Set permissions on the tempDir to simulate file creation
// failure.
err := os.Chmod(tempDir, 0444)
assert.NoError(t, err)
_, err = setupDatabase(config)
assert.Error(
t, err, "setupDatabase should return an error when "+
"database file cannot be created",
)
// Restore permissions.
err = os.Chmod(tempDir, 0755)
assert.NoError(t, err)
})
}
// TestCleanupDB tests the cleanupDB function.
func TestCleanupDB(t *testing.T) {
// Suppress log output by setting the output to io.Discard.
logrus.SetOutput(io.Discard)
// Case 1: Successful database cleanup.
t.Run("Successful Database Cleanup", func(t *testing.T) {
tempDir := t.TempDir()
config := MockConfig(tempDir)
db, err := setupDatabase(config)
assert.NoError(
t, err, "setupDatabase should not return an error",
)
assert.NotNil(t, db, "Database instance should not be nil")
// Clean up and check no error is logged.
cleanupDB(db)
})
// Case 2: Cleanup with already closed DB.
t.Run("Cleanup With Already Closed DB", func(t *testing.T) {
tempDir := t.TempDir()
config := MockConfig(tempDir)
db, err := setupDatabase(config)
assert.NoError(
t, err, "setupDatabase should not return an error",
)
assert.NotNil(t, db, "Database instance should not be nil")
// Close the DB before calling cleanupDB.
db.Close()
assert.NotPanics(
t, func() { cleanupDB(db) }, "cleanupDB should not "+"panic if the DB is already closed",
)
})
}