From f9164034dd6f0ae8220ae1ddb1aff8ebea7a89dd Mon Sep 17 00:00:00 2001
From: Andrea Fazzi
Date: Mon, 27 Jan 2020 13:15:51 +0100
Subject: [PATCH] Fix test, improve error handling
---
errors/errors.go | 5 +++++
handlers/handlers.go | 19 ++++++++++++++-----
handlers/handlers_test.go | 19 +++++++++++++------
i18n/i18n.go | 3 +++
orm/participant.go | 6 +-----
orm/school.go | 4 ++--
templates/error_category_exists.html.tpl | 2 +-
templates/error_school_exists.html.tpl | 2 +-
8 files changed, 40 insertions(+), 20 deletions(-)
diff --git a/errors/errors.go b/errors/errors.go
index 77e44dd3..86f63242 100644
--- a/errors/errors.go
+++ b/errors/errors.go
@@ -24,6 +24,11 @@ var (
TemplateName: "error_school_exists",
Err: errors.New(i18n.Errors["schoolExists"]["it"]),
}
+
+ ParticipantExists = &Error{
+ TemplateName: "error_participant_exists",
+ Err: errors.New(i18n.Errors["participantExists"]["it"]),
+ }
CategoryExists = &Error{
TemplateName: "error_category_exists",
diff --git a/handlers/handlers.go b/handlers/handlers.go
index b7d3150f..3acbc1fa 100644
--- a/handlers/handlers.go
+++ b/handlers/handlers.go
@@ -376,16 +376,23 @@ func respondWithError(h *Handlers, w http.ResponseWriter, r *http.Request, err e
}
func (h *Handlers) Create(model interface{}) http.Handler {
- fn := func(w http.ResponseWriter, r *http.Request) {
+ fn := func(w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case "GET":
- h.get(w, r, reflect.ModelNameLowerPlural(model), h.Config.CreatePattern())
+ err := h.get(w, r, reflect.ModelNameLowerPlural(model), h.Config.CreatePattern())
+ if err != nil {
+ return err
+ }
case "POST":
- h.post(w, r, reflect.ModelNameLowerPlural(model), h.Config.CreatePattern())
+ err := h.post(w, r, reflect.ModelNameLowerPlural(model), h.Config.CreatePattern())
+ if err != nil {
+ return err
+ }
}
+ return nil
}
- return http.HandlerFunc(fn)
+ return newRootMiddleware(h, fn)
}
func (h *Handlers) ReadAll(model interface{}) http.Handler {
@@ -493,7 +500,9 @@ func (m *rootMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
// This is where our error handling logic starts.
- log.Printf("An error accured: %v", err) // Log the error.
+ if m.h.Config.LogLevel > config.LOG_LEVEL_OFF {
+ log.Printf("An error accured: %v", err) // Log the error.
+ }
respondWithError(m.h, w, r, err)
}
diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go
index a8421c42..4d4e3faf 100644
--- a/handlers/handlers_test.go
+++ b/handlers/handlers_test.go
@@ -175,7 +175,8 @@ func (t *testSuite) BeforeAll() {
if err != nil {
panic(err)
}
- // conf.LogLevel = config.LOG_LEVEL_OFF
+
+ conf.LogLevel = config.LOG_LEVEL_OFF
// Initialize the ORM
@@ -371,8 +372,10 @@ func (t *testSuite) TestSchoolSubscription() {
t.Equal(http.StatusSeeOther, rr.Code)
- log.Println(rr.Header())
schoolId, err := getIdFromPath(rr.Header()["Location"][0])
+ if err != nil {
+ panic(err)
+ }
if !t.Failed() {
doc, err := goquery.NewDocumentFromReader(rr.Body)
if err != nil {
@@ -425,8 +428,13 @@ func (t *testSuite) TestSchoolSubscription() {
router.ServeHTTP(rr, req)
t.Equal(http.StatusSeeOther, rr.Code)
-
- t.Equal(http.StatusOK, deleteParticipant(1001))
+ if !t.Failed() {
+ participantId, err := getIdFromPath(rr.Header()["Location"][0])
+ if err != nil {
+ panic(err)
+ }
+ t.Equal(http.StatusOK, deleteParticipant(participantId))
+ }
}
@@ -455,12 +463,11 @@ func (t *testSuite) TestUserModifier() {
t.Equal(http.StatusOK, rr.Code)
if !t.Failed() {
- fmt.Println(rr.Body)
doc, err := goquery.NewDocumentFromReader(rr.Body)
if err != nil {
log.Fatal(err)
}
- expected := "admin[administrator]"
+ expected := "subscriber[subscriber]"
ok := false
doc.Find("dd").Each(func(i int, s *goquery.Selection) {
if strings.Contains(s.Text(), expected) {
diff --git a/i18n/i18n.go b/i18n/i18n.go
index 9f22c08e..bd180f68 100644
--- a/i18n/i18n.go
+++ b/i18n/i18n.go
@@ -47,6 +47,9 @@ var (
"schoolExists": map[string]string{
"it": "Una scuola con questo codice meccanografico è già presente nella base dati!",
},
+ "participantExists": map[string]string{
+ "it": "Un partecipante con questo codice fiscale è già presente nella base dati!",
+ },
"contestHasZeroQuestions": map[string]string{
"it": "La gara a cui il partecipante è iscritto non contiene alcuna domanda.",
},
diff --git a/orm/participant.go b/orm/participant.go
index c9f81b12..a1af2ed5 100644
--- a/orm/participant.go
+++ b/orm/participant.go
@@ -203,11 +203,7 @@ func (model *Participant) Create(db *Database, args map[string]string, w http.Re
if err := db._db.Where("user_id = ?", user.ID).Find(&participant).Error; err != nil {
return nil, err
}
- // err := setFlashMessage(w, r, "participantExists")
- // if err != nil {
- // return nil, err
- // }
- return participant, nil
+ return nil, errors.ParticipantExists
} else if err != nil {
return nil, err
}
diff --git a/orm/school.go b/orm/school.go
index 78777406..c564c9a0 100644
--- a/orm/school.go
+++ b/orm/school.go
@@ -58,7 +58,7 @@ type School struct {
Region *Region
User *User
- Participants []*Participant
+ Participants []*Participant `gorm:"PRELOAD:false"`
SelectedRegion map[uint]string `gorm:"-"`
AllRegions []*Region `gorm:"-"`
@@ -211,7 +211,7 @@ func (model *School) Read(db *Database, args map[string]string, w http.ResponseW
return nil, errors.NotAuthorized
}
- if err := db._db.Set("gorm:auto_preload", true).First(&school, id).Error; err != nil {
+ if err := db._db.Set("gorm:auto_preload", true).Preload("Participants").First(&school, id).Error; err != nil {
return nil, err
}
diff --git a/templates/error_category_exists.html.tpl b/templates/error_category_exists.html.tpl
index 81cc52af..07a5883c 100644
--- a/templates/error_category_exists.html.tpl
+++ b/templates/error_category_exists.html.tpl
@@ -5,7 +5,7 @@
Si è verificato un errore durante la creazione o l'aggiornamento di un partecipante: {{.Data}}
- Clicca {{all "Participant"|anchor "qui"}} per tornare all'elenco dei partecipanti.
+ Clicca {{create "Participant"|anchor "qui"}} per tornare al modulo di creazione del partecipante.
{{ end }}
diff --git a/templates/error_school_exists.html.tpl b/templates/error_school_exists.html.tpl
index f9fd3b62..3005bd56 100644
--- a/templates/error_school_exists.html.tpl
+++ b/templates/error_school_exists.html.tpl
@@ -2,7 +2,7 @@
Errore
- Si è verificato un errore durante la creazione o l'aggiornamento di un partecipante: {{.Data}}
+ Si è verificato un errore durante la creazione o l'aggiornamento di una scuola: {{.Data}}
Clicca {{create "School"|anchor "qui"}} per tornare al modulo di iscrizione.