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 @@
<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
......@@ -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
......@@ -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'));
......
......@@ -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,
};
};
......
......@@ -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
......@@ -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;
}
......
......@@ -62,6 +62,7 @@ export type EndpointReturn = {
export type TestResult = {
ok: boolean;
time: number;
message?: string;
}
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