-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
190 lines (157 loc) · 4.39 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
package db
import (
"database/sql"
"fmt"
"strings"
"testing"
"github.com/jmoiron/sqlx"
)
type Story struct {
Id PrimaryKey
Name string
Body string
Slug string
SlugBody string
Author *HasOne `table:"author"`
}
type Author struct {
Id PrimaryKey
Name string
Stories *HasMany `table:"story" on:"author"`
}
type Data struct {
Statement string
Parameters map[string]interface{}
}
type TestResult struct{}
func (t TestResult) LastInsertId() (int64, error) {
return -5, nil
}
func (t TestResult) RowsAffected() (int64, error) {
return 1, nil
}
type TestDb struct {
Data chan Data
}
func (t *TestDb) NamedExec(query string, arg interface{}) (sql.Result, error) {
t.Data <- Data{
Statement: query,
Parameters: arg.(map[string]interface{}),
}
return TestResult{}, nil
}
func (t *TestDb) NamedQuery(query string, arg interface{}) (*sqlx.Rows, error) {
return nil, nil
}
func TestSelect(t *testing.T) {
dataChan := make(chan Data, 1)
connection := &TestDb{
Data: dataChan,
}
// Create Author Table
authorTable, err := CreateTableFromStruct("author", connection, true, &Author{})
if err != nil {
t.Error(err.Error())
}
data := <-dataChan
if data.Statement != "CREATE TABLE author (id integer, name text, CONSTRAINT author_pk PRIMARY KEY (id))" {
t.Error("Creating Authors Table Incorrect SQL")
}
fmt.Println(data.Statement, data.Parameters)
// Create Story Table
storyTable, err := CreateTableFromStruct("story", connection, false, &Story{})
if err != nil {
t.Error(err.Error())
}
data = <-dataChan
if data.Statement != "CREATE TABLE IF NOT EXISTS story (id integer, name text, body text, slug text, author integer, CONSTRAINT story_pk PRIMARY KEY (id))" {
t.Error("Creating Stories Table Incorrect SQL")
}
fmt.Println(data.Statement, data.Parameters)
// Insert an Author
newAuthor := &Author{
Name: "Hunter Leath",
}
_, err = authorTable.Insert(newAuthor).Exec(connection)
if err != nil {
t.Error(err.Error())
}
if newAuthor.Id != -5 {
t.Error("Id not set successfully.")
}
data = <-dataChan
if data.Statement != "INSERT INTO author (name) VALUES (:name)" {
t.Error("Inserting Author Incorrect SQL")
}
fmt.Println(data.Statement, data.Parameters)
// Insert a Story
newStory := &Story{
Name: "Going to the beach",
Body: "Lorem ipsum.",
Slug: "going-to-the-beach",
Author: ForeignKey(newAuthor),
}
_, err = storyTable.Insert(newStory).Exec(connection)
if err != nil {
t.Error(err.Error())
}
if newStory.Id != -5 {
t.Error("Id not set successfully.")
}
data = <-dataChan
if !strings.HasPrefix(data.Statement, "INSERT INTO story") {
t.Error("Inserting Story Incorrect SQL")
}
fmt.Println(data.Statement, data.Parameters)
// Update a Story
newStory.Body = "Lorem Ipsum 2"
_, err = storyTable.Update(newStory).Exec(connection)
if err != nil {
t.Error(err.Error())
}
data = <-dataChan
if !strings.HasPrefix(data.Statement, "UPDATE story SET") {
t.Error("Updating Story Incorrect SQL")
}
fmt.Println(data.Statement, data.Parameters)
// Basic Select Queries
_, err = storyTable.Get().Where("slug", "going-to-the-beach").Where("author", -5).Order("slug", true).Limit(5).Exec(connection)
if err != nil {
t.Error(err.Error())
}
data = <-dataChan
if data.Statement != "SELECT * FROM story WHERE (slug = :variable_slug AND author = :variable_author) ORDER BY slug ASC LIMIT 5" {
t.Error("Selecting Story Incorrect SQL")
}
fmt.Println(data.Statement, data.Parameters)
// HasOne Relationship
_, err = newStory.Author.Exec(connection)
if err != nil {
t.Error(err.Error())
}
data = <-dataChan
if data.Statement != "SELECT * FROM author WHERE (id = :variable_id)" {
t.Error("Selecting Author Through Relationship Incorrect SQL")
}
fmt.Println(data.Statement, data.Parameters)
// HasMany Relationship
_, err = newAuthor.Stories.Limit(2).Exec(connection)
if err != nil {
t.Error(err.Error())
}
data = <-dataChan
if data.Statement != "SELECT * FROM story WHERE (author = :variable_author) LIMIT 2" {
t.Error("Selecting Stories Through Relationship Incorrect SQL")
}
fmt.Println(data.Statement, data.Parameters)
// Deleting Items
_, err = authorTable.Delete(newAuthor).Exec(connection)
if err != nil {
t.Error(err.Error())
}
data = <-dataChan
if data.Statement != "DELETE FROM author WHERE id = :variable_id" {
t.Error("Deleting Author Incorrect SQL")
}
fmt.Println(data.Statement, data.Parameters)
}