Skip to content
Snippets Groups Projects
Commit 851ad1d0 authored by Sercan Yesildal's avatar Sercan Yesildal
Browse files

add tests

parent 14b416ba
Branches main
No related tags found
No related merge requests found
......@@ -12,7 +12,7 @@ import (
var DB *gorm.DB
func init() {
func Init() {
// init database
dsn := fmt.Sprintf("root:root@tcp(%s)/myaktion?charset=utf8&parseTime=True&loc=Local", os.Getenv("DB_CONNECT"))
log.Info("Using DSN for DB:", dsn)
......
......@@ -13,7 +13,7 @@ func getCampaign(r *http.Request) (*model.Campaign, error) {
var campaign model.Campaign
err := json.NewDecoder(r.Body).Decode(&campaign)
if err != nil {
log.Errorf("Can't serialize request body to campaign struct: %v %v", err)
log.Errorf("Can't serialize request body to campaign struct: %v", err)
return nil, err
}
return &campaign, nil
......@@ -72,7 +72,7 @@ func UpdateCampaign(w http.ResponseWriter, r *http.Request) {
}
campaign, err = service.UpdateCampaign(id, campaign)
if err != nil {
log.Errorf("Error updating campaign with ID dd: %v", id, err)
log.Errorf("Error updating campaign with ID %d: %v", id, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
......
package handler
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestCreateCampaign(t *testing.T) {
rr := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/campaigns", nil)
handler := http.HandlerFunc(CreateCampaign)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("handler returned wrong status code: got %v expected %v", status, http.StatusBadRequest)
}
}
package handler
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHealth(t *testing.T) {
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/health", nil)
handler := http.HandlerFunc(Health)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
expected := `{"alive": true}`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
}
......@@ -7,12 +7,16 @@ import (
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"gitlab.reutlingen-university.de/yesildas/myaktion-go/src/myaktion/db"
"gitlab.reutlingen-university.de/yesildas/myaktion-go/src/myaktion/handler"
"gitlab.reutlingen-university.de/yesildas/myaktion-go/src/myaktion/model"
"gitlab.reutlingen-university.de/yesildas/myaktion-go/src/myaktion/service"
)
func init() {
// init database
defer db.Init()
// init logger
log.SetFormatter(&log.TextFormatter{})
log.SetReportCaller(true)
......
......@@ -36,7 +36,7 @@ func GetCampaign(id uint) (*model.Campaign, error) {
return nil, result.Error
}
log.Infof("Successfully retrieved campaign with ID %v.", campaign.ID)
log.Tracef("Retrieved: ", campaign)
log.Tracef("Retrieved: %v", campaign)
return &campaign, nil
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment