-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
183 lines (147 loc) · 4.6 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
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
// main_test.go
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"strconv"
"testing"
"github.com/TheStevbeef/communityServiceGo/models"
"github.com/TheStevbeef/communityServiceGo/app"
)
var a app.App
func TestMain(m *testing.M) {
a = app.App{}
a.Initialize(os.Getenv("DB_PATH_TEST"))
ensureTableExists()
code := m.Run()
clearTable()
os.Exit(code)
}
func ensureTableExists() {
if _, err := a.DB.Exec(postTableCreationQuery); err != nil {
log.Fatal(err)
}
if _, err := a.DB.Exec(mediaTableCreationQuery); err != nil {
log.Fatal(err)
}
}
func clearTable() {
a.DB.Exec("DELETE * FROM post")
a.DB.Exec("DELETE * FROM media")
}
const postTableCreationQuery = `
CREATE TABLE IF NOT EXISTS post
(post_id TEXT PRIMARY KEY,
timestamp TEXT,
message TEXT,
user_id)`
const mediaTableCreationQuery = `
CREATE TABLE IF NOT EXISTS media
(content_type TEXT,
url TEXT,
post_id TEXT, FOREIGN KEY(post_id) REFERENCES post(post_id))`
func TestEmptyTable(t *testing.T) {
clearTable()
req, _ := http.NewRequest("GET", "/posts", 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 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 TestGetNonExistentPost(t *testing.T) {
clearTable()
req, _ := http.NewRequest("GET", "/posts/45", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusNotFound, response.Code)
var m map[string]string
json.Unmarshal(response.Body.Bytes(), &m)
if m["error"] != "User not found" {
t.Errorf("Expected the 'error' key of the response to be set to 'User not found'. Got '%s'", m["error"])
}
}
func TestCreatePost(t *testing.T) {
clearTable()
payload := []byte(`{
"user": {
"id": "test_user",
"name": "Kevin",
"image_url": "https://i.pravatar.cc/150?u=abc12345"
},
"message": "123123",
"media":{
"content_type": "image/jpeg",
"url": "https://picsum.photos/id/6/300"
}
}`)
req, _ := http.NewRequest("POST", "/posts", bytes.NewBuffer(payload))
response := executeRequest(req)
checkResponseCode(t, http.StatusCreated, response.Code)
var p models.Post
row := a.DB.QueryRow("Select * FROM media")
if err := row.Scan(&p.Media.Content_type, &p.Media.Url, &p.Post_ID); err != nil {
t.Errorf("Something with the database went wrong")
}
row = a.DB.QueryRow("Select * FROM post")
if err := row.Scan(&p.Post_ID, &p.Timestamp, &p.Message, &p.User.User_ID); err != nil {
t.Errorf("Something with the database went wrong")
}
if p.User.User_ID != "test_user" {
t.Errorf("Expected user name to be 'test_user'. Got '%v'", p.User.User_ID)
}
if p.Message != "123123" {
t.Errorf("Expected Message to be '123123'. Got '%v'", p.Message)
}
if p.Media.Content_type != "image/jpeg" {
t.Errorf("Expected content type to be 'image/jpeg'. Got '%v'", p.Media.Content_type)
}
if p.Media.Url != "https://picsum.photos/id/6/300" {
t.Errorf("Expected media url to be 'https://picsum.photos/id/6/300'. Got '%v'", p.Media.Url)
}
}
func addUsers(count int) {
if count < 1 {
count = 1
}
for i := 0; i < count; i++ {
postStatement := fmt.Sprintf("INSERT INTO post(post_id, timestamp,message, user_id) VALUES('%s', '%s', '%s','%s')", ("post_id_" + strconv.Itoa(i+1)), "2006-01-02T15:04:05Z", ("message_" + strconv.Itoa(i+1)), ("user_id_" + strconv.Itoa(i+1)))
mediaStatement := fmt.Sprintf("INSERT INTO media(content_type, url, post_id) VALUES('%s', '%s', '%s')", ("content_type_" + strconv.Itoa(i+1)), ("url_" + strconv.Itoa(i+1)), ("post_id_" + strconv.Itoa(i+1)))
a.DB.Exec(postStatement)
a.DB.Exec(mediaStatement)
}
}
func TestGetUser(t *testing.T) {
clearTable()
addUsers(1)
req, _ := http.NewRequest("GET", "/posts/post_id_1", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
}
func TestDeleteUser(t *testing.T) {
clearTable()
addUsers(1)
req, _ := http.NewRequest("GET", "/posts/post_id_1", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
req, _ = http.NewRequest("DELETE", "/posts/post_id_1", nil)
response = executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
req, _ = http.NewRequest("GET", "/posts/post_id_1", nil)
response = executeRequest(req)
checkResponseCode(t, http.StatusNotFound, response.Code)
}