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

correctness testing

parent a318f742
No related branches found
No related tags found
No related merge requests found
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
} }
.fail { .fail {
color: orange; color: red;
} }
tr:nth-child(even) { tr:nth-child(even) {
......
import * as categoriesOracle from './oracles/categories.json'; import categoriesOracle from './oracles/categories.json';
import * as ordersOracle from './oracles/orders.json'; import ordersOracle from './oracles/orders.json';
import * as productsOracle from './oracles/products.json'; import productsOracle from './oracles/products.json';
import * as usersOracle from './oracles/users.json'; import usersOracle from './oracles/users.json';
const constants = { const constants = {
TEST_USER_ID: usersOracle[0].id, TEST_USER_ID: usersOracle[0].id,
......
import { UserPatchRequest } from "./compiled_proto/app"; 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 { export interface ITestSuite {
testRandomProduct(): Promise<Product>; testRandomProduct(): Promise<EndpointReturn>;
testUserById(id: string): Promise<User>; testUserById(id: string): Promise<EndpointReturn>;
testAllProducts(categoryId?: string): Promise<Product[]>; testAllProducts(categoryId?: string): Promise<EndpointReturn>;
testProductById(productId: string): Promise<Product>; testProductById(productId: string): Promise<EndpointReturn>;
testAllCategories(): Promise<Category[]>; testAllCategories(): Promise<EndpointReturn>;
testAllOrders(): Promise<Order[]>; testAllOrders(): Promise<EndpointReturn>;
testOrdersByUser(id: string): Promise<Order[]>; testOrdersByUser(id: string): Promise<EndpointReturn>;
testOrderById(id: string): Promise<Order>; testOrderById(id: string): Promise<EndpointReturn>;
testAllUsers(): Promise<User[]>; testAllUsers(): Promise<EndpointReturn>;
testInsertOrder(order: Order): Promise<void>; testInsertOrder(order: Order): Promise<EndpointReturn>;
updateUser(patch: UserPatchRequest): Promise<void>; updateUser(patch: UserPatchRequest): Promise<EndpointReturn>;
runSuite(iterations: number): Promise<TestResults>; runSuite(iterations: number): Promise<TestResults>;
} }
\ No newline at end of file
import RestTestSuite from "./tests/restTests"; import RestTestSuite from "./tests/restTests";
import GrpcTestSuite from "./tests/grpcTests"; import GrpcTestSuite from "./tests/grpcTests";
import { performance } from "perf_hooks"; import { performance } from "perf_hooks";
import { TestResult } from "./types"; import { EndpointReturn, TestResult } from "./types";
import * as _ from 'lodash'; 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 start = performance.now();
const result = await func(); const result = await func();
const end = performance.now(); const end = performance.now();
return { return {
ok: expected != null ? _.isMatch(result, expected) : true, ok: expected != null ? result.ok && _.isMatch(result.payload, expected) : result.ok,
time: end - start time: end - start
}; };
} }
......
...@@ -18,40 +18,43 @@ export default class GrpcTestSuite implements ITestSuite { ...@@ -18,40 +18,43 @@ export default class GrpcTestSuite implements ITestSuite {
public async testRandomProduct(): Promise<any> { public async testRandomProduct(): Promise<any> {
try { try {
const data = await this.client.getRandomProduct({}); const data = await this.client.getRandomProduct({});
return data; return {
payload: data,
ok: true,
};
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return null; return { payload: null, ok: false };
} }
} }
public async testUserById(id: string): Promise<any> { public async testUserById(id: string): Promise<any> {
try { try {
const data = await this.client.getUser({ id }); const data = await this.client.getUser({ id });
return data; return { payload: data, ok: true };
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return null; return { payload: null, ok: false };
} }
} }
public async testAllProducts(categoryId?: string): Promise<any> { public async testAllProducts(categoryId?: string): Promise<any> {
try { try {
const data = await this.client.getAllProducts({ categoryId }); const data = await this.client.getAllProducts({ categoryId });
return data; return { payload: data, ok: true };
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return null; return { payload: null, ok: false };
} }
} }
public async testProductById(productId: string): Promise<any> { public async testProductById(productId: string): Promise<any> {
try { try {
const data = await this.client.getProduct({ productId }); const data = await this.client.getProduct({ productId });
return data; return { payload: data, ok: true };
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return null; return { payload: null, ok: false };
} }
} }
...@@ -59,70 +62,70 @@ export default class GrpcTestSuite implements ITestSuite { ...@@ -59,70 +62,70 @@ export default class GrpcTestSuite implements ITestSuite {
public async testAllCategories(): Promise<any> { public async testAllCategories(): Promise<any> {
try { try {
const data = await this.client.getAllCategories({}); const data = await this.client.getAllCategories({});
return data; return { payload: data, ok: true };
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return null; return { payload: null, ok: false };
} }
} }
public async testAllOrders(): Promise<any> { public async testAllOrders(): Promise<any> {
try { try {
const data = await this.client.getAllOrders({}); const data = await this.client.getAllOrders({});
return data; return { payload: data, ok: true };
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return null; return { payload: null, ok: false };
} }
} }
public async testOrdersByUser(id: string): Promise<any> { public async testOrdersByUser(id: string): Promise<any> {
try { try {
const data = await this.client.getAllUserOrders({ id }); const data = await this.client.getAllUserOrders({ id });
return data; return { payload: data, ok: true };
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return null; return { payload: null, ok: false };
} }
} }
public async testOrderById(id: string): Promise<any> { public async testOrderById(id: string): Promise<any> {
try { try {
const data = await this.client.getOrder({ id }); const data = await this.client.getOrder({ id });
return data; return { payload: data, ok: true };
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return null; return { payload: null, ok: false };
} }
} }
public async testAllUsers(): Promise<any> { public async testAllUsers(): Promise<any> {
try { try {
const data = await this.client.getAllUsers({}); const data = await this.client.getAllUsers({});
return data; return { payload: data, ok: true };
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return null; return { payload: null, ok: false };
} }
} }
public async testInsertOrder(order: Order): Promise<any> { public async testInsertOrder(order: Order): Promise<any> {
try { try {
const data = await this.client.postOrder(order); const data = await this.client.postOrder(order);
return data; return { payload: data, ok: true };
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return null; return { payload: null, ok: false };
} }
} }
public async updateUser(patch: UserPatchRequest): Promise<any> { public async updateUser(patch: UserPatchRequest): Promise<any> {
try { try {
const data = await this.client.patchAccountDetails(patch); const data = await this.client.patchAccountDetails(patch);
return data; return { payload: data, ok: true };
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return null; return { payload: null, ok: false };
} }
} }
......
import { ITestSuite } from '../interfaces'; 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 { avgRuntime } from '../testing';
import { uuid } from 'uuidv4'; import { uuid } from 'uuidv4';
import constants from '../constants'; import constants from '../constants';
...@@ -11,115 +11,121 @@ export default class RestTestSuite implements ITestSuite { ...@@ -11,115 +11,121 @@ export default class RestTestSuite implements ITestSuite {
this.endpoint = `http://${ip}:3000`; this.endpoint = `http://${ip}:3000`;
} }
public async testRandomProduct(): Promise<Product> { public async testRandomProduct(): Promise<EndpointReturn> {
try { try {
const resp = await fetch(this.endpoint + "/randomproduct"); const resp = await fetch(this.endpoint + "/randomproduct");
if (!resp.ok) throw new Error("Failed to fetch random product"); if (!resp.ok) throw new Error("Failed to fetch random product");
const json = await resp.json(); const json = await resp.json();
return json; return {
payload: json,
ok: true
};
} catch (e) { } catch (e) {
console.error(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 { try {
const resp = await fetch(this.endpoint + "/user/" + id); const resp = await fetch(this.endpoint + "/user/" + id);
if (!resp.ok) throw new Error("Failed to fetch user by id"); if (!resp.ok) throw new Error("Failed to fetch user by id");
const json = await resp.json(); const json = await resp.json();
return json; return { payload: json, ok: true };
} catch (e) { } catch (e) {
console.error("error"); 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 { try {
const resp = await fetch(this.endpoint + "/products" + (categoryId ? "?categoryId=" + categoryId : "")); const resp = await fetch(this.endpoint + "/products" + (categoryId ? "?categoryId=" + categoryId : ""));
if (!resp.ok) throw new Error("Failed to fetch all products"); if (!resp.ok) throw new Error("Failed to fetch all products");
const json = await resp.json(); const json = await resp.json();
return json; return { payload: json, ok: true };
} catch (e) { } catch (e) {
console.error(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 { try {
const resp = await fetch(this.endpoint + "/product/" + productId); const resp = await fetch(this.endpoint + "/product/" + productId);
if (!resp.ok) throw new Error("Failed to fetch product by id"); if (!resp.ok) throw new Error("Failed to fetch product by id");
const json = await resp.json(); const json = await resp.json();
return json; return { payload: json, ok: true };
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return null; return { payload: null, ok: false };
} }
} }
public async testAllCategories(): Promise<Category[]> { public async testAllCategories(): Promise<EndpointReturn> {
try { try {
const resp = await fetch(this.endpoint + "/categories"); const resp = await fetch(this.endpoint + "/categories");
if (!resp.ok) throw new Error("Failed to fetch all categories"); if (!resp.ok) throw new Error("Failed to fetch all categories");
const json = await resp.json(); const json = await resp.json();
return json; return { payload: json, ok: true };
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return null; return { payload: null, ok: false };
} }
} }
public async testAllOrders(): Promise<Order[]> { public async testAllOrders(): Promise<EndpointReturn> {
try { try {
const resp = await fetch(this.endpoint + "/allorders"); const resp = await fetch(this.endpoint + "/allorders");
if (!resp.ok) throw new Error("Failed to fetch all orders"); if (!resp.ok) throw new Error("Failed to fetch all orders");
const json = await resp.json(); const json = await resp.json();
return json; return { payload: json, ok: true };
} catch (e) { } catch (e) {
console.error(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 { try {
const resp = await fetch(this.endpoint + "/orders?id=" + id); const resp = await fetch(this.endpoint + "/orders?id=" + id);
if (!resp.ok) throw new Error("Failed to fetch orders by user"); if (!resp.ok) throw new Error("Failed to fetch orders by user");
const json = await resp.json(); const json = await resp.json();
return json; return { payload: json, ok: true };
} catch (e) { } catch (e) {
console.error(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 { try {
const resp = await fetch(this.endpoint + "/order/" + id); const resp = await fetch(this.endpoint + "/order/" + id);
if (!resp.ok) throw new Error("Failed to fetch order by id"); if (!resp.ok) throw new Error("Failed to fetch order by id");
const json = await resp.json(); const json = await resp.json();
return json; return { payload: json, ok: true };
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return null; return { payload: null, ok: false };
} }
} }
public async testAllUsers(): Promise<User[]> { public async testAllUsers(): Promise<EndpointReturn> {
try { try {
const resp = await fetch(this.endpoint + "/users"); const resp = await fetch(this.endpoint + "/users");
if (!resp.ok) throw new Error("Failed to fetch all users"); if (!resp.ok) throw new Error("Failed to fetch all users");
const json = await resp.json(); const json = await resp.json();
return json; return { payload: json, ok: true };
} catch (e) { } catch (e) {
console.error(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 { try {
const resp = await fetch(this.endpoint + "/orders", { const resp = await fetch(this.endpoint + "/orders", {
method: "POST", method: "POST",
...@@ -129,12 +135,14 @@ export default class RestTestSuite implements ITestSuite { ...@@ -129,12 +135,14 @@ export default class RestTestSuite implements ITestSuite {
body: JSON.stringify(order) body: JSON.stringify(order)
}); });
if (!resp.ok) throw new Error("Failed to insert order"); if (!resp.ok) throw new Error("Failed to insert order");
return { payload: null, ok: true }
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return { payload: null, ok: false }
} }
} }
public async updateUser(patch: UserPatchRequest): Promise<void> { public async updateUser(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",
...@@ -144,8 +152,10 @@ export default class RestTestSuite implements ITestSuite { ...@@ -144,8 +152,10 @@ export default class RestTestSuite implements ITestSuite {
body: JSON.stringify(patch) body: JSON.stringify(patch)
}); });
if (!resp.ok) throw new Error("Failed to update user"); if (!resp.ok) throw new Error("Failed to update user");
return { payload: null, ok: true }
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return { payload: null, ok: false }
} }
} }
......
...@@ -53,6 +53,11 @@ export type User = { ...@@ -53,6 +53,11 @@ export type User = {
email: string; email: string;
} }
export type EndpointReturn = {
ok: boolean;
payload: any;
}
export type TestResult = { export type TestResult = {
ok: boolean; ok: boolean;
time: number; time: number;
......
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