api_result_test.go 7.2 KB
Newer Older
aaronchen2k2k's avatar
aaronchen2k2k 已提交
1 2 3 4
package main

import (
	"encoding/json"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
5
	"fmt"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
6 7
	commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain"
	zentaoHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/zentao"
8
	stringUtils "github.com/easysoft/zentaoatf/pkg/lib/string"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
9 10
	constTestHelper "github.com/easysoft/zentaoatf/test/helper/conf"
	httpHelper "github.com/easysoft/zentaoatf/test/helper/http"
11
	"github.com/easysoft/zentaoatf/test/restapi/config"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
12 13
	"github.com/ozontech/allure-go/pkg/framework/provider"
	"github.com/ozontech/allure-go/pkg/framework/suite"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
14
	"github.com/tidwall/gjson"
15
	"log"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
	"testing"
)

func TestResultApi(t *testing.T) {
	suite.RunSuite(t, new(ResultApiSuite))
}

type ResultApiSuite struct {
	suite.Suite
}

func (s *ResultApiSuite) BeforeEach(t provider.T) {
	t.AddSubSuite("SuiteApi")
}

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
func (s *ResultApiSuite) TestResultSubmitZtfResultApi(t provider.T) {
	t.ID("7626,7628")
	token := httpHelper.Login()

	latestId := getCaseResult(config.CaseId)["latestId"].(int64)

	url := zentaoHelper.GenApiUrl("ciresults", nil, constTestHelper.ZentaoSiteUrl)

	report := commDomain.ZtfReport{}
	err := json.Unmarshal([]byte(ztfReportJson), &report)
	t.Require().Equal(err, nil, "submit result failed")

	report.Name = "接口测试任务" + stringUtils.NewUuid()

	resp, err := httpHelper.Post(url, token, report)
	log.Print(resp)
	t.Require().Equal(err, nil, "submit result failed")

	// check case result
	latestIdNew := getCaseResult(config.CaseId)["latestId"].(int64)
51
	t.Require().Greater(latestIdNew, latestId, "submit result failed")
52 53 54 55 56 57

	// check task record
	tasksBytes := listTask(token)
	name := gjson.Get(string(tasksBytes), "testtasks.0.name").String()
	t.Require().Equal(name, report.Name, "submit result failed")
}
58 59 60 61 62

func (s *ResultApiSuite) TestResultSubmitUnitResultApi(t provider.T) {
	t.ID("7627")
	token := httpHelper.Login()

63
	latestCaseResultId := getCaseResult(config.CaseId)["latestId"].(int64)
64 65 66 67

	url := zentaoHelper.GenApiUrl("ciresults", nil, constTestHelper.ZentaoSiteUrl)

	report := commDomain.ZtfReport{}
68 69
	err := json.Unmarshal([]byte(unitReportJson), &report)
	t.Require().Equal(err, nil, "submit result failed")
70
	report.Name = "接口测试任务" + stringUtils.NewUuid()
71

72 73
	resp, err := httpHelper.Post(url, token, report)
	log.Print(resp)
74 75
	t.Require().Equal(err, nil, "submit result failed")

76
	// check case result
77 78
	latestCaseResultIdNew := getCaseResult(config.CaseId)["latestId"].(int64)
	t.Require().Greater(latestCaseResultIdNew, latestCaseResultId, "submit result failed")
79
}
80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
func (s *ResultApiSuite) TestResultSubmitSameTaskIdApi(t provider.T) {
	t.ID("7630")
	token := httpHelper.Login()

	latestCaseResultId := getCaseResult(config.CaseId)["latestId"]

	url := zentaoHelper.GenApiUrl("ciresults", nil, constTestHelper.ZentaoSiteUrl)

	report := commDomain.ZtfReport{}
	err := json.Unmarshal([]byte(ztfReportJson), &report)
	t.Require().Equal(err, nil, "submit result failed")

	_, err = httpHelper.Post(url, token, report)
	t.Require().Equal(err, nil, "submit result failed")

	// check case result
	latestCaseResultId2 := getCaseResult(config.CaseId)["latestId"]
	t.Require().Greater(latestCaseResultId2, latestCaseResultId, "submit result failed")

	// get latest task id
	latestTaskId := getLatestTaskId(token)

	// submit again with same task id
	report.TaskId = latestTaskId
	_, err = httpHelper.Post(url, token, report)
	t.Require().Equal(err, nil, "submit result failed")

	// check case result
	latestCaseResultId3 := getCaseResult(config.CaseId)["latestId"].(int64)
	t.Require().Greater(latestCaseResultId3, latestCaseResultId2, "submit result failed")

	// get latest task id
	latestTaskId2 := getLatestTaskId(token)

	// check not add an new task
	t.Require().Equal(latestTaskId2, latestTaskId, "submit result failed")
}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130

