package test
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/session"
"net/http"
"net/url"
"reflect"
"testing"
"youApp/controllers"
)
func prepareController(c *beego.Controller) {
c.Ctx = &context.Context{
Request: &http.Request{URL: &url.URL{Scheme: "http", Host: "localhost", Path: "/"}},
ResponseWriter: &fakeResponseWriter{},
}
c.Ctx.Output = &context.BeegoOutput{Context: c.Ctx}
c.Ctx.Input = &context.BeegoInput{Request: c.Ctx.Request}
globalSessions, _ := session.NewManager("memory", `{"cookieName":"gosessionid","gclifetime":10}`)
c.Ctx.Request.Header = http.Header{}
c.Ctx.Request.AddCookie(&http.Cookie{Name: "gosessionid", Value: "test"})
c.CruSession = globalSessions.SessionRegenerateId(c.Ctx.ResponseWriter, c.Ctx.Request)
c.Data = map[interface{}]interface{}{}
}
func TestRecomputeBanlance(t *testing.T) {
c := &controllers.BanlanceController{}
prepareController(&(c.Controller))
c.Ctx.Request.Form = url.Values{
"range": []string{"2016-10-01到2016-10-31"},
"city": []string{"北京"},
}
c.SetSession("login", "123")
c.Prepare()
c.Recompute()
j := c.Data["json"]
mapV := reflect.ValueOf(j)
errV := mapV.MapIndex(reflect.ValueOf("Error"))
if errV.IsValid() && errV.String() != "" {
t.Fatal("has error:", errV)
}
banV := mapV.MapIndex(reflect.ValueOf("ban"))
if !banV.IsValid() {
t.Fatal("no output data!")
} else {
V := reflect.ValueOf(banV.Interface())
fnum := V.NumField()
if fnum < 10 {
t.Fatal("not ban table format")
}
v1 := V.FieldByName("Value1").Float()
v2 := V.FieldByName("Value2").Float()
v3 := V.FieldByName("Value3").Float()
v4 := V.FieldByName("Value4").Float()
if !(v1 > 0 &&
v2 > 0 &&
v3 > 0 &&
v4 > 0) {
t.Fatal("ban table data wrong:", v1, v2, v3, v4)
}
}
}
type fakeResponseWriter struct{}
func (f *fakeResponseWriter) Header() http.Header {
return http.Header{}
}
func (f *fakeResponseWriter) Write(b []byte) (int, error) {
return 0, nil
}
func (f *fakeResponseWriter) WriteHeader(n int) {}