From eabfa4ada07947f096e398b61ffc7573e1482b13 Mon Sep 17 00:00:00 2001 From: Simon Walker Date: Wed, 18 Oct 2023 16:54:23 +0100 Subject: [PATCH] Add resolver test for query expression --- .../__snapshots__/resolvers.test.js.snap | 39 ++++++++++++ __tests__/helpers.js | 50 ++++++++++----- __tests__/resolvers.test.js | 61 +++++++++++++++++++ 3 files changed, 136 insertions(+), 14 deletions(-) create mode 100644 __tests__/__snapshots__/resolvers.test.js.snap create mode 100644 __tests__/resolvers.test.js diff --git a/__tests__/__snapshots__/resolvers.test.js.snap b/__tests__/__snapshots__/resolvers.test.js.snap new file mode 100644 index 0000000..aef6a39 --- /dev/null +++ b/__tests__/__snapshots__/resolvers.test.js.snap @@ -0,0 +1,39 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`dynamodb resolvers something 1`] = ` +{ + "filter": { + "expression": "#shift = :shift", + "expressionNames": { + "#shift": "shift", + }, + "expressionValues": { + ":shift": { + "N": 10, + }, + }, + }, + "index": "nameIndex", + "operation": "Query", + "query": { + "expression": "#name = :name", + "expressionNames": { + "#name": "name", + }, + "expressionValues": { + ":name": { + "S": "test", + }, + }, + }, + "select": "ALL_ATTRIBUTES", +} +`; + +exports[`dynamodb resolvers something 2`] = ` +[ + { + "a": 10, + }, +] +`; diff --git a/__tests__/helpers.js b/__tests__/helpers.js index fb07970..fdbc93e 100644 --- a/__tests__/helpers.js +++ b/__tests__/helpers.js @@ -6,7 +6,7 @@ import * as ddb from "../dynamodb"; let client = null; -const runOnAWS = async (s, context) => { +const runResolverFunctionOnAWS = async (code, context, functionName) => { if (!client) { client = new AppSyncClient(); } @@ -15,22 +15,10 @@ const runOnAWS = async (s, context) => { context = {}; } - const code = ` - import { util } from '@aws-appsync/utils'; - import * as ddb from '@aws-appsync/utils/dynamodb'; - - export function request(ctx) { - return ${s}; - } - - export function response(ctx) { - } - `; - const command = new EvaluateCodeCommand({ code, context: JSON.stringify(context), - function: "request", + function: functionName, runtime: { name: "APPSYNC_JS", runtimeVersion: "1.0.0", @@ -43,8 +31,42 @@ const runOnAWS = async (s, context) => { } catch (e) { console.error("invalid json", result); } +}; + +const runOnAWS = async (s, context) => { + const code = ` + import { util } from '@aws-appsync/utils'; + import * as ddb from '@aws-appsync/utils/dynamodb'; + + export function request(ctx) { + return ${s}; + } + + export function response(ctx) { + } + `; + + return await runResolverFunctionOnAWS(code, context, "request"); + } +export const checkResolverValid = async (code, context, functionName) => { + let result; + if (process.env.TEST_TARGET === "AWS_CLOUD") { + const fullCode = `import { util } from '@aws-appsync/utils';\n` + code; + result = await runResolverFunctionOnAWS(fullCode, context, functionName); + } else { + const fullCode = `import { util } from '..';\n` + code; + const encodedJs = encodeURIComponent(fullCode); + const dataUri = 'data:text/javascript;charset=utf-8,' + encodedJs; + const module = await import(dataUri); + + const fn = module[functionName]; + result = fn(context); + } + expect(result).toMatchSnapshot(); +}; + // If TEST_TARGET is AWS_CLOUD then run the check against AWS. Otherwise, run locally. export const checkValid = async (s, context) => { let result; diff --git a/__tests__/resolvers.test.js b/__tests__/resolvers.test.js new file mode 100644 index 0000000..9ed97de --- /dev/null +++ b/__tests__/resolvers.test.js @@ -0,0 +1,61 @@ +/* test full resolver pipelines */ + +import { checkResolverValid } from "./helpers"; +import { util } from ".."; + +describe("dynamodb resolvers", () => { + test("something", async () => { + const code = ` + export function request(ctx) { + return { + operation: "Query", + index: "nameIndex", + select : "ALL_ATTRIBUTES", + query: { + "expression" : "#name = :name", + "expressionNames": { + "#name": "name", + }, + "expressionValues" : { + ":name" : util.dynamodb.toDynamoDB(ctx.arguments.filter.line), + } + }, + filter: { + "expression" : "#shift = :shift", + "expressionNames": { + "#shift": "shift", + }, + "expressionValues" : { + ":shift" : util.dynamodb.toDynamoDB(ctx.arguments.filter.shift), + } + }, + }; + } + + export function response(ctx) { + return ctx.result.items; + } + `; + + const requestContext = { + arguments: { + filter: { + line: "test", + shift: 10, + }, + }, + }; + + await checkResolverValid(code, requestContext, "request"); + + const responseContext = { + result: { + items: [ + { a: 10 }, + ], + }, + }; + + await checkResolverValid(code, responseContext, "response"); + }); +});