-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_test.go
124 lines (107 loc) · 2.54 KB
/
db_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
123
124
package cleantone
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"io/ioutil"
"log"
"math"
"os"
"strconv"
"testing"
)
type DBTestSuite struct {
suite.Suite
DBSvc *DB
DBConfig *DBConfig
}
//func (suite *DBTestSuite) SetupSuite() {
//dataPath := "/tmp/cleantone_test_data"
//err := os.Mkdir(dataPath, 0755)
//if err != nil {
// log.Panic(err)
//}
//
//dbConfig := DBConfig{
// RotateThreshold: 1 * KB,
// DataPath: dataPath,
// DataFormat: PersistenceSvc.CSV,
//}
//suite.DBConfig = &dbConfig
//suite.DBSvc = NewDB(dbConfig)
//}
//func (suite *DBTestSuite) TearDownSuite() {
// err := os.RemoveAll(suite.DBConfig.DataPath)
// if err != nil {
// log.Panic(err)
// return
// }
//}
func (suite *DBTestSuite) BeforeTest(_, _ string) {
dataPath := "/tmp/cleantone_test_data"
err := os.Mkdir(dataPath, 0755)
if err != nil {
log.Panic(err)
}
dbConfig := DBConfig{
RotateThreshold: 1 * FileSize.KB,
DataPath: dataPath,
DataFormat: DataFormat.CSV,
}
suite.DBConfig = &dbConfig
suite.DBSvc = NewDB(dbConfig)
}
func (suite *DBTestSuite) AfterTest(_, _ string) {
err := os.RemoveAll(suite.DBConfig.DataPath)
if err != nil {
log.Panic(err)
return
}
}
func (suite *DBTestSuite) TestDBSet() {
key := "a"
value := "b"
suite.DBSvc.Set(key, value)
assert.Equal(suite.T(), suite.DBSvc.Index[key], value)
}
func (suite *DBTestSuite) TestDBRead() {
key := "a"
value := "b"
suite.DBSvc.Set(key, value)
result, err := suite.DBSvc.Get(key)
if err != nil {
assert.Equal(suite.T(), err.Error(), fmt.Sprintf("Key %s not exist.", key))
}
assert.Equal(suite.T(), result, value)
}
func (suite *DBTestSuite) TestDBPersistence() {
totalDataSize := 0
key := "a"
for i := 0; i < 1*FileSize.KB; i++ {
value := strconv.FormatInt(int64(i), 10)
suite.DBSvc.Set(key, value)
totalDataSize = totalDataSize + len(fmt.Sprintf("%s,%s\n", key, value))
}
files, _ := ioutil.ReadDir(suite.DBConfig.DataPath)
fileAmount := math.Ceil(float64(totalDataSize) / float64(suite.DBConfig.RotateThreshold))
assert.Equal(suite.T(), int(fileAmount), len(files))
}
func (suite *DBTestSuite) TestDBInitBuildIndex() {
key := "a"
value := "666"
for i := 0; i < FileSize.KB; i++ {
suite.DBSvc.Set(key, value)
}
newValue := "76786838"
suite.DBSvc.Set(key, newValue)
suite.DBSvc.Close()
suite.DBSvc = NewDB(*suite.DBConfig)
result, err := suite.DBSvc.Get(key)
if err != nil {
log.Panic(err)
}
assert.Equal(suite.T(), result, newValue)
}
func TestDBTestSuite(t *testing.T) {
suite.Run(t, new(DBTestSuite))
}