From 176ba70e5c1e90b9b499c23dddf4305261bd48b3 Mon Sep 17 00:00:00 2001 From: Gergely Brautigam Date: Thu, 4 Feb 2016 20:43:24 +0100 Subject: [PATCH 1/3] Added test for AddCategory basic case. --- views/addViews_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/views/addViews_test.go b/views/addViews_test.go index b8b6069..54ef4d6 100644 --- a/views/addViews_test.go +++ b/views/addViews_test.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "net/http" "net/http/httptest" + "net/url" "os" "testing" ) @@ -33,3 +34,20 @@ func TestUploadedFileHandler(t *testing.T) { t.Errorf("Actual content (%s) did not match expected content (%s)", actualContent, expectedContent) } } + +//TestAddEmptyCategory tests that if the category field is empty it should do nothing +func TestAddEmptyCategory(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(AddCategoryFunc)) + defer ts.Close() + req, err := http.NewRequest("POST", ts.URL, nil) + req.Form, _ = url.ParseQuery("category=") + if err != nil { + t.Errorf("Error occured while constracting request:%s", err) + } + w := httptest.NewRecorder() + AddCategoryFunc(w, req) + body := w.Body.String() + if len(body) != 0 { + t.Error("Body should be empty. Instead contained data: ", body) + } +} From 38d7058e799ed5fc3ce40f2d77ad209df3ff2b41 Mon Sep 17 00:00:00 2001 From: Gergely Brautigam Date: Thu, 4 Feb 2016 20:57:32 +0100 Subject: [PATCH 2/3] Added two new tests for Editing Tasks. --- views/addViews_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/views/addViews_test.go b/views/addViews_test.go index 54ef4d6..abac205 100644 --- a/views/addViews_test.go +++ b/views/addViews_test.go @@ -51,3 +51,35 @@ func TestAddEmptyCategory(t *testing.T) { t.Error("Body should be empty. Instead contained data: ", body) } } + +func TestEditTaskWithWrongMethod(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(EditTaskFunc)) + defer ts.Close() + req, err := http.NewRequest("OPTIONS", ts.URL, nil) + if err != nil { + t.Errorf("Error occured while constracting request:%s", err) + } + w := httptest.NewRecorder() + EditTaskFunc(w, req) + + if w.Code != http.StatusFound && message != "Method not allowed" { + t.Errorf("Message was: %s Return code was: %d. Should have been message: %s return code: %d", message, w.Code, "Method not allowed", http.StatusFound) + } +} + +func TestEditTaskWrongTaskName(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(EditTaskFunc)) + defer ts.Close() + ts.URL = ts.URL + "/edit/invalidID" + req, err := http.NewRequest("GET", ts.URL, nil) + if err != nil { + t.Errorf("Error occured while constracting request:%s", err) + } + w := httptest.NewRecorder() + EditTaskFunc(w, req) + //TODO: Error should be returned as part of the string so that the message can tell + //why there was a problem. + if w.Code != http.StatusBadRequest { + t.Errorf("Actual status: (%d); Expected status:(%d)", w.Code, http.StatusBadRequest) + } +} From 6d40ebfcebe7b74ef73f7b1bc61a72695372bf53 Mon Sep 17 00:00:00 2001 From: Gergely Brautigam Date: Thu, 4 Feb 2016 21:24:19 +0100 Subject: [PATCH 3/3] Form is just a map[string][]string. It is easy to manipulate. But url.ParseQuery can also be used. --- views/addViews_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/views/addViews_test.go b/views/addViews_test.go index abac205..8d53e2c 100644 --- a/views/addViews_test.go +++ b/views/addViews_test.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "net/http" "net/http/httptest" - "net/url" "os" "testing" ) @@ -40,7 +39,9 @@ func TestAddEmptyCategory(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(AddCategoryFunc)) defer ts.Close() req, err := http.NewRequest("POST", ts.URL, nil) - req.Form, _ = url.ParseQuery("category=") + req.Form = make(map[string][]string, 0) + req.Form.Add("category", "") + // req.Form, _ = url.ParseQuery("category=") if err != nil { t.Errorf("Error occured while constracting request:%s", err) }