From 1f997971a4a4f1b05dae7e4bd43111af0adf59f7 Mon Sep 17 00:00:00 2001 From: Tovi Jaeschke-Rogers Date: Sat, 12 Mar 2022 11:45:34 +1030 Subject: [PATCH] Simplify and clean up Posts_test --- Api/Posts_test.go | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/Api/Posts_test.go b/Api/Posts_test.go index a72e531..2493e80 100644 --- a/Api/Posts_test.go +++ b/Api/Posts_test.go @@ -15,15 +15,21 @@ import ( "github.com/gorilla/mux" ) -func Test_getPosts(t *testing.T) { +var ( + r *mux.Router +) + +func init() { log.SetOutput(ioutil.Discard) Database.Init() - r := mux.NewRouter() + r = mux.NewRouter() +} + +func Test_getPosts(t *testing.T) { r.HandleFunc("/post", getPosts).Methods("GET") ts := httptest.NewServer(r) - defer ts.Close() for i := 0; i < 20; i++ { @@ -66,10 +72,6 @@ func Test_getPosts(t *testing.T) { } func Test_getPost(t *testing.T) { - log.SetOutput(ioutil.Discard) - Database.Init() - - r := mux.NewRouter() r.HandleFunc("/post/{postID}", getPost).Methods("GET") ts := httptest.NewServer(r) @@ -90,6 +92,7 @@ func Test_getPost(t *testing.T) { } Database.CreatePost(&postData) + defer Database.DB.Unscoped().Delete(&postData) res, err := http.Get(fmt.Sprintf( "%s/post/%s", @@ -109,15 +112,9 @@ func Test_getPost(t *testing.T) { if err != nil { t.Errorf("Expected nil, recieved %s", err.Error()) } - - Database.DB.Unscoped().Delete(&postData) } func Test_createPost(t *testing.T) { - log.SetOutput(ioutil.Discard) - Database.Init() - - r := mux.NewRouter() r.HandleFunc("/post", createPost).Methods("POST") ts := httptest.NewServer(r) @@ -151,21 +148,17 @@ func Test_createPost(t *testing.T) { t.Errorf("Expected nil, recieved %s", err.Error()) } + defer Database.DB.Unscoped().Delete(&postData) + if postData.Title != "Test post" { t.Errorf("Expected title \"Test post\", recieved \"%s\"", postData.Title) } if postData.Content != "Test content" { t.Errorf("Expected content \"Test content\", recieved \"%s\"", postData.Content) } - - Database.DB.Unscoped().Delete(&postData) } func Test_deletePost(t *testing.T) { - log.SetOutput(ioutil.Discard) - Database.Init() - - r := mux.NewRouter() r.HandleFunc("/post/{postID}", deletePost).Methods("DELETE") ts := httptest.NewServer(r) @@ -186,6 +179,7 @@ func Test_deletePost(t *testing.T) { } Database.CreatePost(&postData) + defer Database.DB.Unscoped().Delete(&postData) req, err := http.NewRequest("DELETE", fmt.Sprintf( "%s/post/%s", @@ -208,15 +202,9 @@ func Test_deletePost(t *testing.T) { if res.StatusCode != http.StatusOK { t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode) } - - Database.DB.Unscoped().Delete(&postData) } func Test_updatePost(t *testing.T) { - log.SetOutput(ioutil.Discard) - Database.Init() - - r := mux.NewRouter() r.HandleFunc("/post/{postID}", updatePost).Methods("PUT") ts := httptest.NewServer(r) @@ -273,6 +261,8 @@ func Test_updatePost(t *testing.T) { t.Errorf("Expected nil, recieved %s", err.Error()) } + defer Database.DB.Unscoped().Delete(&postData) + if updatePostData.Content != "New test content" { t.Errorf("Expected \"New test content\", recieved %s", updatePostData.Content) } @@ -284,6 +274,4 @@ func Test_updatePost(t *testing.T) { if updatePostData.Order != 2 { t.Errorf("Expected 2, recieved %d", updatePostData.Order) } - - Database.DB.Unscoped().Delete(&postData) }