Working on tests

This commit is contained in:
Andrea Fazzi 2020-01-17 12:11:13 +01:00
parent d85f66d256
commit 7f20441abf
3 changed files with 64 additions and 6 deletions

View file

@ -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() 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 { func (c *ConfigT) ReadAllPattern() PathPattern {
return c.Handlers.PathPatterns[actions[ReadAllLabel]] 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 { func (c *ConfigT) query(model interface{}, format string) url.Values {
values := make(url.Values) values := make(url.Values)

View file

@ -183,8 +183,9 @@ func NewHandlers(config *config.ConfigT, renderer map[string]renderer.Renderer,
return handlers return handlers
} }
func (h *Handlers) NewReadAllRequest(model interface{}, format string) (*http.Request, error) { func (h *Handlers) NewRequest(model interface{}, path string, format string) (*http.Request, error) {
return http.NewRequest("GET", h.Config.ReadAllPath(model, format), nil) 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) { 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 { func (h *Handlers) ReadAll(model interface{}) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) { 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()) h.get(w, r, reflect.ModelNameLowerPlural(model), h.Config.ReadAllPattern())
} }
return http.HandlerFunc(fn) 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 { func (h *Handlers) modelHandler(model string, pattern config.PathPattern) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) { fn := func(w http.ResponseWriter, r *http.Request) {

View file

@ -6,6 +6,7 @@ import (
"log" "log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings"
"testing" "testing"
"time" "time"
@ -151,7 +152,7 @@ func (t *testSuite) BeforeAll() {
} }
func (t *testSuite) TestReadAllContests() { func (t *testSuite) TestReadAllContests() {
req, err := handlers.NewReadAllRequest(&orm.Contest{}, "html") req, err := handlers.NewRequest(&orm.Contest{}, "/contests", "html")
t.Nil(err) t.Nil(err)
req, err = login(req, handlers, "admin", "admin") req, err = login(req, handlers, "admin", "admin")
@ -168,7 +169,45 @@ func (t *testSuite) TestReadAllContests() {
if err != nil { if err != nil {
log.Fatal(err) 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)
} }
} }