|
@ -7,12 +7,16 @@ import ( |
|
|
"log" |
|
|
"log" |
|
|
"net/http" |
|
|
"net/http" |
|
|
"net/http/httptest" |
|
|
"net/http/httptest" |
|
|
|
|
|
"os" |
|
|
|
|
|
"path" |
|
|
|
|
|
"runtime" |
|
|
"strings" |
|
|
"strings" |
|
|
"testing" |
|
|
"testing" |
|
|
|
|
|
|
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database" |
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database" |
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models" |
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models" |
|
|
"github.com/gorilla/mux" |
|
|
"github.com/gorilla/mux" |
|
|
|
|
|
"gorm.io/gorm" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
var ( |
|
|
var ( |
|
@ -20,6 +24,14 @@ var ( |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func init() { |
|
|
func init() { |
|
|
|
|
|
// Fix working directory for tests
|
|
|
|
|
|
_, filename, _, _ := runtime.Caller(0) |
|
|
|
|
|
dir := path.Join(path.Dir(filename), "..") |
|
|
|
|
|
err := os.Chdir(dir) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
panic(err) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
log.SetOutput(ioutil.Discard) |
|
|
log.SetOutput(ioutil.Discard) |
|
|
Database.Init() |
|
|
Database.Init() |
|
|
|
|
|
|
|
@ -27,11 +39,14 @@ func init() { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Test_getPosts(t *testing.T) { |
|
|
func Test_getPosts(t *testing.T) { |
|
|
|
|
|
t.Log("Testing getPosts...") |
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
|
|
|
var err error |
|
|
for i := 0; i < 20; i++ { |
|
|
for i := 0; i < 20; i++ { |
|
|
postData := Models.Post{ |
|
|
postData := Models.Post{ |
|
|
Title: "Test post", |
|
|
Title: "Test post", |
|
@ -41,14 +56,21 @@ func Test_getPosts(t *testing.T) { |
|
|
PostLinks: []Models.PostLink{ |
|
|
PostLinks: []Models.PostLink{ |
|
|
{ |
|
|
{ |
|
|
Type: "Facebook", |
|
|
Type: "Facebook", |
|
|
Link: "http://google.com/", |
|
|
|
|
|
|
|
|
Link: "http://facebook.com/", |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Database.CreatePost(&postData) |
|
|
|
|
|
|
|
|
err = Database.CreatePost(&postData) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
defer Database.DB.Unscoped().Delete(&postData) |
|
|
|
|
|
|
|
|
defer Database.DB. |
|
|
|
|
|
Session(&gorm.Session{FullSaveAssociations: true}). |
|
|
|
|
|
Unscoped(). |
|
|
|
|
|
Select("PostLinks"). |
|
|
|
|
|
Delete(&postData) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
res, err := http.Get(ts.URL + "/post?page=1&pageSize=10") |
|
|
res, err := http.Get(ts.URL + "/post?page=1&pageSize=10") |
|
@ -72,6 +94,8 @@ func Test_getPosts(t *testing.T) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Test_getPost(t *testing.T) { |
|
|
func Test_getPost(t *testing.T) { |
|
|
|
|
|
t.Log("Testing getPost...") |
|
|
|
|
|
|
|
|
r.HandleFunc("/post/{postID}", getPost).Methods("GET") |
|
|
r.HandleFunc("/post/{postID}", getPost).Methods("GET") |
|
|
|
|
|
|
|
|
ts := httptest.NewServer(r) |
|
|
ts := httptest.NewServer(r) |
|
@ -86,13 +110,21 @@ func Test_getPost(t *testing.T) { |
|
|
PostLinks: []Models.PostLink{ |
|
|
PostLinks: []Models.PostLink{ |
|
|
{ |
|
|
{ |
|
|
Type: "Facebook", |
|
|
Type: "Facebook", |
|
|
Link: "http://google.com/", |
|
|
|
|
|
|
|
|
Link: "http://facebook.com/", |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Database.CreatePost(&postData) |
|
|
|
|
|
defer Database.DB.Unscoped().Delete(&postData) |
|
|
|
|
|
|
|
|
err := Database.CreatePost(&postData) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
defer Database.DB. |
|
|
|
|
|
Session(&gorm.Session{FullSaveAssociations: true}). |
|
|
|
|
|
Unscoped(). |
|
|
|
|
|
Select("PostLinks"). |
|
|
|
|
|
Delete(&postData) |
|
|
|
|
|
|
|
|
res, err := http.Get(fmt.Sprintf( |
|
|
res, err := http.Get(fmt.Sprintf( |
|
|
"%s/post/%s", |
|
|
"%s/post/%s", |
|
@ -115,6 +147,8 @@ func Test_getPost(t *testing.T) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Test_createPost(t *testing.T) { |
|
|
func Test_createPost(t *testing.T) { |
|
|
|
|
|
t.Log("Testing createPost...") |
|
|
|
|
|
|
|
|
r.HandleFunc("/post", createPost).Methods("POST") |
|
|
r.HandleFunc("/post", createPost).Methods("POST") |
|
|
|
|
|
|
|
|
ts := httptest.NewServer(r) |
|
|
ts := httptest.NewServer(r) |
|
@ -129,7 +163,7 @@ func Test_createPost(t *testing.T) { |
|
|
"order": 1, |
|
|
"order": 1, |
|
|
"links": [{ |
|
|
"links": [{ |
|
|
"type": "Facebook", |
|
|
"type": "Facebook", |
|
|
"link": "http://google.com/" |
|
|
|
|
|
|
|
|
"link": "http://facebook.com/" |
|
|
}] |
|
|
}] |
|
|
} |
|
|
} |
|
|
` |
|
|
` |
|
@ -148,7 +182,11 @@ 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) |
|
|
|
|
|
|
|
|
defer Database.DB. |
|
|
|
|
|
Session(&gorm.Session{FullSaveAssociations: true}). |
|
|
|
|
|
Unscoped(). |
|
|
|
|
|
Select("PostLinks"). |
|
|
|
|
|
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) |
|
@ -159,6 +197,8 @@ func Test_createPost(t *testing.T) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Test_deletePost(t *testing.T) { |
|
|
func Test_deletePost(t *testing.T) { |
|
|
|
|
|
t.Log("Testing deletePost...") |
|
|
|
|
|
|
|
|
r.HandleFunc("/post/{postID}", deletePost).Methods("DELETE") |
|
|
r.HandleFunc("/post/{postID}", deletePost).Methods("DELETE") |
|
|
|
|
|
|
|
|
ts := httptest.NewServer(r) |
|
|
ts := httptest.NewServer(r) |
|
@ -173,13 +213,21 @@ func Test_deletePost(t *testing.T) { |
|
|
PostLinks: []Models.PostLink{ |
|
|
PostLinks: []Models.PostLink{ |
|
|
{ |
|
|
{ |
|
|
Type: "Facebook", |
|
|
Type: "Facebook", |
|
|
Link: "http://google.com/", |
|
|
|
|
|
|
|
|
Link: "http://facebook.com/", |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Database.CreatePost(&postData) |
|
|
|
|
|
defer Database.DB.Unscoped().Delete(&postData) |
|
|
|
|
|
|
|
|
err := Database.CreatePost(&postData) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
defer Database.DB. |
|
|
|
|
|
Session(&gorm.Session{FullSaveAssociations: true}). |
|
|
|
|
|
Unscoped(). |
|
|
|
|
|
Select("PostLinks"). |
|
|
|
|
|
Delete(&postData) |
|
|
|
|
|
|
|
|
req, err := http.NewRequest("DELETE", fmt.Sprintf( |
|
|
req, err := http.NewRequest("DELETE", fmt.Sprintf( |
|
|
"%s/post/%s", |
|
|
"%s/post/%s", |
|
@ -205,6 +253,8 @@ func Test_deletePost(t *testing.T) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Test_updatePost(t *testing.T) { |
|
|
func Test_updatePost(t *testing.T) { |
|
|
|
|
|
t.Log("Testing updatePost...") |
|
|
|
|
|
|
|
|
r.HandleFunc("/post/{postID}", updatePost).Methods("PUT") |
|
|
r.HandleFunc("/post/{postID}", updatePost).Methods("PUT") |
|
|
|
|
|
|
|
|
ts := httptest.NewServer(r) |
|
|
ts := httptest.NewServer(r) |
|
@ -219,12 +269,15 @@ func Test_updatePost(t *testing.T) { |
|
|
PostLinks: []Models.PostLink{ |
|
|
PostLinks: []Models.PostLink{ |
|
|
{ |
|
|
{ |
|
|
Type: "Facebook", |
|
|
Type: "Facebook", |
|
|
Link: "http://google.com/", |
|
|
|
|
|
|
|
|
Link: "http://facebook.com/", |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Database.CreatePost(&postData) |
|
|
|
|
|
|
|
|
err := Database.CreatePost(&postData) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
postJson := ` |
|
|
postJson := ` |
|
|
{ |
|
|
{ |
|
@ -261,7 +314,11 @@ 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) |
|
|
|
|
|
|
|
|
defer Database.DB. |
|
|
|
|
|
Session(&gorm.Session{FullSaveAssociations: true}). |
|
|
|
|
|
Unscoped(). |
|
|
|
|
|
Select("PostLinks"). |
|
|
|
|
|
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) |
|
|