|  | @ -7,25 +7,47 @@ 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" | 
														
													
														
															
																|  |  | ) |  |  | ) | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  | func Test_getPosts(t *testing.T) { |  |  |  | 
														
													
														
															
																|  |  |  |  |  | var ( | 
														
													
														
															
																|  |  |  |  |  | r *mux.Router | 
														
													
														
															
																|  |  |  |  |  | ) | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  |  |  |  | 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() | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  | r := mux.NewRouter() |  |  |  | 
														
													
														
															
																|  |  |  |  |  | r = mux.NewRouter() | 
														
													
														
															
																|  |  |  |  |  | } | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  |  |  |  | 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", | 
														
													
												
													
														
															
																|  | @ -35,14 +57,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") | 
														
													
												
													
														
															
																|  | @ -66,10 +95,8 @@ func Test_getPosts(t *testing.T) { | 
														
													
														
															
																|  |  | } |  |  | } | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  | func Test_getPost(t *testing.T) { |  |  | func Test_getPost(t *testing.T) { | 
														
													
														
															
																|  |  | log.SetOutput(ioutil.Discard) |  |  |  | 
														
													
														
															
																|  |  | Database.Init() |  |  |  | 
														
													
														
															
																|  |  |  |  |  | t.Log("Testing getPost...") | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  | 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) | 
														
													
												
													
														
															
																|  | @ -84,12 +111,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) |  |  |  | 
														
													
														
															
																|  |  |  |  |  | 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", | 
														
													
												
													
														
															
																|  | @ -109,15 +145,11 @@ 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() |  |  |  | 
														
													
														
															
																|  |  |  |  |  | t.Log("Testing createPost...") | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  | r := mux.NewRouter() |  |  |  | 
														
													
														
															
																|  |  | r.HandleFunc("/post", createPost).Methods("POST") |  |  | r.HandleFunc("/post", createPost).Methods("POST") | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  | ts := httptest.NewServer(r) |  |  | ts := httptest.NewServer(r) | 
														
													
												
													
														
															
																|  | @ -132,7 +164,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/" | 
														
													
														
															
																|  |  | }] |  |  | }] | 
														
													
														
															
																|  |  | } |  |  | } | 
														
													
														
															
																|  |  | ` |  |  | ` | 
														
													
												
													
														
															
																|  | @ -151,21 +183,23 @@ 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. | 
														
													
														
															
																|  |  |  |  |  | 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) | 
														
													
														
															
																|  |  | } |  |  | } | 
														
													
														
															
																|  |  | 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() |  |  |  | 
														
													
														
															
																|  |  |  |  |  | t.Log("Testing deletePost...") | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  | 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) | 
														
													
												
													
														
															
																|  | @ -180,12 +214,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) |  |  |  | 
														
													
														
															
																|  |  |  |  |  | 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", | 
														
													
												
													
														
															
																|  | @ -208,15 +251,11 @@ 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() |  |  |  | 
														
													
														
															
																|  |  |  |  |  | t.Log("Testing updatePost...") | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  | 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) | 
														
													
												
													
														
															
																|  | @ -231,12 +270,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 := ` | 
														
													
														
															
																|  |  | { |  |  | { | 
														
													
												
													
														
															
																|  | @ -273,6 +315,12 @@ 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. | 
														
													
														
															
																|  |  |  |  |  | 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) | 
														
													
														
															
																|  |  | } |  |  | } | 
														
													
												
													
														
															
																|  | @ -284,6 +332,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) |  |  |  | 
														
													
														
															
																|  |  | } |  |  | } |