Skip to content

Commit

Permalink
Merge pull request #4 from localstack/resolver-tests
Browse files Browse the repository at this point in the history
Add resolver test for query expression
  • Loading branch information
simonrw authored Oct 19, 2023
2 parents 8169c36 + eabfa4a commit 79b2682
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 14 deletions.
39 changes: 39 additions & 0 deletions __tests__/__snapshots__/resolvers.test.js.snap
Original file line number Diff line number Diff line change
@@ -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,
},
]
`;
50 changes: 36 additions & 14 deletions __tests__/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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",
Expand All @@ -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, postProcess) => {
let result;
Expand Down
61 changes: 61 additions & 0 deletions __tests__/resolvers.test.js
Original file line number Diff line number Diff line change
@@ -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");
});
});

0 comments on commit 79b2682

Please sign in to comment.