-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
executable file
·168 lines (130 loc) · 3.48 KB
/
main_test.go
File metadata and controls
executable file
·168 lines (130 loc) · 3.48 KB
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
package main
import (
"bytes"
"encoding/json"
"errors"
"github.com/joho/godotenv"
"github.com/suvidsahay/Factly/controllers"
"github.com/suvidsahay/Factly/types"
"log"
"net/http"
"net/http/httptest"
"os"
"strconv"
"testing"
)
var a controllers.App
func TestMain(m *testing.M) {
if err := godotenv.Load(); err != nil {
log.Fatal("Error loading .env file")
}
a.Initialize(
os.Getenv("DB_HOST"),
os.Getenv("DB_PORT"),
os.Getenv("DB_USER"),
os.Getenv("DB_PASSWORD"),
os.Getenv("DB_NAME"),
)
ensureTableExists()
code := m.Run()
clearTable()
os.Exit(code)
}
func addUsers(count int) {
if count < 1 {
count = 1
}
for i := 0; i < count; i++ {
a.DB.Exec("INSERT INTO userdb(name) VALUES($1)", "User "+strconv.Itoa(i))
}
}
func ensureTableExists() {
if _, err := a.DB.Exec("CREATE TABLE IF NOT EXISTS userdb (userid SERIAL, name text NOT NULL, PRIMARY KEY(userid))"); err != nil {
log.Fatal(err)
}
}
func clearTable() {
a.DB.Exec("DELETE FROM userdb")
a.DB.Exec("ALTER SEQUENCE userdb_userid_seq RESTART WITH 1")
}
func getOriginalUser(id int) (types.User, error) {
rows, err := a.DB.Query("SELECT * FROM userdb WHERE userid = $1", id)
if err != nil {
log.Fatal(err)
}
var user types.User
defer rows.Close()
for rows.Next() {
err := rows.Scan(&user.ID, &user.Name)
if err != nil {
return types.User{}, err
}
return user, nil
}
return types.User{}, errors.New("no data found")
}
func executeRequest(req *http.Request) *httptest.ResponseRecorder {
rr := httptest.NewRecorder()
a.Router.ServeHTTP(rr, req)
return rr
}
func checkResponseCode(t *testing.T, expected, actual int) {
if expected != actual {
t.Errorf("Expected response code %d. Got %d\n", expected, actual)
}
}
func TestEmptyTable(t *testing.T) {
clearTable()
req, _ := http.NewRequest("GET", "/users", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
body := response.Body.String()
if body != "[]\n" {
t.Errorf("Expected an empty array. Got %s", body)
}
}
func TestCreateUser(t *testing.T) {
clearTable()
var jsonStr = []byte(`{"name":"test user"}`)
req, _ := http.NewRequest("POST", "/user", bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
response := executeRequest(req)
checkResponseCode(t, http.StatusCreated, response.Code)
var m types.User
json.Unmarshal(response.Body.Bytes(), &m)
if m.Name != "test user" {
t.Errorf("Expected user name to be 'test user'. Got '%v'", m.Name)
}
if m.ID != 1.0 {
t.Errorf("Expected user ID to be '1'. Got '%v'", m.ID)
}
}
func TestUpdateUser(t *testing.T) {
clearTable()
addUsers(1)
var jsonStr = []byte(`{"name":"updated name"}`)
req, _ := http.NewRequest("PUT", "/user/1", bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
var m types.User
json.Unmarshal(response.Body.Bytes(), &m)
var originalUser types.User
originalUser, err := getOriginalUser(1)
if err != nil {
log.Fatal(err)
}
if m.ID != originalUser.ID {
t.Errorf("Expected the id to remain the same (%v). Got %v", originalUser.ID, m.ID)
}
if m.Name != originalUser.Name {
t.Errorf("Expected the name to change from '%v' to '%v'. Got '%v'", originalUser.Name, m.Name, m.Name)
}
}
func TestDeleteUser(t *testing.T) {
clearTable()
addUsers(1)
req, _ := http.NewRequest("DELETE", "/user/1", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
}