Skip to content
Snippets Groups Projects
Commit 1cab257a authored by Jack Hu's avatar Jack Hu
Browse files

also give an error message

parent 572b7a97
No related branches found
No related tags found
No related merge requests found
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
<button type="button">Submit</button> <button type="button">Submit</button>
</div> </div>
<div id="results"></div> <div id="results"></div>
<div id="messages"></div>
</body> </body>
<script> <script>
...@@ -58,6 +59,7 @@ ...@@ -58,6 +59,7 @@
const grpcJsonData = data["grpc"]; const grpcJsonData = data["grpc"];
document.getElementById('results').replaceChildren(jsonToTable("REST", restJsonData)); document.getElementById('results').replaceChildren(jsonToTable("REST", restJsonData));
document.getElementById('results').appendChild(jsonToTable("gRPC", grpcJsonData)); document.getElementById('results').appendChild(jsonToTable("gRPC", grpcJsonData));
document.getElementById('messages').replaceChildren(concatAllMessages(restJsonData, grpcJsonData));
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }
...@@ -96,6 +98,19 @@ ...@@ -96,6 +98,19 @@
return div; 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> </script>
</html> </html>
\ No newline at end of file
...@@ -12,6 +12,6 @@ export interface ITestSuite { ...@@ -12,6 +12,6 @@ export interface ITestSuite {
testOrderById(id: string): Promise<EndpointReturn>; testOrderById(id: string): Promise<EndpointReturn>;
testAllUsers(): Promise<EndpointReturn>; testAllUsers(): Promise<EndpointReturn>;
testInsertOrder(order: Order): Promise<EndpointReturn>; testInsertOrder(order: Order): Promise<EndpointReturn>;
updateUser(patch: UserPatchRequest): Promise<EndpointReturn>; testUpdateUser(patch: UserPatchRequest): Promise<EndpointReturn>;
runSuite(iterations: number): Promise<TestResults>; runSuite(iterations: number): Promise<TestResults>;
} }
\ No newline at end of file
...@@ -2,12 +2,13 @@ import express from 'express'; ...@@ -2,12 +2,13 @@ import express from 'express';
import expressWs from 'express-ws'; import expressWs from 'express-ws';
import path from 'path'; import path from 'path';
import { runTests } from './testing'; import { runTests } from './testing';
import { TestResult } from './types';
const { app } = expressWs(express()); const { app } = expressWs(express());
app.use(express.json()); app.use(express.json());
app.use(express.urlencoded({ extended: true })); app.use(express.urlencoded({ extended: true }));
const resultsCache = {}; const resultsCache: { [key: string]: TestResult } = {};
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html')); res.sendFile(path.join(__dirname, 'index.html'));
......
...@@ -12,18 +12,16 @@ const runTest: (func: () => any, expected?: any) => Promise<TestResult> = async ...@@ -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; const pass = expected ? (_.isEqual(result.payload, expected) || _.differenceWith(expected, result.payload, _.isEqual).length == 0) && result.ok : result.ok;
if (!pass) { let message = pass ? 'ok.' : `failed.
console.log('failed.') Expected:
console.log('Expected:') ${JSON.stringify(expected)}
console.log(expected) Received:
console.log('Received:') ${JSON.stringify(result.payload)}`;
console.log(result.payload)
console.log('----------')
}
return { return {
ok: pass, ok: pass,
time: end - start time: end - start,
message
}; };
} }
...@@ -36,7 +34,8 @@ export const avgRuntime: (func: () => any, iterations: number, expected?: any) = ...@@ -36,7 +34,8 @@ export const avgRuntime: (func: () => any, iterations: number, expected?: any) =
const results = await Promise.all(promises); const results = await Promise.all(promises);
return { return {
ok: results.reduce((acc, curr) => acc && curr.ok, true), 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,
}; };
}; };
......
...@@ -119,7 +119,7 @@ export default class GrpcTestSuite implements ITestSuite { ...@@ -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 { try {
const data = await this.client.patchAccountDetails(patch); const data = await this.client.patchAccountDetails(patch);
return { payload: data, ok: true }; return { payload: data, ok: true };
...@@ -186,7 +186,7 @@ export default class GrpcTestSuite implements ITestSuite { ...@@ -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.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.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.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; return results;
} }
} }
\ No newline at end of file
...@@ -142,7 +142,7 @@ export default class RestTestSuite implements ITestSuite { ...@@ -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 { try {
const resp = await fetch(this.endpoint + `/user/${patch.id}`, { const resp = await fetch(this.endpoint + `/user/${patch.id}`, {
method: "PATCH", method: "PATCH",
...@@ -206,28 +206,18 @@ export default class RestTestSuite implements ITestSuite { ...@@ -206,28 +206,18 @@ export default class RestTestSuite implements ITestSuite {
time: 0 time: 0
} }
}; };
console.log('randomProduct');
results.randomProduct = await avgRuntime(async () => await this.testRandomProduct(), iterations); 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); 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); 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); 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); 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); 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); 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); 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); 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); results.insertOrder = await avgRuntime(async () => await this.testInsertOrder({ id: uuid(), ...constants.TEST_ORDER }), iterations);
console.log('updateUser'); results.updateUser = await avgRuntime(async () => await this.testUpdateUser(constants.TEST_UPDATE), iterations);
results.updateUser = await avgRuntime(async () => await this.updateUser(constants.TEST_UPDATE), iterations);
return results; return results;
} }
......
...@@ -62,6 +62,7 @@ export type EndpointReturn = { ...@@ -62,6 +62,7 @@ export type EndpointReturn = {
export type TestResult = { export type TestResult = {
ok: boolean; ok: boolean;
time: number; time: number;
message?: string;
} }
export type TestResults = { export type TestResults = {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment