Working on tests
This commit is contained in:
parent
d85f66d256
commit
7f20441abf
3 changed files with 64 additions and 6 deletions
|
@ -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)
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue