-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
91 lines (74 loc) · 1.66 KB
/
server_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
package main
import (
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/Joematpal/phone_book_golang_api/v1"
"github.com/gorilla/mux"
)
func TestMain(m *testing.M) {
host := os.Getenv("TEST_DB_HOST")
port := os.Getenv("TEST_DB_PORT")
username := os.Getenv("TEST_DB_USERNAME")
password := os.Getenv("TEST_DB_PASSWORD")
dbname := os.Getenv("TEST_DB_NAME")
router := mux.NewRouter()
v = v1.V1{}
v.Initialize(
host,
port,
username,
password,
dbname,
router,
)
// ensureTableExists()
code := m.Run()
// clearTable()
os.Exit(code)
}
func TestTableExists(t *testing.T) {
ensureTableExists()
}
// func TestEmptyTable(t *testing.T) {
// clearTable()
//
// req, _ := http.NewRequest("GET", "/person", nil)
// response := executeRequest(req)
//
// checkResponseCode(t, http.StatusOK, response.Code)
//
// if body := response.Body.String(); body != "[]" {
// t.Errorf("Expected an empty array. Got %s", body)
// }
// }
func checkResponseCode(t *testing.T, expected, actual int) {
if expected != actual {
t.Errorf("Expected response code %d. Got %d\n", expected, actual)
}
}
func ensureTableExists() {
if _, err := v.DB.Exec(tableCreationQuery); err != nil {
log.Fatal(err)
}
}
func clearTable() {
v.DB.Exec("DELETE FROM contact")
}
func executeRequest(req *http.Request) *httptest.ResponseRecorder {
rr := httptest.NewRecorder()
// v1.Router.ServeHTTP(rr, req)
return rr
}
const tableCreationQuery = `CREATE TABLE IF NOT EXISTS contacts
(
id VARCHAR(64),
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(50),
phone VARCHAR(50),
CONSTRAINT contact_pkey PRIMARY KEY (id),
CONSTRAINT email_id UNIQUE("email")
)`