作者 王健

新增【公司 http and test code】

package controllers
import (
"github.com/linmadan/egglib-go/web/beego"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/company/command"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/company/query"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/company/service"
)
type CompanyController struct {
beego.BaseController
}
func (controller *CompanyController) CreateCompany() {
companyService := service.NewCompanyService(nil)
createCompanyCommand := &command.CreateCompanyCommand{}
controller.Unmarshal(createCompanyCommand)
data, err := companyService.CreateCompany(createCompanyCommand)
controller.Response(data, err)
}
func (controller *CompanyController) UpdateCompany() {
companyService := service.NewCompanyService(nil)
updateCompanyCommand := &command.UpdateCompanyCommand{}
controller.Unmarshal(updateCompanyCommand)
companyId, _ := controller.GetInt64(":companyId")
updateCompanyCommand.CompanyId = companyId
data, err := companyService.UpdateCompany(updateCompanyCommand)
controller.Response(data, err)
}
func (controller *CompanyController) GetCompany() {
companyService := service.NewCompanyService(nil)
getCompanyQuery := &query.GetCompanyQuery{}
companyId, _ := controller.GetInt64(":companyId")
getCompanyQuery.CompanyId = companyId
data, err := companyService.GetCompany(getCompanyQuery)
controller.Response(data, err)
}
func (controller *CompanyController) RemoveCompany() {
companyService := service.NewCompanyService(nil)
removeCompanyCommand := &command.RemoveCompanyCommand{}
controller.Unmarshal(removeCompanyCommand)
companyId, _ := controller.GetInt64(":companyId")
removeCompanyCommand.CompanyId = companyId
data, err := companyService.RemoveCompany(removeCompanyCommand)
controller.Response(data, err)
}
func (controller *CompanyController) ListCompany() {
companyService := service.NewCompanyService(nil)
listCompanyQuery := &query.ListCompanyQuery{}
offset, _ := controller.GetInt("offset")
listCompanyQuery.Offset = offset
limit, _ := controller.GetInt("limit")
listCompanyQuery.Limit = limit
data, err := companyService.ListCompany(listCompanyQuery)
controller.Response(data, err)
}
func (controller *CompanyController) ConvertCompanyStatus() {
companyService := service.NewCompanyService(nil)
convertCompanyStatusCommand := &command.ConvertCompanyStatusCommand{}
controller.Unmarshal(convertCompanyStatusCommand)
data, err := companyService.ConvertCompanyStatus(convertCompanyStatusCommand)
controller.Response(data, err)
}
func (controller *CompanyController) SetCompanyAdmin() {
companyService := service.NewCompanyService(nil)
setCompanyAdminCommand := &command.SetCompanyAdminCommand{}
controller.Unmarshal(setCompanyAdminCommand)
data, err := companyService.SetCompanyAdmin(setCompanyAdminCommand)
controller.Response(data, err)
}
... ...
package routers
import (
"github.com/beego/beego/v2/server/web"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/port/beego/controllers"
)
func init() {
web.Router("/companies/", &controllers.CompanyController{}, "Post:CreateCompany")
web.Router("/companies/:companyId", &controllers.CompanyController{}, "Put:UpdateCompany")
web.Router("/companies/:companyId", &controllers.CompanyController{}, "Get:GetCompany")
web.Router("/companies/:companyId", &controllers.CompanyController{}, "Delete:RemoveCompany")
web.Router("/companies/", &controllers.CompanyController{}, "Get:ListCompany")
web.Router("/companies/convertCompanyStatus", &controllers.CompanyController{}, "Post:ConvertCompanyStatus")
web.Router("/companies/setCompanyAdmin", &controllers.CompanyController{}, "Post:SetCompanyAdmin")
}
... ...
package company
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/beego/beego/v2/server/web"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
_ "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
_ "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/port/beego"
)
func TestCompany(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Beego Port Company Correlations Test Case Suite")
}
var handler http.Handler
var server *httptest.Server
var _ = BeforeSuite(func() {
handler = web.BeeApp.Handlers
server = httptest.NewServer(handler)
})
var _ = AfterSuite(func() {
server.Close()
})
... ...
package company
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("公司状态转换 禁用、启用", func() {
var companyId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&companyId),
"INSERT INTO companys (company_id, company_info, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?) RETURNING company_id",
"testCompanyId", "testCompanyInfo", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("公司状态转换 禁用、启用", func() {
Context("", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"companyId": "int64",
"status": "int64",
}
httpExpect.POST("/companys/convertCompanyStatus").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM companys WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package company
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("创建", func() {
Describe("提交数据创建", func() {
Context("提交正确的新公司信息数据", func() {
It("返回公司信息数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"name": "string",
"abbreviation": "string",
"status": "int64",
"remarks": "string",
"userAccount": "string",
"userName": "string",
}
httpExpect.POST("/companys/").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("companyId").ValueNotEqual("companyId", BeZero())
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM companys WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package company
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("返回", func() {
var companyId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&companyId),
"INSERT INTO companys (company_id, company_info, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?) RETURNING company_id",
"testCompanyId", "testCompanyInfo", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据companyId参数返回公司信息", func() {
Context("传入有效的companyId", func() {
It("返回公司信息数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/companys/{companyId}").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM companys WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package company
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("返回列表", func() {
var companyId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&companyId),
"INSERT INTO companys (company_id, company_info, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?) RETURNING company_id",
"testCompanyId", "testCompanyInfo", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数返回公司信息列表", func() {
Context("传入有效的参数", func() {
It("返回公司信息数据列表", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/companys/").
WithQuery("offset", "int").
WithQuery("limit", "int").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("count").ValueEqual("count", 1).
ContainsKey("companys").Value("companys").Array()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM companys WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package company
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("移除", func() {
var companyId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&companyId),
"INSERT INTO companys (company_id, company_info, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?) RETURNING company_id",
"testCompanyId", "testCompanyInfo", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数移除", func() {
Context("传入有效的companyId", func() {
It("返回被移除公司信息的数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.DELETE("/companys/{companyId}").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM companys WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package company
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("设置公司管理员", func() {
var companyId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&companyId),
"INSERT INTO companys (company_id, company_info, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?) RETURNING company_id",
"testCompanyId", "testCompanyInfo", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("设置公司管理员", func() {
Context("", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"companyId": "int64",
"userAccount": "string",
"userName": "string",
}
httpExpect.POST("/companys/setCompanyAdmin").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM companys WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package company
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("更新", func() {
var companyId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&companyId),
"INSERT INTO companys (company_id, company_info, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?) RETURNING company_id",
"testCompanyId", "testCompanyInfo", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("提交数据更新", func() {
Context("提交正确的公司信息数据", func() {
It("返回更新后的公司信息数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"name": "string",
"abbreviation": "string",
"status": "int64",
"remarks": "string",
"userAccount": "string",
"userName": "string",
}
httpExpect.PUT("/companys/{companyId}").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("companyId").ValueEqual("companyId", companyId)
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM companys WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...