-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_test.go
207 lines (191 loc) · 3.93 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
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
197
198
199
200
201
202
203
204
205
206
207
package sorm
import (
// "fmt"
// "github.com/otaleghani/spg"
// "strconv"
"testing"
)
type Item struct {
Id string
Desc string
Price float64
InStock bool
Cat_id string
}
type Cat struct {
Id string
CatName_u string
CatDesc string
}
type SomeType struct {
Seppia string
CatName_nu string
Cat_id string
Anvedi int
}
type ItemCat struct {
Id string
Desc string
Price float64
InStock bool
IdCat string
IdCat2 string
CatName string
CatDesc string
}
var path = "test.db"
var dbG Database
func Test_Delete(t *testing.T) {
if err := DeleteDatabase(path); err != nil {
t.Fatal(err)
}
}
func Test_Create(t *testing.T) {
db, err := CreateDatabase("test.db", true)
dbG = *db
if err != nil {
t.Fatal(err)
}
}
func Test_CreateTable(t *testing.T) {
if err := dbG.CreateTable(Cat{}); err != nil {
t.Fatal(err)
}
if err := dbG.CreateTable(Item{}); err != nil {
t.Fatal(err)
}
if err := dbG.CreateTable(SomeType{}); err != nil {
t.Fatal(err)
}
err := dbG.InsertInto(SomeType{
Seppia: "asd",
CatName_nu: "asdoma",
// Cat_id: "nil",
})
if err != nil {
t.Fatal(err)
}
result := []SomeType{}
err = dbG.Select(&result, "seppia = ?", "asd")
if err != nil {
t.Fatal(err)
}
newType := SomeType{
Seppia: "asdef",
}
err = dbG.Update(newType, "Seppia = ?", "asd2")
err = dbG.Delete(SomeType{}, "Seppia = ?", "asdef")
if err != nil {
t.Fatal(err)
}
}
//func Test_Insert(t *testing.T) {
// generator := spg.New("en-usa")
// var opt = spg.Options{}
//
// numItems := 10
// numCat := 10
// items := make([]interface{}, 0, numItems+numCat)
// for i := 0; i < numItems; i++ {
// items = append(items, Item{
// Id: generator.Product().UUID(opt),
// Desc: generator.Person().FirstName(opt),
// Price: 0.99,
// InStock: generator.Boolean(),
// CatId: strconv.Itoa(i),
// })
// }
// for i := 0; i < numCat; i++ {
// items = append(items, Cat{
// Id: strconv.Itoa(i),
// CatName_u: generator.Product().Technology(opt),
// CatDesc: generator.Product().ProductDescription(opt),
// })
// }
//
// if err := dbG.InsertInto(items...); err != nil {
// t.Fatal(err)
// }
//}
//
//func Test_Select(t *testing.T) {
// var selectResult []Item
// for i := 0; i < 1; i++ {
// if err := dbG.Select(&selectResult, ""); err != nil {
// t.Fatal(err)
// }
// }
//}
//
//func Test_Delete_Items(t *testing.T) {
// err := dbG.Delete(Item{}, "InStock = ?", false)
// if err != nil {
// t.Fatal(err)
// }
//}
//
//func Test_Select_After_Delete(t *testing.T) {
// var selectResult []Item
// if err := dbG.Select(&selectResult, ""); err != nil {
// t.Fatal(err)
// }
// for _, row := range selectResult {
// fmt.Println(row)
// }
//}
//
//func Test_Join(t *testing.T) {
// var joinResult []ItemCat
// if err := dbG.Join(&joinResult, Item{}, Cat{}, "Item.CatId = Cat.Id"); err != nil {
// t.Fatal(err)
// }
// for _, val := range joinResult {
// fmt.Println(val)
// }
//}
//
//func Test_Update(t *testing.T) {
// items := make([]interface{}, 0, 1)
// items = append(items, Item{
// Id: "sandro",
// Desc: "something",
// Price: 0.99,
// InStock: false,
// CatId: "1",
// })
//
// if err := dbG.InsertInto(items...); err != nil {
// t.Fatal(err)
// }
//
// updatedItem := Item{
// Id: "sandro",
// Desc: "something else",
// // Price: 0.99,
// // InStock: false,
// // CatId: "1",
// }
// err := dbG.Update(updatedItem, "id = ?", "sandro")
// if err != nil {
// t.Fatal(err)
// }
//
// var selectResult []Item
// if err := dbG.Select(&selectResult, "id = ?", "sandro"); err != nil {
// t.Fatal(err)
// }
// for _, row := range selectResult {
// fmt.Println(row)
// }
//}
//
//func Test_Select2(t *testing.T) {
// var selectResult []Item
// err := dbG.Select(&selectResult, "id = ?", "asdasdasd");
// // if err != nil {
// // t.Fatal(err)
// // }
// fmt.Println(err)
// fmt.Println(len(selectResult))
// fmt.Println(selectResult)
//}