-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
141 lines (125 loc) · 4.04 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
// Note that these tests must be run only when the main server is up and running
// These tests are only to check if the apis are working end-to-end with our current database
// and cache. The tests involving a mock db to only test a part (ex- api layer) has not been done here.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"
)
type Response struct {
ID uint `json:"ID"`
CreatedAt time.Time `json:"CreatedAt"`
UpdatedAt time.Time `json:"UpdatedAt"`
DeletedAt gorm.DeletedAt `json:"DeletedAt"`
TaskId string `json:"taskId"`
TaskDescription string `json:"taskDescription"`
}
func createPostBody(id string, desc string) *bytes.Buffer {
postBody, _ := json.Marshal(map[string]string{
"taskId": id,
"taskDescription": desc})
return bytes.NewBuffer(postBody)
}
func createTodoTask(id string, desc string) {
resp, err := http.Post("http://localhost:8081/todos", "application/json", createPostBody(id, desc))
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
defer resp.Body.Close()
}
func setupSuite() {
createTodoTask("T001", "First Task: Eat breakfast")
}
func deleteTodoTask(id string) {
client := &http.Client{}
// Create request
req, err := http.NewRequest("DELETE", fmt.Sprintf("http://localhost:8081/todo/%s", id), nil)
if err != nil {
fmt.Println(err)
return
}
// Fetch Request
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
}
func tearDownSuite() {
deleteTodoTask("T001")
}
func TestHandleGetTodo(t *testing.T) {
// In the setup suite, we are adding certain todos to the database, so that we can
// demonstrate each of these apis. And in the teardown suite, we remove them from the db.
setupSuite()
defer tearDownSuite()
response, err := http.Get("http://localhost:8081/todo/T001")
if err != nil {
panic("Get request to /todo/taskId failed")
}
body, _ := ioutil.ReadAll(response.Body)
r := new(Response)
json.Unmarshal(body, &r)
assert.Equal(t, response.StatusCode, 200, "Asserting status code")
assert.Equal(t, r.TaskId, "T001", "Asserting taskId")
}
func TestHandleGetAllTodos(t *testing.T) {
setupSuite()
defer tearDownSuite()
response, err := http.Get("http://localhost:8081/todos")
if err != nil {
panic("Get request to /todos failed")
}
body, _ := ioutil.ReadAll(response.Body)
var r []Response
json.Unmarshal(body, &r)
found := false
for _, resp := range r {
if resp.TaskId == "T001" {
found = true
}
}
assert.Equal(t, response.StatusCode, 200, "Asserting status code")
assert.Equal(t, found, true, "Asserting taskId present in list of task elements")
}
func TestHandleCreateTodo(t *testing.T) {
response, err := http.Post("http://localhost:8081/todos", "application/json", createPostBody("T003", "Get an internship!"))
if err != nil {
panic("Post request to /todos failed")
}
body, _ := ioutil.ReadAll(response.Body)
r := new(Response)
json.Unmarshal(body, &r)
assert.Equal(t, response.StatusCode, 200, "Asserting status code")
assert.Equal(t, r.TaskId, "T003", "Asserting taskId")
}
func TestHandleUpdateTodo(t *testing.T) {
setupSuite()
defer tearDownSuite()
client := &http.Client{}
req, _ := http.NewRequest(http.MethodPut, "http://localhost:8081/todo/T001", createPostBody("T001", "Updated task"))
resp, _ := client.Do(req)
body, _ := ioutil.ReadAll(resp.Body)
r := new(Response)
json.Unmarshal(body, &r)
assert.Equal(t, resp.StatusCode, 200, "Asserting status code")
assert.Equal(t, r.TaskDescription, "Updated task", "Asserting taskDescription")
}
func TestHandleDeleteTodo(t *testing.T) {
setupSuite()
client := &http.Client{}
req, _ := http.NewRequest("DELETE", fmt.Sprintf("http://localhost:8081/todo/T001"), nil)
resp, _ := client.Do(req)
body, _ := ioutil.ReadAll(resp.Body)
assert.Equal(t, resp.StatusCode, 200, "Asserting status code")
assert.Equal(t, string(body), "\"The user with taskId T001 is deleted\"\n", "Asserting response body")
}