diff --git a/public/results.html b/public/results.html index 49646dfff7accefa120468994fd181b1b68319eb..d97fc95287aab3aae8ce36b5db0b247859e9da8f 100644 --- a/public/results.html +++ b/public/results.html @@ -22,7 +22,7 @@ } .fail { - color: orange; + color: red; } tr:nth-child(even) { diff --git a/src/constants.ts b/src/constants.ts index 80a940f3307e44d479133add5e872e4c91d6998d..623dc7b5203cc7456b3729f2566facaf71ab427e 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,7 +1,7 @@ -import * as categoriesOracle from './oracles/categories.json'; -import * as ordersOracle from './oracles/orders.json'; -import * as productsOracle from './oracles/products.json'; -import * as usersOracle from './oracles/users.json'; +import categoriesOracle from './oracles/categories.json'; +import ordersOracle from './oracles/orders.json'; +import productsOracle from './oracles/products.json'; +import usersOracle from './oracles/users.json'; const constants = { TEST_USER_ID: usersOracle[0].id, diff --git a/src/interfaces.ts b/src/interfaces.ts index df0f3d35995ee390d9481262737dc82f11299541..672643d43768024f79e8c448f04b0562d38d2615 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -1,17 +1,17 @@ import { UserPatchRequest } from "./compiled_proto/app"; -import { Category, Order, Product, TestResults, User } from "./types"; +import { Category, EndpointReturn, Order, Product, TestResults, User } from "./types"; export interface ITestSuite { - testRandomProduct(): Promise<Product>; - testUserById(id: string): Promise<User>; - testAllProducts(categoryId?: string): Promise<Product[]>; - testProductById(productId: string): Promise<Product>; - testAllCategories(): Promise<Category[]>; - testAllOrders(): Promise<Order[]>; - testOrdersByUser(id: string): Promise<Order[]>; - testOrderById(id: string): Promise<Order>; - testAllUsers(): Promise<User[]>; - testInsertOrder(order: Order): Promise<void>; - updateUser(patch: UserPatchRequest): Promise<void>; + testRandomProduct(): Promise<EndpointReturn>; + testUserById(id: string): Promise<EndpointReturn>; + testAllProducts(categoryId?: string): Promise<EndpointReturn>; + testProductById(productId: string): Promise<EndpointReturn>; + testAllCategories(): Promise<EndpointReturn>; + testAllOrders(): Promise<EndpointReturn>; + testOrdersByUser(id: string): Promise<EndpointReturn>; + testOrderById(id: string): Promise<EndpointReturn>; + testAllUsers(): Promise<EndpointReturn>; + testInsertOrder(order: Order): Promise<EndpointReturn>; + updateUser(patch: UserPatchRequest): Promise<EndpointReturn>; runSuite(iterations: number): Promise<TestResults>; } \ No newline at end of file diff --git a/src/testing.ts b/src/testing.ts index 9b62f64b33fb7797db44a1938f46310df3e8cf30..4a62b5176d453c6453de6567564bc763977fcb96 100644 --- a/src/testing.ts +++ b/src/testing.ts @@ -1,17 +1,17 @@ import RestTestSuite from "./tests/restTests"; import GrpcTestSuite from "./tests/grpcTests"; import { performance } from "perf_hooks"; -import { TestResult } from "./types"; +import { EndpointReturn, TestResult } from "./types"; import * as _ from 'lodash'; -const runTest: (func: () => any, expected?: any) => Promise<TestResult> = async (func: () => any, expected?: any) => { +const runTest: (func: () => any, expected?: any) => Promise<TestResult> = async (func: () => Promise<EndpointReturn>, expected?: any) => { const start = performance.now(); const result = await func(); const end = performance.now(); return { - ok: expected != null ? _.isMatch(result, expected) : true, + ok: expected != null ? result.ok && _.isMatch(result.payload, expected) : result.ok, time: end - start }; } diff --git a/src/tests/grpcTests.ts b/src/tests/grpcTests.ts index 24b6eb49832d94363ab806016de189f7ddb0d6fe..377bd2edc645359cd0fff97fff3259c649ff7dd4 100644 --- a/src/tests/grpcTests.ts +++ b/src/tests/grpcTests.ts @@ -18,40 +18,43 @@ export default class GrpcTestSuite implements ITestSuite { public async testRandomProduct(): Promise<any> { try { const data = await this.client.getRandomProduct({}); - return data; + return { + payload: data, + ok: true, + }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } public async testUserById(id: string): Promise<any> { try { const data = await this.client.getUser({ id }); - return data; + return { payload: data, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } public async testAllProducts(categoryId?: string): Promise<any> { try { const data = await this.client.getAllProducts({ categoryId }); - return data; + return { payload: data, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } public async testProductById(productId: string): Promise<any> { try { const data = await this.client.getProduct({ productId }); - return data; + return { payload: data, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } @@ -59,70 +62,70 @@ export default class GrpcTestSuite implements ITestSuite { public async testAllCategories(): Promise<any> { try { const data = await this.client.getAllCategories({}); - return data; + return { payload: data, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } public async testAllOrders(): Promise<any> { try { const data = await this.client.getAllOrders({}); - return data; + return { payload: data, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } public async testOrdersByUser(id: string): Promise<any> { try { const data = await this.client.getAllUserOrders({ id }); - return data; + return { payload: data, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } public async testOrderById(id: string): Promise<any> { try { const data = await this.client.getOrder({ id }); - return data; + return { payload: data, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } public async testAllUsers(): Promise<any> { try { const data = await this.client.getAllUsers({}); - return data; + return { payload: data, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } public async testInsertOrder(order: Order): Promise<any> { try { const data = await this.client.postOrder(order); - return data; + return { payload: data, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } public async updateUser(patch: UserPatchRequest): Promise<any> { try { const data = await this.client.patchAccountDetails(patch); - return data; + return { payload: data, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } diff --git a/src/tests/restTests.ts b/src/tests/restTests.ts index dc047d39e7e16cdbd4a51e36c9007fe5bda99c2d..78ed9a5951205de427307bcbf703d8127d4afe1c 100644 --- a/src/tests/restTests.ts +++ b/src/tests/restTests.ts @@ -1,5 +1,5 @@ import { ITestSuite } from '../interfaces'; -import { Category, Order, Product, TestResults, User, UserPatchRequest } from '../types'; +import { Category, EndpointReturn, Order, Product, TestResults, User, UserPatchRequest } from '../types'; import { avgRuntime } from '../testing'; import { uuid } from 'uuidv4'; import constants from '../constants'; @@ -11,115 +11,121 @@ export default class RestTestSuite implements ITestSuite { this.endpoint = `http://${ip}:3000`; } - public async testRandomProduct(): Promise<Product> { + public async testRandomProduct(): Promise<EndpointReturn> { try { const resp = await fetch(this.endpoint + "/randomproduct"); if (!resp.ok) throw new Error("Failed to fetch random product"); const json = await resp.json(); - return json; + return { + payload: json, + ok: true + }; } catch (e) { console.error(e); - return null; + return { + payload: null, + ok: false + }; } } - public async testUserById(id: string): Promise<User> { + public async testUserById(id: string): Promise<EndpointReturn> { try { const resp = await fetch(this.endpoint + "/user/" + id); if (!resp.ok) throw new Error("Failed to fetch user by id"); const json = await resp.json(); - return json; + return { payload: json, ok: true }; } catch (e) { console.error("error"); - return null; + return { payload: null, ok: false }; } } - public async testAllProducts(categoryId?: string): Promise<Product[]> { + public async testAllProducts(categoryId?: string): Promise<EndpointReturn> { try { const resp = await fetch(this.endpoint + "/products" + (categoryId ? "?categoryId=" + categoryId : "")); if (!resp.ok) throw new Error("Failed to fetch all products"); const json = await resp.json(); - return json; + return { payload: json, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } - public async testProductById(productId: string): Promise<Product> { + public async testProductById(productId: string): Promise<EndpointReturn> { try { const resp = await fetch(this.endpoint + "/product/" + productId); if (!resp.ok) throw new Error("Failed to fetch product by id"); const json = await resp.json(); - return json; + return { payload: json, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } - public async testAllCategories(): Promise<Category[]> { + public async testAllCategories(): Promise<EndpointReturn> { try { const resp = await fetch(this.endpoint + "/categories"); if (!resp.ok) throw new Error("Failed to fetch all categories"); const json = await resp.json(); - return json; + return { payload: json, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } - public async testAllOrders(): Promise<Order[]> { + public async testAllOrders(): Promise<EndpointReturn> { try { const resp = await fetch(this.endpoint + "/allorders"); if (!resp.ok) throw new Error("Failed to fetch all orders"); const json = await resp.json(); - return json; + return { payload: json, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } - public async testOrdersByUser(id: string): Promise<Order[]> { + public async testOrdersByUser(id: string): Promise<EndpointReturn> { try { const resp = await fetch(this.endpoint + "/orders?id=" + id); if (!resp.ok) throw new Error("Failed to fetch orders by user"); const json = await resp.json(); - return json; + return { payload: json, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } - public async testOrderById(id: string): Promise<Order> { + public async testOrderById(id: string): Promise<EndpointReturn> { try { const resp = await fetch(this.endpoint + "/order/" + id); if (!resp.ok) throw new Error("Failed to fetch order by id"); const json = await resp.json(); - return json; + return { payload: json, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } - public async testAllUsers(): Promise<User[]> { + public async testAllUsers(): Promise<EndpointReturn> { try { const resp = await fetch(this.endpoint + "/users"); if (!resp.ok) throw new Error("Failed to fetch all users"); const json = await resp.json(); - return json; + return { payload: json, ok: true }; } catch (e) { console.error(e); - return null; + return { payload: null, ok: false }; } } - public async testInsertOrder(order: Order): Promise<void> { + public async testInsertOrder(order: Order): Promise<EndpointReturn> { try { const resp = await fetch(this.endpoint + "/orders", { method: "POST", @@ -129,12 +135,14 @@ export default class RestTestSuite implements ITestSuite { body: JSON.stringify(order) }); if (!resp.ok) throw new Error("Failed to insert order"); + return { payload: null, ok: true } } catch (e) { console.error(e); + return { payload: null, ok: false } } } - public async updateUser(patch: UserPatchRequest): Promise<void> { + public async updateUser(patch: UserPatchRequest): Promise<EndpointReturn> { try { const resp = await fetch(this.endpoint + `/user/${patch.id}`, { method: "PATCH", @@ -144,8 +152,10 @@ export default class RestTestSuite implements ITestSuite { body: JSON.stringify(patch) }); if (!resp.ok) throw new Error("Failed to update user"); + return { payload: null, ok: true } } catch (e) { console.error(e); + return { payload: null, ok: false } } } diff --git a/src/types.ts b/src/types.ts index 90a277511149f3a11f53896119c71cbf82d40a09..c867a8c7952134f506aa0ad408385dfbcd5f84f1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -53,6 +53,11 @@ export type User = { email: string; } +export type EndpointReturn = { + ok: boolean; + payload: any; +} + export type TestResult = { ok: boolean; time: number;