forked from semaphoreci-demos/semaphore-demo-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
147 lines (125 loc) · 2.83 KB
/
main_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
package main
import (
"database/sql"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func createTable() {
connStr := "user=postgres dbname=s2 sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
fmt.Println(err)
}
const query = `
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
first_name TEXT,
last_name TEXT
)`
_, err = db.Exec(query)
if err != nil {
fmt.Println(err)
return
}
db.Close()
}
func dropTable() {
connStr := "user=postgres dbname=s2 sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
fmt.Println(err)
return
}
_, err = db.Exec("DROP TABLE IF EXISTS users")
if err != nil {
fmt.Println(err)
return
}
db.Close()
}
func insertRecord(query string) {
connStr := "user=postgres dbname=s2 sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
fmt.Println(err)
return
}
_, err = db.Exec(query)
if err != nil {
fmt.Println(err)
return
}
db.Close()
}
func Test_count(t *testing.T) {
var count int
createTable()
insertRecord("INSERT INTO users (first_name, last_name) VALUES ('John', 'Doe')")
insertRecord("INSERT INTO users (first_name, last_name) VALUES ('Mihalis', 'Tsoukalos')")
insertRecord("INSERT INTO users (first_name, last_name) VALUES ('Marko', 'Anastasov')")
connStr := "user=postgres dbname=s2 sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
fmt.Println(err)
return
}
row := db.QueryRow("SELECT COUNT(*) FROM users")
err = row.Scan(&count)
db.Close()
if count != 3 {
t.Errorf("Select query returned %d", count)
}
dropTable()
}
func Test_queryDB(t *testing.T) {
createTable()
connStr := "user=postgres dbname=s2 sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
fmt.Println(err)
return
}
query := "INSERT INTO users (first_name, last_name) VALUES ('Random Text', '123456')"
insertRecord(query)
rows, err := db.Query(`SELECT * FROM users WHERE last_name=$1`, `123456`)
if err != nil {
fmt.Println(err)
return
}
var col1 int
var col2 string
var col3 string
for rows.Next() {
rows.Scan(&col1, &col2, &col3)
}
if col2 != "Random Text" {
t.Errorf("first_name returned %s", col2)
}
if col3 != "123456" {
t.Errorf("last_name returned %s", col3)
}
db.Close()
dropTable()
}
func Test_record(t *testing.T) {
createTable()
insertRecord("INSERT INTO users (first_name, last_name) VALUES ('John', 'Doe')")
req, err := http.NewRequest("GET", "/getdata", nil)
if err != nil {
fmt.Println(err)
return
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(getData)
handler.ServeHTTP(rr, req)
status := rr.Code
if status != http.StatusOK {
t.Errorf("Handler returned %v", status)
}
if rr.Body.String() != "<h3 align=\"center\">1, John, Doe</h3>\n" {
t.Errorf("Wrong server response!")
}
dropTable()
}