From 7f20441abf0ba08b852050610ca92019125acb8d Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Fri, 17 Jan 2020 12:11:13 +0100 Subject: [PATCH] Working on tests --- config/config.go | 12 +++++++++++ handlers/handlers.go | 15 ++++++++++---- handlers/handlers_test.go | 43 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/config/config.go b/config/config.go index 779d79f9..abc6841e 100644 --- a/config/config.go +++ b/config/config.go @@ -134,10 +134,22 @@ func (c *ConfigT) ReadAllPath(model interface{}, format string) string { return fmt.Sprintf(c.Handlers.PathPatterns[actions[ReadAllLabel]].PathPattern, reflect.ModelNameLowerPlural(model)) + "?" + c.query(model, format).Encode() } +func (c *ConfigT) ReadPath(model interface{}, format string) string { + return fmt.Sprintf(c.Handlers.PathPatterns[actions[ReadLabel]].PathPattern, reflect.ModelNameLowerPlural(model)) + "?" + c.query(model, format).Encode() +} + func (c *ConfigT) ReadAllPattern() PathPattern { return c.Handlers.PathPatterns[actions[ReadAllLabel]] } +func (c *ConfigT) ReadPattern() PathPattern { + return c.Handlers.PathPatterns[actions[ReadLabel]] +} + +func (c *ConfigT) PathWithQueryParams(model interface{}, path, format string) string { + return path + "?" + c.query(model, format).Encode() +} + func (c *ConfigT) query(model interface{}, format string) url.Values { values := make(url.Values) diff --git a/handlers/handlers.go b/handlers/handlers.go index a7b87a93..aa1ceab1 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -183,8 +183,9 @@ func NewHandlers(config *config.ConfigT, renderer map[string]renderer.Renderer, return handlers } -func (h *Handlers) NewReadAllRequest(model interface{}, format string) (*http.Request, error) { - return http.NewRequest("GET", h.Config.ReadAllPath(model, format), nil) +func (h *Handlers) NewRequest(model interface{}, path string, format string) (*http.Request, error) { + log.Print(h.Config.PathWithQueryParams(model, path, format)) + return http.NewRequest("GET", h.Config.PathWithQueryParams(model, path, format), nil) } func (h *Handlers) onError(w http.ResponseWriter, r *http.Request, err string) { @@ -350,14 +351,20 @@ func respondWithError(h *Handlers, w http.ResponseWriter, r *http.Request, err e func (h *Handlers) ReadAll(model interface{}) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { - // Replace "api" prefix - // pattern.PathPattern = strings.Replace(pattern.PathPattern, "/api", "", -1) h.get(w, r, reflect.ModelNameLowerPlural(model), h.Config.ReadAllPattern()) } return http.HandlerFunc(fn) } +func (h *Handlers) Read(model interface{}) http.Handler { + fn := func(w http.ResponseWriter, r *http.Request) { + h.get(w, r, reflect.ModelNameLowerPlural(model), h.Config.ReadPattern()) + } + + return http.HandlerFunc(fn) +} + func (h *Handlers) modelHandler(model string, pattern config.PathPattern) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go index 2c7946db..3fcce387 100644 --- a/handlers/handlers_test.go +++ b/handlers/handlers_test.go @@ -6,6 +6,7 @@ import ( "log" "net/http" "net/http/httptest" + "strings" "testing" "time" @@ -151,7 +152,7 @@ func (t *testSuite) BeforeAll() { } func (t *testSuite) TestReadAllContests() { - req, err := handlers.NewReadAllRequest(&orm.Contest{}, "html") + req, err := handlers.NewRequest(&orm.Contest{}, "/contests", "html") t.Nil(err) req, err = login(req, handlers, "admin", "admin") @@ -168,7 +169,45 @@ func (t *testSuite) TestReadAllContests() { if err != nil { log.Fatal(err) } - t.Equal(2, doc.Find(".list-group-item").Size()) + expected := []string{"JUNIOR Contest", "SENIOR Contest"} + ok := true + doc.Find(".list-group-item").Each(func(i int, s *goquery.Selection) { + if !strings.Contains(s.Text(), expected[i]) { + ok = false + } + }) + t.True(ok) + } + } + +} + +func (t *testSuite) TestReadContest() { + req, err := handlers.NewRequest(&orm.Contest{}, "/contests/1", "html") + t.Nil(err) + + req, err = login(req, handlers, "admin", "admin") + t.Nil(err) + + if !t.Failed() { + rr := httptest.NewRecorder() + + handlers.Read(&orm.Contest{}).ServeHTTP(rr, req) + t.Equal(http.StatusOK, rr.Code) + + if !t.Failed() { + doc, err := goquery.NewDocumentFromReader(rr.Body) + if err != nil { + log.Fatal(err) + } + expected := "JUNIOR Contest" + ok := true + doc.Find(".list-group-item").Each(func(i int, s *goquery.Selection) { + if !strings.Contains(s.Text(), expected) { + ok = false + } + }) + t.True(ok) } }