From df4f44fde17c6312a41451a19377bfcbb13772d0 Mon Sep 17 00:00:00 2001 From: astaxie Date: Mon, 23 Dec 2013 23:28:31 +0800 Subject: [PATCH 1/6] bee tools add router & test --- apiapp.go | 68 ++++++++++++++++++++++++++++++++++++++++--------------- new.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 110 insertions(+), 21 deletions(-) diff --git a/apiapp.go b/apiapp.go index 87ade63..dcb7b11 100644 --- a/apiapp.go +++ b/apiapp.go @@ -36,9 +36,13 @@ In the appname folder has the follow struct: │ └── app.conf ├── controllers │ └── default.go + ├── routers + │ └── router.go + ├── tests + │ └── default_test.go ├── main.go └── models - └── object.go + └── object.go `, } @@ -53,8 +57,8 @@ copyrequestbody = true var apiMaingo = `package main import ( - "github.com/astaxie/beego" - "{{.Appname}}/controllers" + _ "{{.Appname}}/routes" + "github.com/astaxie/beego" ) // Objects @@ -67,11 +71,21 @@ import ( // /object/ DELETE Deleting Objects func main() { - beego.RESTRouter("/object", &controllers.ObjectController{}) - beego.Router("/ping", &controllers.ObjectController{},"get:Ping") beego.Run() } ` +var apirouter = `package routes + +import ( + "{{.Appname}}/controllers" + "github.com/astaxie/beego" +) + +func init() { + beego.RESTRouter("/object", &controllers.ObjectController{}) +} +` + var apiModels = `package models import ( @@ -192,22 +206,34 @@ func (this *ObjectController) Ping() { ` -var apiTests = `package tests +var apiTests = `package test import ( - "testing" - beetest "github.com/astaxie/beego/testing" - "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + _ "{{.Appname}}/routes" + + "github.com/astaxie/beego" + . "github.com/smartystreets/goconvey/convey" ) -func TestHelloWorld(t *testing.T) { - request:=beetest.Get("/ping") - response,_:=request.Response() - defer response.Body.Close() - contents, _ := ioutil.ReadAll(response.Body) - if string(contents)!="pong"{ - t.Errorf("response sould be pong") - } +// TestGet is a sample to run an endpoint test +func TestGet(t *testing.T) { + r, _ := http.NewRequest("GET", "/", nil) + w := httptest.NewRecorder() + beego.BeeApp.Handlers.ServeHTTP(w, r) + + beego.Trace("testing", "TestGet", "Code[%d]\n%s", w.Code, w.Body.String()) + + Convey("Subject: Test Station Endpoint\n", t, func() { + Convey("Status Code Should Be 200", func() { + So(w.Code, ShouldEqual, 200) + }) + Convey("The Result Should Not Be Empty", func() { + So(w.Body.Len(), ShouldBeGreaterThan, 0) + }) + }) } ` @@ -233,6 +259,8 @@ func createapi(cmd *Command, args []string) { os.Mkdir(path.Join(apppath, "controllers"), 0755) fmt.Println("create controllers:", path.Join(apppath, "controllers")) os.Mkdir(path.Join(apppath, "models"), 0755) + fmt.Println(path.Join(apppath, "routes") + string(path.Separator)) + os.Mkdir(path.Join(apppath, "routes"), 0755) fmt.Println("create models:", path.Join(apppath, "models")) os.Mkdir(path.Join(apppath, "tests"), 0755) fmt.Println("create tests:", path.Join(apppath, "tests")) @@ -247,7 +275,11 @@ func createapi(cmd *Command, args []string) { fmt.Println("create tests default.go:", path.Join(apppath, "tests", "default_test.go")) writetofile(path.Join(apppath, "tests", "default_test.go"), - apiTests) + strings.Replace(apiTests, "{{.Appname}}", packpath, -1)) + + fmt.Println("create routers router.go:", path.Join(apppath, "routers", "router.go")) + writetofile(path.Join(apppath, "routers", "router.go"), + strings.Replace(apirouter, "{{.Appname}}", packpath, -1)) fmt.Println("create models object.go:", path.Join(apppath, "models", "object.go")) writetofile(path.Join(apppath, "models", "object.go"), apiModels) diff --git a/new.go b/new.go index 8d2c1ea..e7e2d0a 100644 --- a/new.go +++ b/new.go @@ -37,7 +37,11 @@ The [appname] folder has following structure: |- controllers |- default.go |- models - |- static + |- routes + |- router.go + |- tests + |- default_test.go + |- static |- js |- css |- img @@ -102,6 +106,10 @@ func createApp(cmd *Command, args []string) { fmt.Println(path.Join(apppath, "controllers") + string(path.Separator)) os.Mkdir(path.Join(apppath, "models"), 0755) fmt.Println(path.Join(apppath, "models") + string(path.Separator)) + os.Mkdir(path.Join(apppath, "routes"), 0755) + fmt.Println(path.Join(apppath, "routes") + string(path.Separator)) + os.Mkdir(path.Join(apppath, "tests"), 0755) + fmt.Println(path.Join(apppath, "tests") + string(path.Separator)) os.Mkdir(path.Join(apppath, "static"), 0755) fmt.Println(path.Join(apppath, "static") + string(path.Separator)) os.Mkdir(path.Join(apppath, "static", "js"), 0755) @@ -121,6 +129,12 @@ func createApp(cmd *Command, args []string) { fmt.Println(path.Join(apppath, "views", "index.tpl")) writetofile(path.Join(apppath, "views", "index.tpl"), indextpl) + fmt.Println(path.Join(apppath, "routers", "router.go")) + writetofile(path.Join(apppath, "routers", "router.go"), strings.Replace(router, "{{.Appname}}", strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/"), -1)) + + fmt.Println(path.Join(apppath, "tests", "default_test.go")) + writetofile(path.Join(apppath, "tests", "default_test.go"), strings.Replace(test, "{{.Appname}}", strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/"), -1)) + fmt.Println(path.Join(apppath, "main.go")) writetofile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/"), -1)) @@ -135,16 +149,59 @@ runmode = dev var maingo = `package main import ( - "{{.Appname}}/controllers" + _ "{{.Appname}}/routes" "github.com/astaxie/beego" ) func main() { - beego.Router("/", &controllers.MainController{}) beego.Run() } ` +var router = `package routes + +import ( + "{{.Appname}}/controllers" + "github.com/astaxie/beego" +) + +func init() { + beego.Router("/", &controllers.MainController{}) +} +` + +var test = `package test + +import ( + "net/http" + "net/http/httptest" + "testing" + _ "{{.Appname}}/routes" + + "github.com/astaxie/beego" + . "github.com/smartystreets/goconvey/convey" +) + +// TestMain is a sample to run an endpoint test +func TestMain(t *testing.T) { + r, _ := http.NewRequest("GET", "/object", nil) + w := httptest.NewRecorder() + beego.BeeApp.Handlers.ServeHTTP(w, r) + + beego.Trace("testing", "TestMain", "Code[%d]\n%s", w.Code, w.Body.String()) + + Convey("Subject: Test Station Endpoint\n", t, func() { + Convey("Status Code Should Be 200", func() { + So(w.Code, ShouldEqual, 200) + }) + Convey("The Result Should Not Be Empty", func() { + So(w.Body.Len(), ShouldBeGreaterThan, 0) + }) + }) +} + +` + var controllers = `package controllers import ( From 88be60060e4094b0a7f57f5edad4ba12889290d8 Mon Sep 17 00:00:00 2001 From: astaxie Date: Mon, 23 Dec 2013 23:32:01 +0800 Subject: [PATCH 2/6] add goconvey --- test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test.go b/test.go index cc2ee95..1377bf2 100644 --- a/test.go +++ b/test.go @@ -19,6 +19,8 @@ import ( "os/exec" path "path/filepath" "time" + + _ "github.com/smartystreets/goconvey/convey" ) var cmdTest = &Command{ From 2ff8323c1189a4f1ef6d0c01aaf5fbdb193d7e49 Mon Sep 17 00:00:00 2001 From: astaxie Date: Mon, 23 Dec 2013 23:45:15 +0800 Subject: [PATCH 3/6] routes fixed to routers --- apiapp.go | 4 ++-- new.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/apiapp.go b/apiapp.go index dcb7b11..655d31b 100644 --- a/apiapp.go +++ b/apiapp.go @@ -259,8 +259,8 @@ func createapi(cmd *Command, args []string) { os.Mkdir(path.Join(apppath, "controllers"), 0755) fmt.Println("create controllers:", path.Join(apppath, "controllers")) os.Mkdir(path.Join(apppath, "models"), 0755) - fmt.Println(path.Join(apppath, "routes") + string(path.Separator)) - os.Mkdir(path.Join(apppath, "routes"), 0755) + fmt.Println(path.Join(apppath, "routers") + string(path.Separator)) + os.Mkdir(path.Join(apppath, "routers"), 0755) fmt.Println("create models:", path.Join(apppath, "models")) os.Mkdir(path.Join(apppath, "tests"), 0755) fmt.Println("create tests:", path.Join(apppath, "tests")) diff --git a/new.go b/new.go index e7e2d0a..e156c0f 100644 --- a/new.go +++ b/new.go @@ -37,7 +37,7 @@ The [appname] folder has following structure: |- controllers |- default.go |- models - |- routes + |- routers |- router.go |- tests |- default_test.go @@ -106,8 +106,8 @@ func createApp(cmd *Command, args []string) { fmt.Println(path.Join(apppath, "controllers") + string(path.Separator)) os.Mkdir(path.Join(apppath, "models"), 0755) fmt.Println(path.Join(apppath, "models") + string(path.Separator)) - os.Mkdir(path.Join(apppath, "routes"), 0755) - fmt.Println(path.Join(apppath, "routes") + string(path.Separator)) + os.Mkdir(path.Join(apppath, "routers"), 0755) + fmt.Println(path.Join(apppath, "routers") + string(path.Separator)) os.Mkdir(path.Join(apppath, "tests"), 0755) fmt.Println(path.Join(apppath, "tests") + string(path.Separator)) os.Mkdir(path.Join(apppath, "static"), 0755) From 34920594295d0540228e146ad62641431a968cbd Mon Sep 17 00:00:00 2001 From: astaxie Date: Tue, 24 Dec 2013 00:00:52 +0800 Subject: [PATCH 4/6] routers --- apiapp.go | 10 +++------- new.go | 6 +++--- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/apiapp.go b/apiapp.go index 655d31b..5b8b161 100644 --- a/apiapp.go +++ b/apiapp.go @@ -57,7 +57,7 @@ copyrequestbody = true var apiMaingo = `package main import ( - _ "{{.Appname}}/routes" + _ "{{.Appname}}/routers" "github.com/astaxie/beego" ) @@ -74,7 +74,7 @@ func main() { beego.Run() } ` -var apirouter = `package routes +var apirouter = `package routers import ( "{{.Appname}}/controllers" @@ -200,10 +200,6 @@ func (this *ObjectController) Delete() { this.ServeJson() } -func (this *ObjectController) Ping() { - this.Ctx.WriteString("pong") -} - ` var apiTests = `package test @@ -212,7 +208,7 @@ import ( "net/http" "net/http/httptest" "testing" - _ "{{.Appname}}/routes" + _ "{{.Appname}}/routers" "github.com/astaxie/beego" . "github.com/smartystreets/goconvey/convey" diff --git a/new.go b/new.go index e156c0f..8543d10 100644 --- a/new.go +++ b/new.go @@ -149,7 +149,7 @@ runmode = dev var maingo = `package main import ( - _ "{{.Appname}}/routes" + _ "{{.Appname}}/routers" "github.com/astaxie/beego" ) @@ -158,7 +158,7 @@ func main() { } ` -var router = `package routes +var router = `package routers import ( "{{.Appname}}/controllers" @@ -176,7 +176,7 @@ import ( "net/http" "net/http/httptest" "testing" - _ "{{.Appname}}/routes" + _ "{{.Appname}}/routers" "github.com/astaxie/beego" . "github.com/smartystreets/goconvey/convey" From b103a8a3a29009bfeb69e913aca6168748489099 Mon Sep 17 00:00:00 2001 From: astaxie Date: Tue, 24 Dec 2013 00:11:13 +0800 Subject: [PATCH 5/6] miss url --- apiapp.go | 2 +- new.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apiapp.go b/apiapp.go index 5b8b161..8af8d00 100644 --- a/apiapp.go +++ b/apiapp.go @@ -216,7 +216,7 @@ import ( // TestGet is a sample to run an endpoint test func TestGet(t *testing.T) { - r, _ := http.NewRequest("GET", "/", nil) + r, _ := http.NewRequest("GET", "/object", nil) w := httptest.NewRecorder() beego.BeeApp.Handlers.ServeHTTP(w, r) diff --git a/new.go b/new.go index 8543d10..09e7457 100644 --- a/new.go +++ b/new.go @@ -184,7 +184,7 @@ import ( // TestMain is a sample to run an endpoint test func TestMain(t *testing.T) { - r, _ := http.NewRequest("GET", "/object", nil) + r, _ := http.NewRequest("GET", "/", nil) w := httptest.NewRecorder() beego.BeeApp.Handlers.ServeHTTP(w, r) From 0844deda674fcfdfb0456a943a0f55ebab5158af Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 24 Dec 2013 17:03:33 -0500 Subject: [PATCH 6/6] UPDATED README --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2573f71..23b3c72 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,8 @@ bee [![Build Status](https://drone.io/github.com/beego/bee/status.png)](https://drone.io/github.com/beego/bee/latest) -Bee is a tool for helping develop with beego app framework. \ No newline at end of file +Bee is a tool for helping develop with beego app framework. + +## Requirements + +- Go version >= 1.1. \ No newline at end of file