diff --git a/public/results.html b/public/results.html index d97fc95287aab3aae8ce36b5db0b247859e9da8f..a490530ddfa9443cce5e88b89d6f6e157687f94b 100644 --- a/public/results.html +++ b/public/results.html @@ -39,6 +39,7 @@ <button type="button">Submit</button> </div> <div id="results"></div> + <div id="messages"></div> </body> <script> @@ -58,6 +59,7 @@ const grpcJsonData = data["grpc"]; document.getElementById('results').replaceChildren(jsonToTable("REST", restJsonData)); document.getElementById('results').appendChild(jsonToTable("gRPC", grpcJsonData)); + document.getElementById('messages').replaceChildren(concatAllMessages(restJsonData, grpcJsonData)); } catch (e) { console.error(e); } @@ -96,6 +98,19 @@ return div; } + function concatAllMessages(...jsonData) { + const div = document.createElement('div'); + for (const data of jsonData) { + for (const [k, v] of Object.entries(data)) { + const p = document.createElement('p'); + p.textContent = `${k}: ${v["message"]}`; + p.className = v["ok"] ? 'success' : 'fail'; + div.appendChild(p); + } + } + return div; + } + </script> </html> \ No newline at end of file diff --git a/src/interfaces.ts b/src/interfaces.ts index 672643d43768024f79e8c448f04b0562d38d2615..1f1c3da67f0552c877b7c70c355e80346af2f6af 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -12,6 +12,6 @@ export interface ITestSuite { testOrderById(id: string): Promise<EndpointReturn>; testAllUsers(): Promise<EndpointReturn>; testInsertOrder(order: Order): Promise<EndpointReturn>; - updateUser(patch: UserPatchRequest): Promise<EndpointReturn>; + testUpdateUser(patch: UserPatchRequest): Promise<EndpointReturn>; runSuite(iterations: number): Promise<TestResults>; } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 246dcdee2c2172951ed942e7a7f8c4522add120a..9adc0c3d350ba3b3b25571ac88c128538b599485 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,12 +2,13 @@ import express from 'express'; import expressWs from 'express-ws'; import path from 'path'; import { runTests } from './testing'; +import { TestResult } from './types'; const { app } = expressWs(express()); app.use(express.json()); app.use(express.urlencoded({ extended: true })); -const resultsCache = {}; +const resultsCache: { [key: string]: TestResult } = {}; app.get('/', (req, res) => { res.sendFile(path.join(__dirname, 'index.html')); diff --git a/src/testing.ts b/src/testing.ts index 094291fac053a5c8d1765d06718075c835b5dc05..17211981a2123c05b666bf8d177fd5db7bc0f555 100644 --- a/src/testing.ts +++ b/src/testing.ts @@ -12,18 +12,16 @@ const runTest: (func: () => any, expected?: any) => Promise<TestResult> = async const pass = expected ? (_.isEqual(result.payload, expected) || _.differenceWith(expected, result.payload, _.isEqual).length == 0) && result.ok : result.ok; - if (!pass) { - console.log('failed.') - console.log('Expected:') - console.log(expected) - console.log('Received:') - console.log(result.payload) - console.log('----------') - } + let message = pass ? 'ok.' : `failed. + Expected: + ${JSON.stringify(expected)} + Received: + ${JSON.stringify(result.payload)}`; return { ok: pass, - time: end - start + time: end - start, + message }; } @@ -36,7 +34,8 @@ export const avgRuntime: (func: () => any, iterations: number, expected?: any) = const results = await Promise.all(promises); return { ok: results.reduce((acc, curr) => acc && curr.ok, true), - time: results.reduce((acc, curr) => acc + curr.time, 0) / iterations + time: results.reduce((acc, curr) => acc + curr.time, 0) / iterations, + message: results[0].message, }; }; diff --git a/src/tests/grpcTests.ts b/src/tests/grpcTests.ts index b605a211b069225f908912f4e0dff3f202bb2e91..da8952805e539376ba01b9f70ff668afac7b02a7 100644 --- a/src/tests/grpcTests.ts +++ b/src/tests/grpcTests.ts @@ -119,7 +119,7 @@ export default class GrpcTestSuite implements ITestSuite { } } - public async updateUser(patch: UserPatchRequest): Promise<any> { + public async testUpdateUser(patch: UserPatchRequest): Promise<any> { try { const data = await this.client.patchAccountDetails(patch); return { payload: data, ok: true }; @@ -186,7 +186,7 @@ export default class GrpcTestSuite implements ITestSuite { results.orderById = await avgRuntime(async () => await this.testOrderById(constants.TEST_ORDER_ID), iterations, constants.EXPECTED_ORDER); results.allUsers = await avgRuntime(async () => await this.testAllUsers(), iterations, constants.EXPECTED_USERS); results.insertOrder = await avgRuntime(async () => await this.testInsertOrder({ id: uuid(), ...constants.TEST_ORDER }), iterations); - results.updateUser = await avgRuntime(async () => await this.updateUser(constants.TEST_UPDATE), iterations); + results.updateUser = await avgRuntime(async () => await this.testUpdateUser(constants.TEST_UPDATE), iterations); return results; } } \ No newline at end of file diff --git a/src/tests/restTests.ts b/src/tests/restTests.ts index 2c3a56257e40c379f972847832694b7573ef1e46..580a439d408c83be1ca3a557888a1302114a1200 100644 --- a/src/tests/restTests.ts +++ b/src/tests/restTests.ts @@ -142,7 +142,7 @@ export default class RestTestSuite implements ITestSuite { } } - public async updateUser(patch: UserPatchRequest): Promise<EndpointReturn> { + public async testUpdateUser(patch: UserPatchRequest): Promise<EndpointReturn> { try { const resp = await fetch(this.endpoint + `/user/${patch.id}`, { method: "PATCH", @@ -206,28 +206,18 @@ export default class RestTestSuite implements ITestSuite { time: 0 } }; - console.log('randomProduct'); + results.randomProduct = await avgRuntime(async () => await this.testRandomProduct(), iterations); - console.log('userById'); results.userById = await avgRuntime(async () => await this.testUserById(constants.TEST_USER_ID), iterations, constants.EXPECTED_USER); - console.log('allProducts'); results.allProducts = await avgRuntime(async () => await this.testAllProducts(), iterations, constants.EXPECTED_PRODUCTS); - console.log('productById'); results.productById = await avgRuntime(async () => await this.testProductById(constants.TEST_PRODUCT_ID), iterations, constants.EXPECTED_PRODUCT); - console.log('allCategories'); results.allCategories = await avgRuntime(async () => await this.testAllCategories(), iterations, constants.EXPECTED_CATEGORIES); - console.log('allOrders'); results.allOrders = await avgRuntime(async () => await this.testAllOrders(), iterations, constants.EXPECTED_ORDERS); - console.log('ordersByUser'); results.ordersByUser = await avgRuntime(async () => await this.testOrdersByUser(constants.TEST_USER_ID), iterations, constants.EXPECTED_ORDERSBYUSER); - console.log('orderById'); results.orderById = await avgRuntime(async () => await this.testOrderById(constants.TEST_ORDER_ID), iterations, constants.EXPECTED_ORDER); - console.log('allUsers'); results.allUsers = await avgRuntime(async () => await this.testAllUsers(), iterations, constants.EXPECTED_USERS); - console.log('insertOrder'); results.insertOrder = await avgRuntime(async () => await this.testInsertOrder({ id: uuid(), ...constants.TEST_ORDER }), iterations); - console.log('updateUser'); - results.updateUser = await avgRuntime(async () => await this.updateUser(constants.TEST_UPDATE), iterations); + results.updateUser = await avgRuntime(async () => await this.testUpdateUser(constants.TEST_UPDATE), iterations); return results; } diff --git a/src/types.ts b/src/types.ts index cb3b83275fae46c02723ef40d945102a7cc7d69a..92b419344f80ea4bc151ecbc3132d0d006210af5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -62,6 +62,7 @@ export type EndpointReturn = { export type TestResult = { ok: boolean; time: number; + message?: string; } export type TestResults = {