Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 660 Update match is slow #774

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions controllers/tournaments/match_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package tournaments

import (
"fmt"
"net/http"
"testing"

"github.com/taironas/gonawin/helpers/handlers"
"github.com/taironas/gonawin/test"
"github.com/taironas/route"

"appengine/aetest"
)

// UpdateMatchResult tests that result is properly updated.
//
func TestUpdateMatchResult(t *testing.T) {
var i aetest.Instance
var err error
options := aetest.Options{StronglyConsistentDatastore: true}

if i, err = aetest.NewInstance(&options); err != nil {
t.Fatal(err)
}
defer i.Close()

var c aetest.Context

if c, err = aetest.NewContext(&options); err != nil {
t.Fatal(err)
}
defer c.Close()

// Create 5 users
users := gonawintest.CreateUsers(c, 5)

// Create a Tournament
tournament := gonawintest.CreateWorldCup(c, users[0].Id)

// Add 5 users to the Tournament and place a bet on a match
for i, u := range users {
if err = tournament.Join(c, u); err != nil {
t.Errorf("Error: %v", err)
}

if err = gonawintest.PredictOnMatch(c, u, tournament.Matches1stStage[0], int64(i), int64(i+1)); err != nil {
t.Errorf("Error: %v", err)
}
}

r := new(route.Router)
r.HandleFunc("/j/tournaments/:tournamentId/matches/:matchId/update", handlers.ErrorHandler(gonawintest.TestingAuthorized(UpdateMatchResult)))

test := gonawintest.GenerateHandlerFunc(t, UpdateMatchResult)

var parameters map[string]string
parameters = make(map[string]string)

parameters["result"] = "1 0"

path := fmt.Sprintf("/j/tournaments/%d/matches/%d/update", tournament.Id, tournament.Matches1stStage[0])

recorder := test(path, "POST", parameters, r)
if recorder.Code != http.StatusOK {
t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
}
}
4 changes: 3 additions & 1 deletion helpers/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ func init() {
var err error
if config, err = gwconfig.ReadConfig(""); err != nil {
golog.Printf("Error: unable to read config file; %v", err)
KOfflineMode = true
} else {
KOfflineMode = config.OfflineMode
}
KOfflineMode = config.OfflineMode
}

type UserInfo struct {
Expand Down
25 changes: 25 additions & 0 deletions test/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gonawintest

import (
"fmt"
"math/rand"
"strings"
)

var firstNames = [...]string{"Jon", "Robb", "Cersei", "Daenerys", "Ned", "Tyrion", "Stannis"}
var lastNames = [...]string{"Stark", "Targaryen", "Lannister", "Baratheon"}

func generateNames() (firstName string, lastName string) {
firstName = firstNames[rand.Int()%len(firstNames)]
lastName = lastNames[rand.Int()%len(lastNames)]

return firstName, lastName
}

func generateUsername(firstName, lastName string) string {
return fmt.Sprintf("%c%s", strings.ToLower(firstName)[0], strings.ToLower(lastName))
}

func generateEmail(firstName, lastName string) string {
return fmt.Sprintf("%s.%[email protected]", firstName, lastName)
}
46 changes: 46 additions & 0 deletions test/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package gonawintest

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/taironas/route"

"github.com/taironas/gonawin/helpers/handlers"
"github.com/taironas/gonawin/models"
)

// HandlerFunc type returns an httptest.ResponseRecorder when you pass the following params:
// url: the url you want to query.
// params: the parameters that the url should use.
// r: the router to record the httptest.ResponseRecorder.
type HandlerFunc func(url, method string, params map[string]string, r *route.Router) *httptest.ResponseRecorder

// GenerateHandlerFunc returns a HandlerFunc type based on the handler passed as argument.
func GenerateHandlerFunc(t *testing.T, handler func(http.ResponseWriter, *http.Request, *models.User) error) HandlerFunc {

return func(url, method string, params map[string]string, r *route.Router) *httptest.ResponseRecorder {
if req, err := http.NewRequest(method, url, nil); err != nil {
t.Errorf("%v", err)
} else {
values := req.URL.Query()
for k, v := range params {
values.Add(k, v)
}
req.URL.RawQuery = values.Encode()
req.Header.Set("Content-Type", "application/json")
recorder := httptest.NewRecorder()
r.ServeHTTP(recorder, req)
return recorder
}
return nil
}
}

// TestingAuthorized runs the function pass by parameter
func TestingAuthorized(f func(w http.ResponseWriter, r *http.Request, u *models.User) error) handlers.ErrorHandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
return f(w, r, nil)
}
}
30 changes: 30 additions & 0 deletions test/tournament.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package gonawintest

import (
"appengine/aetest"

mdl "github.com/taironas/gonawin/models"
)

// CreateWorldCup creates a world cup tournament into the datastore
func CreateWorldCup(c aetest.Context, adminID int64) *mdl.Tournament {
tournament, _ := mdl.CreateWorldCup(c, adminID)

return tournament
}

// PredictOnMatch add a predict to given match and for a given user
func PredictOnMatch(c aetest.Context, user *mdl.User, matchID, result1, result2 int64) error {
var predict *mdl.Predict
var err error

if predict, err = mdl.CreatePredict(c, user.Id, result1, result2, matchID); err != nil {
return err
}
// add p.Id to User predict table.
if err = user.AddPredictId(c, predict.Id); err != nil {
return err
}

return nil
}
25 changes: 25 additions & 0 deletions test/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gonawintest

import (
"appengine/aetest"

mdl "github.com/taironas/gonawin/models"
)

// CreateUsers creates n users into the datastore
func CreateUsers(c aetest.Context, count int64) []*mdl.User {
var users []*mdl.User

var i int64
for i = 0; i < count; i++ {
firstName, lastName := generateNames()
username := generateUsername(firstName, lastName)
email := generateEmail(firstName, lastName)

if user, _ := mdl.CreateUser(c, email, username, firstName+" "+lastName, "", false, ""); user != nil {
users = append(users, user)
}
}

return users
}