UNPKG

flora-exception

Version:

An exception system for FQL.

236 lines (184 loc) 7.12 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FxSuiteA = void 0; const faunadb_1 = require("faunadb"); const fauna_test_setup_1 = require("fauna-test-setup"); const { IsNumber, IsArray, Sum, Concat, ToString, Add, IsString } = faunadb_1.query; const FxSuiteA = () => { describe("Flora exceptions basic functionality", () => { let db; beforeAll(() => __awaiter(void 0, void 0, void 0, function* () { db = yield (0, fauna_test_setup_1.FaunaTestDb)(); })); test("Simple Fx", () => __awaiter(void 0, void 0, void 0, function* () { /*const CoolFunc = (a : number, b : number)=>{ return Fx( [ [a, IsNumber as unknown as ()=>boolean], [b, IsNumber as unknown as ()=>boolean] ], $Number, (a ,b)=>{ return Add(a, b) as unknown as number } ) } const result = await db.client.query<FloraExceptionI[]>(Flora( CoolFunc(2, 2) )); expect(result).toBe(4);*/ })); /*test("Array Fx", async ()=>{ const CoolFunc = (a : number[])=>{ return Fx( [ [a, IsArray as unknown as ()=>boolean], ], $Number, (a)=>{ return Sum(a) as unknown as number } ) } const result = await db.client.query<FloraExceptionI[]>(Flora( CoolFunc([2, 2, 2, 2]) )); expect(result).toBe(8); }) test("Mixed Fx", async ()=>{ const CoolFunc = (a : number[], b : string)=>{ return Fx( [ [a, IsArray as unknown as ()=>boolean], [b, IsString as unknown as ()=>boolean] ], $String, (a, b)=>{ return Concat([ToString(Sum(a)), b], "") as unknown as string } ) } const result = await db.client.query<FloraExceptionI[]>(Flora( CoolFunc( [2, 2, 2, 2], " is Liam's score." ) )); expect(result).toBe("8 is Liam's score."); }) test("Can get extracted Exceptions", async()=>{ const args : FxArgI<any>[] = [ [2, IsNumber as unknown as ()=>boolean], ["3", IsString as unknown as ()=>boolean], [[4, 5, 6], IsString as unknown as ()=>boolean] ] const result = await db.client.query<FloraExceptionI[]>(Flora( extractArgs(args, "Here") )); const secondResult = await db.client.query<FloraExceptionI[]>(Flora( Yield({ args : extractArgs(args, "Here"), expr : (a ,b, c)=>{ return [a, b, c] } }) )); }) test("Exception", async ()=>{ const ExceptionFunc = (a : number[])=>{ return Fx( [ [a, IsString as unknown as ()=>boolean], ], $String, (a)=>{ return Concat([a, " great time."], "") as unknown as string } ) } const result = await db.client.query<FloraExceptionI[]>(Flora( ExceptionFunc( [2, 2, 2, 2], ) )); expect(isFloraException(result)).toBe(true); }) test("Complex Exception", async ()=>{ const ExceptionFunc = (a : number[], b : string)=>{ return Fx( [ [a, IsArray as unknown as ()=>boolean], [b, IsArray as unknown as ()=>boolean] ], $String, (a, b)=>{ return Concat([ToString(Sum(a)), b], "") as unknown as string } ) } const result = await db.client.query<FloraExceptionI[]>(Flora( ExceptionFunc( [2, 2, 2, 2], " a thing" ) )); }) test("Basic mfx", async()=>{ const FloraAdd = mFx( [$Number, $Number], $Number, (a , b)=>{ return Add(a, b) as unknown as number } ) const result = await db.client.query(Flora( FloraAdd(2, 2) )); expect(result).toBe(4); }) test("Failed mfx", async()=>{ const FloraAdd = mFx( [$Number, $Number], $Number, (a , b)=>{ return Add(a, b) as unknown as number } ) const result = await db.client.query(Flora( FloraAdd(2, "hello" as unknown as number) )); expect(isFloraException(result)).toBe(true); }) test("Return exception", async ()=>{ const FloraAdd = mFx( [$Number, $Number], $Number, (a , b)=>{ return "hello" as unknown as number } ) const result = await db.client.query(Flora( FloraAdd(2, 2) )); expect(isFloraException(result)).toBe(true); }) test("Peformance mode", async ()=>{ const FloraAdd = mFx( [$Number, $Number], $Number, (a , b)=>{ return "hello" as unknown as number } ) // togglePerformance(true); console.log(JSON.stringify(FloraAdd(2, 2))) })*/ }); }; exports.FxSuiteA = FxSuiteA; (0, exports.FxSuiteA)();