提交 80c2762a 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

generate test scripts using json that returned by mock server

上级 6387a04a
package action
import (
"encoding/json"
"errors"
"fmt"
httpClient "github.com/easysoft/zentaoatf/src/http"
......@@ -25,23 +24,19 @@ func Gen(url string, entityType string, entityVal string, langType string, singl
params["taskId"] = entityVal
}
jsonBuf, err := httpClient.GetBuf(url, params)
json, err := httpClient.Get(url, params)
if err != nil {
Generate(jsonBuf, langType, singleFile)
Generate(json, langType, singleFile)
}
}
func Generate(jsonBuf []byte, langType string, singleFile bool) error {
var resp model.Response
json.Unmarshal(jsonBuf, &resp)
if resp.Code != 1 {
fmt.Println(string(jsonBuf))
return errors.New("request fail")
func Generate(json model.Response, langType string, singleFile bool) error {
if json.Code != 1 {
return errors.New("response code = %s")
}
for _, testCase := range resp.Cases {
for _, testCase := range json.Cases {
DealwithTestCase(testCase, langType, singleFile)
}
......@@ -68,7 +63,7 @@ func DealwithTestCase(tc model.TestCase, langType string, singleFile bool) {
caseId := tc.Id
caseTitle := tc.Title
scriptFile := fmt.Sprintf("xdoc/scripts/tc-%s.%s", strconv.Itoa(caseId), LangMap[langType]["extName"])
scriptFile := fmt.Sprintf(utils.GenDir+"tc-%s.%s", strconv.Itoa(caseId), LangMap[langType]["extName"])
steps := make([]string, 0)
expects := make([]string, 0)
......@@ -94,7 +89,7 @@ func DealwithTestCase(tc model.TestCase, langType string, singleFile bool) {
} else {
expectFile := utils.ScriptToExpectName(scriptFile)
expectsTxt = "@file"
expectsTxt = "@file\n"
utils.WriteFile(expectFile, strings.Join(expects, "\n"))
}
......
......@@ -17,7 +17,7 @@ import (
const (
leftWidth = 32
labelWidth = 15
inputFullLineWidth = 64
inputFullLineWidth = 69
inputNumbWidth = 25
buttonWidth = 10
space = 2
......@@ -295,19 +295,18 @@ func importProjectRequest(g *gocui.Gui, v *gocui.View) error {
_, _ = fmt.Fprintln(cmdView, fmt.Sprintf("#atf gen -u %s -t %s -v %s -l %s -s %t",
url, params["entityType"], params["entityVal"], language, singleFile))
jsonBuf, e := httpClient.GetBuf(url, params)
json, e := httpClient.Get(url, params)
if e != nil {
fmt.Fprint(cmdView, e.Error())
fmt.Fprintln(cmdView, e.Error())
return nil
}
err := action.Generate(jsonBuf, language, singleFile)
err := action.Generate(json, language, singleFile)
if err == nil {
fmt.Fprintln(cmdView, fmt.Sprintf("success to generate test scripts in 'xdoc/scripts' at %s",
utils.DateTimeStr(time.Now())))
fmt.Fprintln(cmdView, fmt.Sprintf("success to generate test scripts in '%s' at %s",
utils.GenDir, utils.DateTimeStr(time.Now())))
} else {
fmt.Fprint(cmdView, err.Error())
fmt.Fprintln(cmdView, err.Error())
}
return nil
......
package httpClient
import (
"encoding/json"
"errors"
"fmt"
"github.com/easysoft/zentaoatf/src/model"
"io/ioutil"
"net/http"
)
func GetBuf(url string, params map[string]string) ([]byte, error) {
func Get(url string, params map[string]string) (model.Response, error) {
var ret model.Response
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return []byte(""), err
return ret, err
}
q := req.URL.Query()
......@@ -20,32 +26,20 @@ func GetBuf(url string, params map[string]string) ([]byte, error) {
var resp *http.Response
resp, err = http.DefaultClient.Do(req)
if err != nil {
return []byte(""), err
return ret, err
}
bytes, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
return bytes, nil
}
func Get(url string, params map[string]string) (string, error) {
bts, err := GetBuf(url, params)
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
return ret, err
}
return string(bts), nil
}
func GetMock(url string, params map[string]string) string {
resp, err := http.Get(url)
if err != nil {
return err.Error()
var respModel model.Response
json.Unmarshal(bytes, &respModel)
if respModel.Code != 1 {
return ret, errors.New(fmt.Sprintf("request fail, code %d", respModel.Code))
}
bytes, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
return string(bytes)
return respModel, nil
}
......@@ -11,4 +11,6 @@ const (
EnRes = "res/messages_en.json"
ZhRes = "res/messages_zh.json"
GenDir = "scripts/"
)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册