-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_test.go
196 lines (177 loc) · 5.14 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package lsm3
import (
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/xia-Sang/bitcask_go/parse"
"github.com/xia-Sang/bitcask_go/util"
)
func TestDb(t *testing.T) {
c, err := NewContains()
assert.Nil(t, err)
t.Log(c)
c.ShowTables()
c.ShowColumns("User")
c.ShowColumns("my_table")
selectSql1 := "SELECT * FROM User"
err = c.SelectTable(selectSql1)
assert.Nil(t, err)
selectSql2 := "SELECT * FROM my_table"
err = c.SelectTable(selectSql2)
assert.Nil(t, err)
}
func TestDb1(t *testing.T) {
c, err := NewContains()
assert.Nil(t, err)
sql := `CREATE TABLE my_table (
id INT(11),
name VARCHAR(255),
balance DECIMAL(10,2),
birthdate DATE(2024/8/8)
)`
err = c.CreatTable(sql)
assert.Nil(t, err)
for i := range 100 {
insertSql := fmt.Sprintf("INSERT INTO my_table (id, name, balance, birthdate) VALUES (%d, 'sang', %d.%d, '19000401')", i, i, i)
err = c.InsertTable(insertSql)
}
assert.Nil(t, err)
selectSql := "SELECT * FROM my_table where id = 1"
err = c.SelectTable(selectSql)
assert.Nil(t, err)
sql1 := `CREATE TABLE User (
id INT(11),
name VARCHAR(255),
balance DECIMAL(10,2),
birthdate DATE(2024/8/8)
)`
err = c.CreatTable(sql1)
assert.Nil(t, err)
for i := range 10 {
insertSql := fmt.Sprintf("INSERT INTO User (id, name, balance, birthdate) VALUES (%d, 'sang', %d.%d, '19000401')", i, i, i)
err = c.InsertTable(insertSql)
}
assert.Nil(t, err)
selectSql1 := "SELECT * FROM User where id = 1"
err = c.SelectTable(selectSql1)
assert.Nil(t, err)
c.ShowTables()
c.ShowColumns("User")
c.ShowColumns("my_table")
selectSql2 := "SELECT (id, name, balance) FROM User where id = 1"
err = c.SelectTable(selectSql2)
assert.Nil(t, err)
c.Close()
util.RemoveDir(c.dirPath)
}
func TestDb2(t *testing.T) {
c, err := NewContains()
assert.Nil(t, err)
sql1 := `CREATE TABLE User (
id INT(11),
name VARCHAR(255),
balance DECIMAL(10,2),
birthdate DATE(2024/8/8)
)`
err = c.CreatTable(sql1)
assert.Nil(t, err)
for i := range 10 {
insertSql := fmt.Sprintf("INSERT INTO User (id, name, balance, birthdate) VALUES (%d, 'sang', %d.%d, '19000401')", i, i, i)
err = c.InsertTable(insertSql)
}
assert.Nil(t, err)
selectSql1 := "SELECT * FROM User where id = 1"
err = c.SelectTable(selectSql1)
assert.Nil(t, err)
c.ShowTables()
c.ShowColumns("User")
selectSql2 := "SELECT (id, name, balance,age) FROM User where id = 1"
err = c.SelectTable(selectSql2)
assert.Nil(t, err)
selectSql3 := "SELECT (id1, name1, balance1,age) FROM User where id = 1"
err = c.SelectTable(selectSql3)
assert.Nil(t, err)
c.Close()
util.RemoveDir(c.dirPath)
}
func TestDb4(t *testing.T) {
c, err := NewContains()
assert.Nil(t, err)
sql1 := `CREATE TABLE User (
id INT(11),
name VARCHAR(255),
balance DECIMAL(10,2),
birthdate DATE(2024/8/8)
)`
err = c.CreatTable(sql1)
assert.Nil(t, err)
for i := range 10 {
insertSql := fmt.Sprintf("INSERT INTO User (id, name, balance, birthdate) VALUES (%d, 'sang', %d.%d, '19000401')", i, i, i)
err = c.InsertTable(insertSql)
}
assert.Nil(t, err)
selectSql1 := "SELECT * FROM User where id = 1"
err = c.SelectTable(selectSql1)
assert.Nil(t, err)
c.ShowTables()
c.ShowColumns("User")
selectSql2 := "SELECT (id, name, balance,age) FROM User where id = 1"
err = c.SelectTable(selectSql2)
assert.Nil(t, err)
selectSql3 := "SELECT (id1, name1, balance1,age) FROM User where id = 1"
err = c.SelectTable(selectSql3)
assert.Nil(t, err)
deleteSql3 := "DELETE FROM User where id >= 2"
err = c.DeleteTable(deleteSql3)
assert.Nil(t, err)
selectSql4 := "SELECT * FROM User"
err = c.SelectTable(selectSql4)
assert.Nil(t, err)
util.RemoveDir(c.dirPath)
}
func TestDb3(t *testing.T) {
c, err := NewContains()
assert.Nil(t, err)
sql1 := `CREATE TABLE User (id INT(11),name VARCHAR(255),balance DECIMAL(10,2),birthdate DATE(2024/8/8))`
err = c.CreatTable(sql1)
assert.Nil(t, err)
for i := range 5 {
insertSql := fmt.Sprintf("INSERT INTO User (id, name, balance, birthdate) VALUES (%d, 'sang', %d.%d, '19000401')", i, i, i)
err = c.InsertTable(insertSql)
}
assert.Nil(t, err)
selectSql1 := "SELECT * FROM User where name = 'sang'"
err = c.SelectTable(selectSql1)
assert.Nil(t, err)
c.ShowTables()
selectSql2 := "SELECT (id, name,balance) FROM User where name = 'sang'"
err = c.SelectTable(selectSql2)
assert.Nil(t, err)
c.ShowTables()
util.RemoveDir(c.dirPath)
}
func TestCreate(t *testing.T) {
columns := []parse.ColumnDefinition{
{Name: "id", DataType: "string", Constraints: []string{"PRIMARY KEY"}},
{Name: "name", DataType: "string", Constraints: []string{"NOT NULL"}},
{Name: "age", DataType: "int", Constraints: []string{"NOT NULL"}},
{Name: "balance", DataType: "float64", Constraints: []string{"DEFAULT 0.0"}},
//{Name: "gender", DataType: "string", Constraints: []string{"DEFAULT 0.0"}},
}
// 定义要填充的值
values := map[string]interface{}{
"id": "123",
"name": "Alice",
"age": 30,
"balance": 1000.50,
"gender": "male",
}
structType := createStructType(columns)
t.Log(structType)
// 创建结构体实例
instance := reflect.New(structType).Interface()
// 填充值
res := fillStructValues(instance, columns, values)
t.Log(res)
}