func getCaseResult(caseId int) (result map[string]interface{}) {
	token := httpHelper.Login()

	url := zentaoHelper.GenApiUrl(fmt.Sprintf("testcases/%d/results", caseId), nil, constTestHelper.ZentaoSiteUrl)

	bodyBytes, _ := httpHelper.Get(url, token)

	result = map[string]interface{}{}

	result["latestId"] = gjson.Get(string(bodyBytes), "results.0.id").Int()

	return
aaronchen2k2k's avatar
aaronchen2k2k 已提交
131 132
}

133
const ztfReportJson = `
aaronchen2k2k's avatar
aaronchen2k2k 已提交
134 135 136 137 138 139 140 141 142 143 144
{
	"name": "",
	"platform": "mac",
	"testType": "func",
	"testTool": "ztf",
	"buildTool": "",
	"testCommand": "/private/var/folders/ry/yjxnkwt12kl6d1d13q5cz6wc0000gn/T/GoLand/___test_1 run demo/t/test.py -p 1",
	"workspaceType": "",
	"workspacePath": "/Users/aaron/rd/project/zentao/go/ztf/",
	"submitResult": false,
	"execBy": "case",
145
	"productId": 1,
aaronchen2k2k's avatar
aaronchen2k2k 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	"zentaoData": "",
	"buildUrl": "",
	"log": "2023-05-29 13:48:35.118\texec/script.go:35\t开始执行/Users/aaron/rd/project/zentao/go/ztf/demo/t/test.py于2023-05-29 13:48:35。\n2023-05-29 13:48:35.521\texec/file.go:117\tone\n\n2023-05-29 13:48:35.521\texec/file.go:117\ttwo\n\n2023-05-29 13:48:35.521\texec/file.go:117\tthree\n\n2023-05-29 13:48:35.537\texec/script.go:64\t结束执行/Users/aaron/rd/project/zentao/go/ztf/demo/t/test.py于2023-05-29 13:48:35。",
	"pass": 1,
	"fail": 0,
	"skip": 0,
	"total": 1,
	"startTime": 1685339315,
	"endTime": 1685339315,
	"duration": 0,
	"funcResult": [
		{
			"id": 1,
			"workspaceId": 0,
			"seq": "",
			"key": "e515b2623360a567023c8ddd9e91514c",
			"productId": 1,
			"path": "/Users/aaron/rd/project/zentao/go/ztf/demo/t/test.py",
			"relativePath": "demo/t/test.py",
			"status": "pass",
			"title": "测试返回结果",
			"steps": [
				{
					"id": "1",
					"name": "返回 one",
					"status": "pass",
					"checkPoints": [
						{
							"numb": 1,
							"expect": "one",
							"actual": "one",
							"status": "pass"
						}
					]
				},
				{
					"id": "2",
					"name": "返回 two",
					"status": "pass",
					"checkPoints": [
						{
187
							"numb": 1,
aaronchen2k2k's avatar
aaronchen2k2k 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
							"expect": "two",
							"actual": "two",
							"status": "pass"
						}
					]
				},
				{
					"id": "3",
					"name": "返回 three",
					"status": "pass",
					"checkPoints": [
						{
							"numb": 1,
							"expect": "three",
							"actual": "three",
							"status": "pass"
						}
					]
				}
			]
		}
	]
}
`
212 213 214 215 216 217 218 219 220 221 222 223

const unitReportJson = `
{
    "name": "",
    "platform": "mac",
    "testType": "unit",
    "testTool": "gotest",
    "buildTool": "",
    "testCommand": "go test restapi/api_product_test.go -v",
    "workspaceType": "",
    "workspacePath": "",
    "submitResult": false,
224
	"productId": 1,
225 226
    "zentaoData": "",
    "buildUrl": "",
227 228
    "log": "2023-05-31 13:58:03.483\texec/unit.go:155\t=== RUN ...",
    "pass": 2,
229 230
    "fail": 0,
    "skip": 0,
231 232 233 234 235 236 237 238 239
    "total": 2,
    "startTime": 1685512678,
    "endTime": 1685512683,
    "duration": 5,
    "unitResult":
    [
        {
            "title": "TestProductDetailApi",
            "testSuite": "ProductApiSuite-ProductApi",
240
			"productId": 1,
241 242 243 244 245 246
            "startTime": 0,
            "endTime": 0,
            "duration": 0.081,
            "failure": null,
            "errorType": "",
            "errorContent": "",
247
            "id": 1,
248 249 250 251 252 253 254 255 256 257 258 259
            "cid": 1,
            "status": "pass"
        },
        {
            "title": "TestProductListApi",
            "testSuite": "ProductApiSuite-ProductApi",
            "startTime": 0,
            "endTime": 0,
            "duration": 0.165,
            "failure": null,
            "errorType": "",
            "errorContent": "",
260
            "id": 2,
261 262 263 264
            "cid": 2,
            "status": "pass"
        }
    ]
265
}
266

267
`