From cb8110cd48edf4445b4679281b207fc5190ac13d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 1 May 2015 16:23:36 +0200 Subject: [PATCH] Refactoring, worked on middleware unit tests, and began thinking about api unit tests, #1921 --- pkg/api/api_test.go | 35 ++++++++++++ pkg/middleware/middleware_test.go | 56 ++++++++++++------- pkg/middleware/session.go | 9 ++- .../sqlstore/migrations/migrations_test.go | 43 +++++++------- pkg/services/sqlstore/org_test.go | 1 - pkg/services/sqlstore/xorm.log | 0 pkg/setting/setting.go | 2 +- 7 files changed, 99 insertions(+), 47 deletions(-) create mode 100644 pkg/api/api_test.go delete mode 100644 pkg/services/sqlstore/xorm.log diff --git a/pkg/api/api_test.go b/pkg/api/api_test.go new file mode 100644 index 00000000000..ea3588b0e67 --- /dev/null +++ b/pkg/api/api_test.go @@ -0,0 +1,35 @@ +package api + +import ( + "testing" +) + +func TestHttpApi(t *testing.T) { + + // Convey("Given the grafana api", t, func() { + // ConveyApiScenario("Can sign up", func(c apiTestContext) { + // c.PostJson() + // So(c.Resp, ShouldEqualJsonApiResponse, "User created and logged in") + // }) + // + // m := macaron.New() + // m.Use(middleware.GetContextHandler()) + // m.Use(middleware.Sessioner(&session.Options{})) + // Register(m) + // + // var context *middleware.Context + // m.Get("/", func(c *middleware.Context) { + // context = c + // }) + // + // resp := httptest.NewRecorder() + // req, err := http.NewRequest("GET", "/", nil) + // So(err, ShouldBeNil) + // + // m.ServeHTTP(resp, req) + // + // Convey("should red 200", func() { + // So(resp.Code, ShouldEqual, 200) + // }) + // }) +} diff --git a/pkg/middleware/middleware_test.go b/pkg/middleware/middleware_test.go index 9be76121718..9bfa73d2df7 100644 --- a/pkg/middleware/middleware_test.go +++ b/pkg/middleware/middleware_test.go @@ -10,31 +10,49 @@ import ( . "github.com/smartystreets/goconvey/convey" ) +type scenarioContext struct { + m *macaron.Macaron + context *Context + resp *httptest.ResponseRecorder +} + +func (sc *scenarioContext) PerformGet(url string) { + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + sc.m.ServeHTTP(sc.resp, req) +} + +type scenarioFunc func(c *scenarioContext) + +func middlewareScenario(desc string, fn scenarioFunc) { + sc := &scenarioContext{} + + sc.m = macaron.New() + sc.m.Use(GetContextHandler()) + // mock out gc goroutine + startSessionGC = func() {} + sc.m.Use(Sessioner(&session.Options{})) + + sc.m.Get("/", func(c *Context) { + sc.context = c + }) + + sc.resp = httptest.NewRecorder() + fn(sc) +} + func TestMiddlewareContext(t *testing.T) { Convey("Given grafana context", t, func() { - m := macaron.New() - m.Use(GetContextHandler()) - m.Use(Sessioner(&session.Options{})) - - var context *Context - - m.Get("/", func(c *Context) { - context = c + middlewareScenario("middleware should add context to injector", func(sc *scenarioContext) { + sc.PerformGet("/") + So(sc.context, ShouldNotBeNil) }) - resp := httptest.NewRecorder() - req, err := http.NewRequest("GET", "/", nil) - So(err, ShouldBeNil) - - m.ServeHTTP(resp, req) - - Convey("Should be able to get grafana context in handlers", func() { - So(context, ShouldNotBeNil) + middlewareScenario("Default middleware should allow get request", func(sc *scenarioContext) { + sc.PerformGet("/") + So(sc.resp.Code, ShouldEqual, 200) }) - Convey("should return 200", func() { - So(resp.Code, ShouldEqual, 200) - }) }) } diff --git a/pkg/middleware/session.go b/pkg/middleware/session.go index c0a12cab781..7b036b9790e 100644 --- a/pkg/middleware/session.go +++ b/pkg/middleware/session.go @@ -17,10 +17,13 @@ const ( var sessionManager *session.Manager var sessionOptions *session.Options +var startSessionGC func() -func startSessionGC() { - sessionManager.GC() - time.AfterFunc(time.Duration(sessionOptions.Gclifetime)*time.Second, startSessionGC) +func init() { + startSessionGC = func() { + sessionManager.GC() + time.AfterFunc(time.Duration(sessionOptions.Gclifetime)*time.Second, startSessionGC) + } } func prepareOptions(opt *session.Options) *session.Options { diff --git a/pkg/services/sqlstore/migrations/migrations_test.go b/pkg/services/sqlstore/migrations/migrations_test.go index d4ba97450f7..0278ea6632b 100644 --- a/pkg/services/sqlstore/migrations/migrations_test.go +++ b/pkg/services/sqlstore/migrations/migrations_test.go @@ -1,12 +1,9 @@ package migrations import ( - "fmt" - "strings" "testing" "github.com/go-xorm/xorm" - "github.com/grafana/grafana/pkg/log" . "github.com/grafana/grafana/pkg/services/sqlstore/migrator" "github.com/grafana/grafana/pkg/services/sqlstore/sqlutil" @@ -16,7 +13,7 @@ import ( var indexTypes = []string{"Unknown", "INDEX", "UNIQUE INDEX"} func TestMigrations(t *testing.T) { - log.NewLogger(0, "console", `{"level": 0}`) + //log.NewLogger(0, "console", `{"level": 0}`) testDBs := []sqlutil.TestDB{ sqlutil.TestDB_Sqlite3, @@ -31,30 +28,30 @@ func TestMigrations(t *testing.T) { sqlutil.CleanDB(x) mg := NewMigrator(x) - mg.LogLevel = log.DEBUG + //mg.LogLevel = log.DEBUG AddMigrations(mg) err = mg.Start() So(err, ShouldBeNil) - tables, err := x.DBMetas() - So(err, ShouldBeNil) - - fmt.Printf("\nDB Schema after migration: table count: %v\n", len(tables)) - - for _, table := range tables { - fmt.Printf("\nTable: %v \n", table.Name) - for _, column := range table.Columns() { - fmt.Printf("\t %v \n", column.String(x.Dialect())) - } - - if len(table.Indexes) > 0 { - fmt.Printf("\n\tIndexes:\n") - for _, index := range table.Indexes { - fmt.Printf("\t %v (%v) %v \n", index.Name, strings.Join(index.Cols, ","), indexTypes[index.Type]) - } - } - } + // tables, err := x.DBMetas() + // So(err, ShouldBeNil) + // + // fmt.Printf("\nDB Schema after migration: table count: %v\n", len(tables)) + // + // for _, table := range tables { + // fmt.Printf("\nTable: %v \n", table.Name) + // for _, column := range table.Columns() { + // fmt.Printf("\t %v \n", column.String(x.Dialect())) + // } + // + // if len(table.Indexes) > 0 { + // fmt.Printf("\n\tIndexes:\n") + // for _, index := range table.Indexes { + // fmt.Printf("\t %v (%v) %v \n", index.Name, strings.Join(index.Cols, ","), indexTypes[index.Type]) + // } + // } + // } }) } } diff --git a/pkg/services/sqlstore/org_test.go b/pkg/services/sqlstore/org_test.go index 7d98da076e0..1b230e349ff 100644 --- a/pkg/services/sqlstore/org_test.go +++ b/pkg/services/sqlstore/org_test.go @@ -10,7 +10,6 @@ import ( ) func TestAccountDataAccess(t *testing.T) { - Convey("Testing Account DB Access", t, func() { InitTestDB(t) diff --git a/pkg/services/sqlstore/xorm.log b/pkg/services/sqlstore/xorm.log deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/pkg/setting/setting.go b/pkg/setting/setting.go index b451aad634f..f3c738af780 100644 --- a/pkg/setting/setting.go +++ b/pkg/setting/setting.go @@ -122,7 +122,7 @@ type CommandLineArgs struct { func init() { IsWindows = runtime.GOOS == "windows" - log.NewLogger(0, "console", `{"level": 0}`) + //log.NewLogger(0, "console", `{"level": 0}`) } func parseAppUrlAndSubUrl(section *ini.Section) (string, string) {