-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotorm_test.go
170 lines (154 loc) · 3.51 KB
/
notorm_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
package notorm
import (
"testing"
_ "github.com/mattn/go-sqlite3"
)
const (
_dbDriver = "mysql"
_dbParam = "root:rootroot@tcp(127.0.0.1:3306)/notorm"
)
type User struct {
ID int
Name string
Address string
}
type Email struct {
ID int
Email string
UserID int
}
type Article struct {
ID int
Content string `mysql:"type=TEXT"`
Uniq string `mysql:"constraints=UNIQUE NOT NULL"`
}
func TestAll(t *testing.T) {
no := NewConnection(_dbDriver, _dbParam)
no.CreateTable(User{})
no.CreateTable(Email{})
no.Insert(User{1, "Simon", "OCT"})
u := User{}
err := no.Select("WHERE id=1", &u)
if err != nil {
t.Error("Failed to select")
}
if u.ID != 1 {
t.Errorf("id error: %d\n", u.ID)
}
if u.Name != "Simon" {
t.Errorf("Name error: %d\n", u.Name)
}
if u.Address != "OCT" {
t.Errorf("address error: %d\n", u.Address)
}
no.Insert(Email{1, "[email protected]", 1})
e := Email{}
err = no.Select("WHERE id=1", &e)
if err != nil {
t.Error("Failed to select")
}
if e.ID != 1 {
t.Errorf("id error: %d\n", e.ID)
}
if e.Email != "[email protected]" {
t.Errorf("email error: %d\n", e.Email)
}
if e.UserID != 1 {
t.Errorf("uid error: %d\n", e.UserID)
}
}
func TestSelectAll(t *testing.T) {
no := NewConnection(_dbDriver, _dbParam)
no.Debug(true)
no.CreateTable(User{})
no.CreateTable(Email{})
no.Insert(User{2, "Simon", "OCT"})
no.Insert(Email{2, "[email protected]", 2})
no.Insert(Email{3, "[email protected]", 2})
no.Insert(Email{4, "[email protected]", 2})
no.Insert(Email{5, "[email protected]", 2})
arr, err := no.SelectAll("WHERE userid=2", Email{})
if err != nil {
t.Errorf("failed.")
}
if len(arr) != 4 {
t.Errorf("should have 4 items: %d", len(arr))
}
email := arr[0].(*Email)
if email.ID != 2 {
t.Errorf("Wrong email id: %d\n", email.ID)
}
email = arr[3].(*Email)
if email.ID != 5 {
t.Errorf("Wrong email id: %d\n", email.ID)
}
}
func TestType(t *testing.T) {
no := NewConnection(_dbDriver, _dbParam)
no.Debug(true)
no.CreateTable(Article{})
}
type Student struct {
ID int
Name string
Grade int
}
func TestPage(t *testing.T) {
no := NewConnection(_dbDriver, _dbParam)
no.Debug(true)
no.CreateTable(Student{})
// Insert 9 students
for i := 0; i < 9; i++ {
no.Insert(Student{Name: "John", Grade: i % 3})
}
c, _ := no.Count("", Student{})
if c != 9 {
t.Errorf("Wrong number of students: %d\n", c)
}
c, _ = no.Count("WHERE grade=2", Student{})
if c != 3 {
t.Errorf("Wrong number of students: %d\n", c)
}
page, err := no.SelectPage("", 1, 3, Student{})
if err != nil {
t.Errorf("Failed")
}
if len(page) != 3 {
t.Errorf("Should have %d elements", len(page))
}
page, err = no.SelectPage("", 2, 3, Student{})
if err != nil {
t.Errorf("Failed")
}
if len(page) != 3 {
t.Errorf("Should have %d elements", len(page))
}
stu := page[0].(*Student)
if stu.Name != "John" {
t.Errorf("Wrong name : %s\n", stu.Name)
}
if stu.ID != 4 {
t.Errorf("Wrong id : %d\n", stu.ID)
}
page, err = no.SelectPage("WHERE grade=0", 2, 3, Student{})
stu = page[0].(*Student)
if stu.Name != "John" {
t.Errorf("Wrong name : %s\n", stu.Name)
}
}
func TestDelete(t *testing.T) {
no := NewConnection(_dbDriver, _dbParam)
no.Debug(true)
no.CreateTable(Student{})
for i := 0; i < 9; i++ {
no.Insert(Student{Name: "Kirsten", Grade: i % 3})
}
c, _ := no.Delete(`WHERE grade=0 AND name="Kirsten"`, Student{})
if c != 3 {
t.Errorf("Wrong number of students: %d\n", c)
}
c, _ = no.Delete(`WHERE name="Kirsten"`, Student{})
if c != 6 {
t.Errorf("Wrong number of students: %d\n", c)
}
}