Skip to content
Snippets Groups Projects
Commit b6445386 authored by Simon Lindblad's avatar Simon Lindblad
Browse files

Comment test cases

parent 05ddf332
No related branches found
No related tags found
No related merge requests found
...@@ -11,12 +11,15 @@ import ( ...@@ -11,12 +11,15 @@ import (
"bytes" "bytes"
) )
// checkFail fails the tests if err is an error (not nil)
func checkFail(t *testing.T, err error) { func checkFail(t *testing.T, err error) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
// setup recreates the database according to schema.sql.
// This is so that all tests occurs from a clean slate
func setup(t *testing.T) { func setup(t *testing.T) {
db := ConnectDb() db := ConnectDb()
defer db.Close() defer db.Close()
...@@ -32,83 +35,86 @@ func setup(t *testing.T) { ...@@ -32,83 +35,86 @@ func setup(t *testing.T) {
} }
// TestTask tests all functionality with regards to tasks
func TestTask(test *testing.T) { func TestTask(test *testing.T) {
// Setup the database and create a test server
setup(test) setup(test)
testServer := httptest.NewServer(Handlers()) testServer := httptest.NewServer(Handlers())
defer testServer.Close() defer testServer.Close()
// Create a new list for the different tasks
b, err := json.Marshal(ListCreateRequest{Name: "Work"}) b, err := json.Marshal(ListCreateRequest{Name: "Work"})
checkFail(test, err) checkFail(test, err)
res, err := http.Post(testServer.URL + "/list", "application/json", bytes.NewReader(b)) res, err := http.Post(testServer.URL + "/list", "application/json", bytes.NewReader(b))
checkFail(test, err) checkFail(test, err)
// Read the response and retrieve the id for the newly created list
listResp := ListCreateResponse{} listResp := ListCreateResponse{}
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
defer res.Body.Close() defer res.Body.Close()
err = json.Unmarshal(body, &listResp) err = json.Unmarshal(body, &listResp)
// Get all the tasks from the new list
res, err = http.Get(testServer.URL + "/list/" + strconv.Itoa(listResp.Id)) res, err = http.Get(testServer.URL + "/list/" + strconv.Itoa(listResp.Id))
checkFail(test, err) checkFail(test, err)
if res.StatusCode != 200 { if res.StatusCode != 200 {
test.Errorf("Expected 200 got %d", res.StatusCode) test.Errorf("Expected 200 got %d", res.StatusCode)
} }
body, err = ioutil.ReadAll(res.Body) body, err = ioutil.ReadAll(res.Body)
defer res.Body.Close() defer res.Body.Close()
var tasks []Task var tasks []Task
err = json.Unmarshal(body, &tasks) err = json.Unmarshal(body, &tasks)
checkFail(test, err) checkFail(test, err)
// There shouldn't be any tasks in the list yet
if len(tasks) != 0 { if len(tasks) != 0 {
test.Errorf("Expected len(tasks) == 0 got %d", len(tasks)) test.Errorf("Expected len(tasks) == 0 got %d", len(tasks))
} }
// Test the creation of a task in the list
b, err = json.Marshal(CreateTaskRequest{Name: "Work", ListId: listResp.Id}) b, err = json.Marshal(CreateTaskRequest{Name: "Work", ListId: listResp.Id})
checkFail(test, err) checkFail(test, err)
res, err = http.Post(testServer.URL + "/task", "application/json", bytes.NewReader(b)) res, err = http.Post(testServer.URL + "/task", "application/json", bytes.NewReader(b))
checkFail(test, err) checkFail(test, err)
if res.StatusCode != 200 { if res.StatusCode != 200 {
test.Errorf("Expected 200, got %d") test.Errorf("Expected 200, got %d")
} }
// Test the if the task got stored in the list
res, err = http.Get(testServer.URL + "/list/" + strconv.Itoa(listResp.Id)) res, err = http.Get(testServer.URL + "/list/" + strconv.Itoa(listResp.Id))
checkFail(test, err) checkFail(test, err)
if res.StatusCode != 200 { if res.StatusCode != 200 {
test.Errorf("Expected 200 got %d", res.StatusCode) test.Errorf("Expected 200 got %d", res.StatusCode)
} }
// Check so that the list contains 1 task
body, err = ioutil.ReadAll(res.Body) body, err = ioutil.ReadAll(res.Body)
defer res.Body.Close() defer res.Body.Close()
err = json.Unmarshal(body, &tasks) err = json.Unmarshal(body, &tasks)
checkFail(test, err) checkFail(test, err)
if len(tasks) != 1 { if len(tasks) != 1 {
test.Errorf("Expected len(tasks) == 1 got %d", len(tasks)) test.Errorf("Expected len(tasks) == 1 got %d", len(tasks))
} }
} }
// TestList manages the testing of lists
func TestList(test *testing.T) { func TestList(test *testing.T) {
// Setup a clean database and a new test server
setup(test) setup(test)
testServer := httptest.NewServer(Handlers()) testServer := httptest.NewServer(Handlers())
defer testServer.Close() defer testServer.Close()
// Retrieve the lists
res, err := http.Get(testServer.URL + "/list") res, err := http.Get(testServer.URL + "/list")
checkFail(test, err) checkFail(test, err)
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if res.StatusCode != 200 { if res.StatusCode != 200 {
test.Errorf("Expected status code 200 got %d", res.StatusCode) test.Errorf("Expected status code 200 got %d", res.StatusCode)
} }
// Check that it didn't contain any lists
var lists []List var lists []List
err = json.Unmarshal(body, &lists) err = json.Unmarshal(body, &lists)
checkFail(test, err) checkFail(test, err)
...@@ -117,9 +123,9 @@ func TestList(test *testing.T) { ...@@ -117,9 +123,9 @@ func TestList(test *testing.T) {
test.Errorf("Expected [] got %s", lists) test.Errorf("Expected [] got %s", lists)
} }
// Test creation of a list
b, err := json.Marshal(ListCreateRequest{Name: "Work"}) b, err := json.Marshal(ListCreateRequest{Name: "Work"})
checkFail(test, err) checkFail(test, err)
res, err = http.Post(testServer.URL + "/list", "application/json", bytes.NewReader(b)) res, err = http.Post(testServer.URL + "/list", "application/json", bytes.NewReader(b))
checkFail(test, err) checkFail(test, err)
...@@ -127,9 +133,9 @@ func TestList(test *testing.T) { ...@@ -127,9 +133,9 @@ func TestList(test *testing.T) {
test.Errorf("Expected status code 200 got %d", res.StatusCode) test.Errorf("Expected status code 200 got %d", res.StatusCode)
} }
// Make sure that the list is stored
res, err = http.Get(testServer.URL + "/list") res, err = http.Get(testServer.URL + "/list")
checkFail(test, err) checkFail(test, err)
body, err = ioutil.ReadAll(res.Body) body, err = ioutil.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
...@@ -139,7 +145,6 @@ func TestList(test *testing.T) { ...@@ -139,7 +145,6 @@ func TestList(test *testing.T) {
err = json.Unmarshal(body, &lists) err = json.Unmarshal(body, &lists)
checkFail(test, err) checkFail(test, err)
if len(lists) != 1 { if len(lists) != 1 {
test.Errorf("Expected [] got %s", lists) test.Errorf("Expected [] got %s", lists)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment