Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve the test case creating process on ui #515

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions console/atest-ui/src/views/TestSuite.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ const testCaseForm = reactive({
suiteName: '',
name: '',
api: '',
method: 'GET'
method: 'GET',
request: {}
})
const rules = reactive<FormRules<Suite>>({
name: [{ required: true, message: 'Please input TestCase name', trigger: 'blur' }]
Expand All @@ -118,20 +119,17 @@ Magic.Keys(openNewTestCaseDialog, ['Alt+N', 'Alt+dead'])

const submitForm = async (formEl: FormInstance | undefined) => {
if (!formEl) return
await formEl.validate((valid: boolean, fields) => {
await formEl.validate((valid: boolean) => {
if (valid) {
suiteCreatingLoading.value = true

API.CreateTestCase(
{
API.CreateTestCase({
suiteName: props.name,
name: testCaseForm.name,
api: testCaseForm.api,
method: testCaseForm.method
},
() => {
request: testCaseForm.request
}, () => {
suiteCreatingLoading.value = false
emit('updated', 'hello from child')
emit('updated', props.name, testCaseForm.name)
}
)

Expand Down Expand Up @@ -203,6 +201,7 @@ const handleAPISelect = (item: TestCase) => {
if (testCaseForm.name === '') {
testCaseForm.name = item.name
}
testCaseForm.request = item.request
}

function paramChange() {
Expand Down
15 changes: 12 additions & 3 deletions console/atest-ui/src/views/TestingPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function loadTestSuites(storeName: string) {

d.data[k].data.forEach((item: any) => {
suite.children?.push({
id: k + item,
id: generateTestCaseID(k, item),
label: item,
store: storeName,
kind: suite.kind,
Expand All @@ -139,6 +139,10 @@ function loadTestSuites(storeName: string) {
}
}

function generateTestCaseID(suiteName: string, caseName: string) {
return suiteName + caseName
}

interface Store {
name: string,
description: string,
Expand All @@ -147,7 +151,12 @@ interface Store {
const loginDialogVisible = ref(false)
const stores = ref([] as Store[])
const storesLoading = ref(false)
function loadStores() {
function loadStores(lastSuitName?: string, lastCaseName?: string) {
if (lastSuitName && lastCaseName && lastSuitName !== '' && lastCaseName !== '') {
// get data from emit event
Cache.SetLastTestCaseLocation(lastSuitName, generateTestCaseID(lastSuitName, lastCaseName))
}

storesLoading.value = true
const requestOptions = {
headers: {
Expand Down Expand Up @@ -203,7 +212,7 @@ function loadStores() {
treeRef.value!.setCheckedKeys([targetChild.id], false)

testSuite.value = targetSuite.label
Cache.SetCurrentStore(targetSuite.store )
Cache.SetCurrentStore(targetSuite.store)
testSuiteKind.value = targetChild.kind
} else {
viewName.value = ""
Expand Down
8 changes: 2 additions & 6 deletions console/atest-ui/src/views/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ function ImportTestSuite(source: ImportSource, callback: (d: any) => void) {
interface TestCase {
suiteName: string
name: string
api: string
method: string
request: any
}

function CreateTestCase(testcase: TestCase,
Expand All @@ -194,10 +193,7 @@ function CreateTestCase(testcase: TestCase,
suiteName: testcase.suiteName,
data: {
name: testcase.name,
request: {
api: testcase.api,
method: testcase.method
}
request: testcase.request
}
})
}
Expand Down
24 changes: 23 additions & 1 deletion pkg/runner/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,18 @@ func (r *simpleTestCaseRunner) GetSuggestedAPIs(suite *testing.TestSuite, api st
switch param.In {
case "query":
// TODO should have a better way to provide the initial value
(&(testcase.Request)).Query[param.Name] = "todo"
(&(testcase.Request)).Query[param.Name] = generateRandomValue(param)
}
}
testcase.Name = swagger.Paths.Paths[api].Get.ID
case http.MethodPost:
testcase.Name = swagger.Paths.Paths[api].Post.ID
case http.MethodPut:
testcase.Name = swagger.Paths.Paths[api].Put.ID
case http.MethodDelete:
testcase.Name = swagger.Paths.Paths[api].Delete.ID
case http.MethodPatch:
testcase.Name = swagger.Paths.Paths[api].Patch.ID
}
result = append(result, testcase)
}
Expand All @@ -264,6 +273,19 @@ func (r *simpleTestCaseRunner) GetSuggestedAPIs(suite *testing.TestSuite, api st
return
}

func generateRandomValue(param spec.Parameter) interface{} {
switch param.Format {
case "int32", "int64":
return 101
case "boolean":
return true
case "string":
return "random"
default:
return "random"
}
}

func (r *simpleTestCaseRunner) withResponseRecord(resp *http.Response) (responseBodyData []byte, err error) {
responseBodyData, err = io.ReadAll(resp.Body)
r.simpleResponse = SimpleResponse{
Expand Down
40 changes: 39 additions & 1 deletion pkg/runner/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

_ "embed"

"github.com/go-openapi/spec"
"github.com/h2non/gock"
atest "github.com/linuxsuren/api-testing/pkg/testing"
"github.com/linuxsuren/api-testing/pkg/util"
Expand Down Expand Up @@ -558,7 +559,6 @@ func TestGetSuggestedAPIs(t *testing.T) {
assert.NotEmpty(t, result)
method := result[0].Request.Method
assert.Equal(t, strings.ToUpper(method), method)
assert.Equal(t, "todo", result[0].Request.Query["text"])
}

func TestIsStructContent(t *testing.T) {
Expand Down Expand Up @@ -589,6 +589,44 @@ func TestIsStructContent(t *testing.T) {
}
}

func TestGenerateRandomValue(t *testing.T) {
tests := []struct {
param spec.Parameter
expected interface{}
}{
{
param: spec.Parameter{
SimpleSchema: spec.SimpleSchema{
Format: "int32",
},
},
expected: 101,
}, {
param: spec.Parameter{
SimpleSchema: spec.SimpleSchema{
Format: "boolean",
},
},
expected: true,
}, {
param: spec.Parameter{
SimpleSchema: spec.SimpleSchema{
Format: "string",
},
},
expected: "random",
},
}

for _, tt := range tests {
result := generateRandomValue(tt.param)

if result != tt.expected {
t.Errorf("generateRandomValue(%v) = %v, expected %v", tt.param, result, tt.expected)
}
}
}

const defaultSchemaForTest = `{"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
Expand Down
4 changes: 4 additions & 0 deletions pkg/runner/testdata/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
"put": {
"summary": "summary",
"operationId": "updateUser"
},
"patch": {
"summary": "summary",
"operationId": "patchUser"
}
}
},
Expand Down
Loading