From 9cc987b854242bf51f4c93723914a315d355e505 Mon Sep 17 00:00:00 2001 From: wangyaqi Date: Wed, 20 Sep 2023 20:38:41 +0800 Subject: [PATCH] feat: uniCloud auto test --- .../unicloud-call-function.test.js | 23 ++++++++ .../unicloud-call-function.uvue | 9 ++- .../unicloud-database.test.js | 58 +++++++++++++++++++ .../unicloud-database/unicloud-database.uvue | 54 +++++++++++------ .../unicloud-import-object.test.js | 30 ++++++++++ .../unicloud-import-object.uvue | 30 ++++++---- .../cloudfunctions/todo/index.obj.js | 2 + 7 files changed, 176 insertions(+), 30 deletions(-) create mode 100644 pages/API/unicloud-call-function/unicloud-call-function.test.js create mode 100644 pages/API/unicloud-database/unicloud-database.test.js create mode 100644 pages/API/unicloud-import-object/unicloud-import-object.test.js diff --git a/pages/API/unicloud-call-function/unicloud-call-function.test.js b/pages/API/unicloud-call-function/unicloud-call-function.test.js new file mode 100644 index 00000000..f9df7a31 --- /dev/null +++ b/pages/API/unicloud-call-function/unicloud-call-function.test.js @@ -0,0 +1,23 @@ +const PAGE_PATH = '/pages/API/unicloud-call-function/unicloud-call-function' + +describe('unicloud-call-function', () => { + let page + beforeAll(async () => { + page = await program.reLaunch(PAGE_PATH) + await page.waitFor(500) + }) + + it('callFunction', async () => { + await page.callMethod('callFunction') + await page.waitFor(3000) + const { + callFunctionResult, + callFunctionError + } = await page.data() + console.error(callFunctionResult) + console.error(callFunctionError) + expect(callFunctionResult['showMessage']).toBe("Hello uniCloud function") + expect(callFunctionResult['event']['num']).toBe(1) + expect(callFunctionResult['event']['str']).toBe('ABC') + }) +}) diff --git a/pages/API/unicloud-call-function/unicloud-call-function.uvue b/pages/API/unicloud-call-function/unicloud-call-function.uvue index 9cfe2e2b..c8ea30c8 100644 --- a/pages/API/unicloud-call-function/unicloud-call-function.uvue +++ b/pages/API/unicloud-call-function/unicloud-call-function.uvue @@ -18,7 +18,9 @@ export default { data() { return { - title: '请求云函数' + title: '请求云函数', + callFunctionResult: {}, + callFunctionError: {} } }, onLoad() { @@ -38,6 +40,7 @@ } }).then(res => { const result = res.result + this.callFunctionResult = result console.log(JSON.stringify(result)) uni.hideLoading() uni.showModal({ @@ -47,6 +50,10 @@ }) }).catch((err: any | null) => { const error = err as UniCloudError + this.callFunctionError = { + errCode: error.errCode, + errMsg: error.errMsg + } uni.hideLoading() uni.showModal({ title: '错误', diff --git a/pages/API/unicloud-database/unicloud-database.test.js b/pages/API/unicloud-database/unicloud-database.test.js new file mode 100644 index 00000000..1af7f5da --- /dev/null +++ b/pages/API/unicloud-database/unicloud-database.test.js @@ -0,0 +1,58 @@ +const PAGE_PATH = '/pages/API/unicloud-database/unicloud-database' + +describe('unicloud-database', () => { + let page + beforeAll(async () => { + page = await program.reLaunch(PAGE_PATH) + await page.waitFor(500) + }) + it('databaseBasic', async () => { + await page.callMethod('dbRemove') + await page.waitFor(3000) + await page.callMethod('dbAdd') + await page.waitFor(1000) + await page.callMethod('dbBatchAdd') + await page.waitFor(1000) + await page.callMethod('dbGet') + await page.waitFor(1000) + await page.callMethod('dbGetWithCommand') + await page.waitFor(1000) + await page.callMethod('dbUpdate') + await page.waitFor(1000) + await page.callMethod('dbRemove') + await page.waitFor(1000) + + const { + addId, + batchAddIds, + batchAddinserted, + updateUpdated, + getData, + getWithCommandData, + removeDeleted, + } = await page.data() + + expect(addId.length > 0).toBe(true) + expect(batchAddIds.length).toBe(2) + expect(batchAddinserted).toBe(2) + expect(getData.length).toBe(2) + expect(getWithCommandData.length).toBe(2) + expect(updateUpdated).toBe(3) + expect(removeDeleted).toBe(3) + + }) + + it('databaseLookup', async () => { + await page.callMethod('dbLookupInit') + await page.waitFor(1000) + await page.callMethod('dbLookup') + await page.waitFor(1000) + + const { + lookupData + } = await page.data() + expect(lookupData.length).toBe(2) + expect(lookupData[0]['foreign_id'].length).toBe(1) + expect(lookupData[1]['foreign_id'].length).toBe(1) + }) +}); \ No newline at end of file diff --git a/pages/API/unicloud-database/unicloud-database.uvue b/pages/API/unicloud-database/unicloud-database.uvue index 50dafe68..c11e4b64 100644 --- a/pages/API/unicloud-database/unicloud-database.uvue +++ b/pages/API/unicloud-database/unicloud-database.uvue @@ -25,7 +25,15 @@ export default { data() { return { - title: 'ClientDB' + title: 'ClientDB', + addId: '', + batchAddIds: [] as Array, + batchAddinserted: 0, + updateUpdated: 0, + getData: [] as Array, + getWithCommandData: [] as Array, + removeDeleted: 0, + lookupData: [] as Array, } }, onLoad() { @@ -48,6 +56,7 @@ .then(res => { uni.hideLoading() console.log(res) + this.addId = res.id uni.showModal({ content: `新增成功,id: ${res.id}`, showCancel: false @@ -79,6 +88,8 @@ .then((res) => { uni.hideLoading() console.log(res) + this.batchAddIds = res.ids + this.batchAddinserted = res.inserted uni.showModal({ content: `新增成功,id列表: ${res.ids.join(',')}`, showCancel: false @@ -94,7 +105,7 @@ }) }) }, - dbUpdate() { + dbGet() { uni.showLoading({ title: '加载中...' }) @@ -103,14 +114,17 @@ .where( 'tag == "default-tag"' ) - .update({ - num: 4 - }) + .field('num, tag') + .orderBy('num desc') + .skip(1) + .limit(2) + .get() .then(res => { uni.hideLoading() console.log(res) + this.getData = res.data uni.showModal({ - content: `更新成功,更新了${res.updated}条数据`, + content: `获取成功,取到了${res.data.length}条数据`, showCancel: false }) }) @@ -124,15 +138,15 @@ }) }) }, - dbGet() { + dbGetWithCommand() { uni.showLoading({ title: '加载中...' }) const db = uniCloud.databaseForJQL() db.collection('type') - .where( - 'tag == "default-tag"' - ) + .where({ + num: db.command.gt(1) + }) .field('num, tag') .orderBy('num desc') .skip(1) @@ -141,6 +155,7 @@ .then(res => { uni.hideLoading() console.log(res) + this.getWithCommandData = res.data uni.showModal({ content: `获取成功,取到了${res.data.length}条数据`, showCancel: false @@ -156,25 +171,24 @@ }) }) }, - dbGetWithCommand() { + dbUpdate() { uni.showLoading({ title: '加载中...' }) const db = uniCloud.databaseForJQL() db.collection('type') - .where({ - num: db.command.gt(2) + .where( + 'tag == "default-tag"' + ) + .update({ + num: 4 }) - .field('num, tag') - .orderBy('num desc') - .skip(1) - .limit(2) - .get() .then(res => { uni.hideLoading() console.log(res) + this.updateUpdated = res.updated uni.showModal({ - content: `获取成功,取到了${res.data.length}条数据`, + content: `更新成功,更新了${res.updated}条数据`, showCancel: false }) }) @@ -201,6 +215,7 @@ .then(res => { uni.hideLoading() console.log(res) + this.removeDeleted = res.deleted uni.showModal({ content: `删除成功,删掉了${res.deleted}条数据`, showCancel: false @@ -289,6 +304,7 @@ .then(res => { uni.hideLoading() console.log(res) + this.lookupData = res.data uni.showModal({ content: `联表查询成功,取到了${res.data.length}条数据`, showCancel: false diff --git a/pages/API/unicloud-import-object/unicloud-import-object.test.js b/pages/API/unicloud-import-object/unicloud-import-object.test.js new file mode 100644 index 00000000..ff1a263c --- /dev/null +++ b/pages/API/unicloud-import-object/unicloud-import-object.test.js @@ -0,0 +1,30 @@ +const PAGE_PATH = '/pages/API/unicloud-import-object/unicloud-import-object' + +describe('unicloud-import-object', () => { + let page + beforeAll(async () => { + page = await program.reLaunch(PAGE_PATH) + await page.waitFor(500) + }) + it('importObject', async () => { + await page.callMethod('addTodo') + await page.callMethod('fail') + await page.callMethod('success') + await page.waitFor(3000) + + const { + todoTitle, + todoContent, + returnTodoTitle, + returnTodoContent, + failErrCode, + successErrCode, + } = await page.data() + + expect(returnTodoTitle).toBe(todoTitle) + expect(returnTodoContent).toBe(todoContent) + expect(failErrCode).toBe('TEST_ERROR_CODE') + expect(successErrCode).toBe(0) + + }) +}); \ No newline at end of file diff --git a/pages/API/unicloud-import-object/unicloud-import-object.uvue b/pages/API/unicloud-import-object/unicloud-import-object.uvue index d2b13a1b..ff3ed229 100644 --- a/pages/API/unicloud-import-object/unicloud-import-object.uvue +++ b/pages/API/unicloud-import-object/unicloud-import-object.uvue @@ -1,7 +1,7 @@ @@ -28,15 +28,23 @@ export default { data() { return { - title: '请求云对象' + title: '请求云对象', + todoTitle: '学习编程', + todoContent: '熟悉uts语法', + returnTodoTitle: '', + returnTodoContent: '', + failErrCode: '', + successErrCode: -1 } }, methods: { addTodo() { const todo = uniCloud.importObject('todo') - const title = '学习编程' - const content = '熟悉uts语法' - todo.add(title, content).then((res: UTSJSONObject) : void => { + const title = this.todoTitle + const content = this.todoContent + todo.add(title, content).then((res : UTSJSONObject) : void => { + this.returnTodoTitle = res['title'] as string + this.returnTodoContent = res['content'] as string uni.showModal({ title: '提示', content: res['showMessage'] as string, @@ -53,7 +61,7 @@ retry: true } }) - todoObj.randomFail().then((res: UTSJSONObject) : void => { + todoObj.randomFail().then((res : UTSJSONObject) : void => { uni.showModal({ title: '提示', content: res['showMessage'] as string, @@ -66,7 +74,7 @@ }, fail() { const todo = uniCloud.importObject('todo') - todo.fail().then((res: UTSJSONObject) : void => { + todo.fail().then((res : UTSJSONObject) : void => { uni.showModal({ title: '提示', content: 'todo.fail应调用失败,此处错误的触发了成功回调', @@ -75,12 +83,14 @@ console.log('todo.fail: ', res); }).catch((err : any | null) : void => { const error = err as UniCloudError + this.failErrCode = error.errCode as string console.error(error) }) }, success() { const todo = uniCloud.importObject('todo') - todo.success().then((res: UTSJSONObject) : void => { + todo.success().then((res : UTSJSONObject) : void => { + this.successErrCode = res['errCode'] as number uni.showModal({ title: '提示', content: res['showMessage'] as string, @@ -97,4 +107,4 @@ + \ No newline at end of file diff --git a/uniCloud-aliyun/cloudfunctions/todo/index.obj.js b/uniCloud-aliyun/cloudfunctions/todo/index.obj.js index 0f33ab3d..9fa04c30 100644 --- a/uniCloud-aliyun/cloudfunctions/todo/index.obj.js +++ b/uniCloud-aliyun/cloudfunctions/todo/index.obj.js @@ -4,6 +4,8 @@ module.exports = { async add(title, content) { console.log(title, content) return { + title, + content, showMessage: `Todo added, title: ${title}, content: ${content}` } }, -- GitLab