Browse Source

Simplify and clean up Posts_test

feature/post-fk-cruds
Tovi Jaeschke-Rogers 3 years ago
parent
commit
1f997971a4
1 changed files with 15 additions and 27 deletions
  1. +15
    -27
      Api/Posts_test.go

+ 15
- 27
Api/Posts_test.go View File

@ -15,15 +15,21 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
func Test_getPosts(t *testing.T) {
var (
r *mux.Router
)
func init() {
log.SetOutput(ioutil.Discard) log.SetOutput(ioutil.Discard)
Database.Init() Database.Init()
r := mux.NewRouter()
r = mux.NewRouter()
}
func Test_getPosts(t *testing.T) {
r.HandleFunc("/post", getPosts).Methods("GET") r.HandleFunc("/post", getPosts).Methods("GET")
ts := httptest.NewServer(r) ts := httptest.NewServer(r)
defer ts.Close() defer ts.Close()
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
@ -66,10 +72,6 @@ func Test_getPosts(t *testing.T) {
} }
func Test_getPost(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") r.HandleFunc("/post/{postID}", getPost).Methods("GET")
ts := httptest.NewServer(r) ts := httptest.NewServer(r)
@ -90,6 +92,7 @@ func Test_getPost(t *testing.T) {
} }
Database.CreatePost(&postData) Database.CreatePost(&postData)
defer Database.DB.Unscoped().Delete(&postData)
res, err := http.Get(fmt.Sprintf( res, err := http.Get(fmt.Sprintf(
"%s/post/%s", "%s/post/%s",
@ -109,15 +112,9 @@ func Test_getPost(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("Expected nil, recieved %s", err.Error()) t.Errorf("Expected nil, recieved %s", err.Error())
} }
Database.DB.Unscoped().Delete(&postData)
} }
func Test_createPost(t *testing.T) { func Test_createPost(t *testing.T) {
log.SetOutput(ioutil.Discard)
Database.Init()
r := mux.NewRouter()
r.HandleFunc("/post", createPost).Methods("POST") r.HandleFunc("/post", createPost).Methods("POST")
ts := httptest.NewServer(r) ts := httptest.NewServer(r)
@ -151,21 +148,17 @@ func Test_createPost(t *testing.T) {
t.Errorf("Expected nil, recieved %s", err.Error()) t.Errorf("Expected nil, recieved %s", err.Error())
} }
defer Database.DB.Unscoped().Delete(&postData)
if postData.Title != "Test post" { if postData.Title != "Test post" {
t.Errorf("Expected title \"Test post\", recieved \"%s\"", postData.Title) t.Errorf("Expected title \"Test post\", recieved \"%s\"", postData.Title)
} }
if postData.Content != "Test content" { if postData.Content != "Test content" {
t.Errorf("Expected content \"Test content\", recieved \"%s\"", postData.Content) t.Errorf("Expected content \"Test content\", recieved \"%s\"", postData.Content)
} }
Database.DB.Unscoped().Delete(&postData)
} }
func Test_deletePost(t *testing.T) { func Test_deletePost(t *testing.T) {
log.SetOutput(ioutil.Discard)
Database.Init()
r := mux.NewRouter()
r.HandleFunc("/post/{postID}", deletePost).Methods("DELETE") r.HandleFunc("/post/{postID}", deletePost).Methods("DELETE")
ts := httptest.NewServer(r) ts := httptest.NewServer(r)
@ -186,6 +179,7 @@ func Test_deletePost(t *testing.T) {
} }
Database.CreatePost(&postData) Database.CreatePost(&postData)
defer Database.DB.Unscoped().Delete(&postData)
req, err := http.NewRequest("DELETE", fmt.Sprintf( req, err := http.NewRequest("DELETE", fmt.Sprintf(
"%s/post/%s", "%s/post/%s",
@ -208,15 +202,9 @@ func Test_deletePost(t *testing.T) {
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode) t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode)
} }
Database.DB.Unscoped().Delete(&postData)
} }
func Test_updatePost(t *testing.T) { func Test_updatePost(t *testing.T) {
log.SetOutput(ioutil.Discard)
Database.Init()
r := mux.NewRouter()
r.HandleFunc("/post/{postID}", updatePost).Methods("PUT") r.HandleFunc("/post/{postID}", updatePost).Methods("PUT")
ts := httptest.NewServer(r) ts := httptest.NewServer(r)
@ -273,6 +261,8 @@ func Test_updatePost(t *testing.T) {
t.Errorf("Expected nil, recieved %s", err.Error()) t.Errorf("Expected nil, recieved %s", err.Error())
} }
defer Database.DB.Unscoped().Delete(&postData)
if updatePostData.Content != "New test content" { if updatePostData.Content != "New test content" {
t.Errorf("Expected \"New test content\", recieved %s", updatePostData.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 { if updatePostData.Order != 2 {
t.Errorf("Expected 2, recieved %d", updatePostData.Order) t.Errorf("Expected 2, recieved %d", updatePostData.Order)
} }
Database.DB.Unscoped().Delete(&postData)
} }

Loading…
Cancel
Save