Skip to content

Commit

Permalink
chore: adding relational snapshot tests for non-id primary keys
Browse files Browse the repository at this point in the history
  • Loading branch information
alharris-at committed Feb 27, 2023
1 parent a256b6d commit 9b886fc
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,33 @@ test('verify generated templates', () => {
});
});

test('verify generated templates with non-id named primary key', () => {
// SETUP
const schema = parse(`
type Tomatoes {
tomatoId: String
name: String
}
`);
let simpleStringFieldMap = new Map<string, string[]>();
let simpleIntFieldMap = new Map<string, string[]>();
let simplePrimaryKeyMap = new Map<string, string>();
let simplePrimaryKeyTypeMap = new Map<string, string>();

simplePrimaryKeyMap.set('Tomatoes', 'tomatoId');
simplePrimaryKeyTypeMap.set('Tomatoes', 'String');
const context = new TemplateContext(schema, simplePrimaryKeyMap, simpleStringFieldMap, simpleIntFieldMap, simplePrimaryKeyTypeMap);
const generator = new RelationalDBResolverGenerator(context);
generator.createRelationalResolvers('testFilePath', true);
expect(writeFileSync_mock.mock.calls.length).toBe(10);
writeFileSync_mock.mock.calls.forEach(call => {
expect(call.length).toBe(3);
expect(call[0]).toMatchSnapshot();
expect(call[1]).toMatchSnapshot();
expect(call[2]).toBe('utf8');
});
});

test('verify generated templates using a Int primary key', () => {
// SETUP
const schema = parse(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,127 @@ exports[`verify generated templates using a Int primary key 19`] = `"testFilePat

exports[`verify generated templates using a Int primary key 20`] = `"$utils.toJson($utils.rds.toJsonObject($ctx.result)[0])"`;

exports[`verify generated templates with non-id named primary key 1`] = `"testFilePath/Mutation.createTomatoes.req.vtl"`;

exports[`verify generated templates with non-id named primary key 2`] = `
"#set( $cols = [] )
#set( $vals = [] )
#set( $variableMap = {} )
#foreach( $entry in $ctx.args.createTomatoesInput.keySet() )
#set( $discard = $cols.add($entry) )
#set( $discard = $vals.add(\\":$entry\\") )
$util.qr($variableMap.put(\\":$entry\\", $ctx.args.createTomatoesInput[$entry]))
#end
#set( $valStr = $vals.toString().replace(\\"[\\",\\"(\\").replace(\\"]\\",\\")\\") )
#set( $colStr = $cols.toString().replace(\\"[\\",\\"(\\").replace(\\"]\\",\\")\\") )
{
\\"version\\": \\"2018-05-29\\",
\\"statements\\": [\\"INSERT INTO Tomatoes $colStr VALUES $valStr\\", \\"SELECT * FROM Tomatoes WHERE tomatoId=:tomatoId\\"],
\\"variableMap\\": $util.toJson($variableMap)
}"
`;

exports[`verify generated templates with non-id named primary key 3`] = `"testFilePath/Mutation.createTomatoes.res.vtl"`;

exports[`verify generated templates with non-id named primary key 4`] = `"$utils.toJson($utils.parseJson($utils.rds.toJsonString($ctx.result))[1][0])"`;

exports[`verify generated templates with non-id named primary key 5`] = `"testFilePath/Query.getTomatoes.req.vtl"`;

exports[`verify generated templates with non-id named primary key 6`] = `
"#set( $variableMap = {} )
$util.qr($variableMap.put(\\":tomatoId\\", \\"$ctx.args.tomatoId\\"))
{
\\"version\\": \\"2018-05-29\\",
\\"statements\\": [\\"SELECT * FROM Tomatoes WHERE tomatoId=:tomatoId\\"],
\\"variableMap\\": $util.toJson($variableMap)
}"
`;

exports[`verify generated templates with non-id named primary key 7`] = `"testFilePath/Query.getTomatoes.res.vtl"`;

exports[`verify generated templates with non-id named primary key 8`] = `
"#set( $output = $utils.rds.toJsonObject($ctx.result) )
#if( $output.isEmpty() )
$util.error(\\"Invalid response from RDS DataSource. See info for the full response.\\", \\"InvalidResponse\\", {}, $output)
#end
#set( $output = $output[0] )
#if( $output.isEmpty() )
#return
#end
$utils.toJson($output[0])"
`;

exports[`verify generated templates with non-id named primary key 9`] = `"testFilePath/Mutation.updateTomatoes.req.vtl"`;

exports[`verify generated templates with non-id named primary key 10`] = `
"#set( $updateList = {} )
#set( $variableMap = {} )
#foreach( $entry in $ctx.args.updateTomatoesInput.keySet() )
$util.qr($variableMap.put(\\":$entry\\", $ctx.args.updateTomatoesInput[$entry]))
#set( $discard = $updateList.put($entry, \\":$entry\\") )
#end
#set( $update = $updateList.toString().replace(\\"{\\",\\"\\").replace(\\"}\\",\\"\\") )
{
\\"version\\": \\"2018-05-29\\",
\\"statements\\": [\\"UPDATE Tomatoes SET $update WHERE tomatoId=:tomatoId\\", \\"SELECT * FROM Tomatoes WHERE tomatoId=:tomatoId\\"],
\\"variableMap\\": $util.toJson($variableMap)
}"
`;

exports[`verify generated templates with non-id named primary key 11`] = `"testFilePath/Mutation.updateTomatoes.res.vtl"`;

exports[`verify generated templates with non-id named primary key 12`] = `
"#set( $output = $utils.rds.toJsonObject($ctx.result) )
#if( $output.length() < 2 )
$util.error(\\"Invalid response from RDS DataSource. See info for the full response.\\", \\"InvalidResponse\\", {}, $output)
#end
#set( $output = $output[1] )
#if( $output.isEmpty() )
#return
#end
$utils.toJson($output[0])"
`;

exports[`verify generated templates with non-id named primary key 13`] = `"testFilePath/Mutation.deleteTomatoes.req.vtl"`;

exports[`verify generated templates with non-id named primary key 14`] = `
"#set( $variableMap = {} )
$util.qr($variableMap.put(\\":tomatoId\\", \\"$ctx.args.tomatoId\\"))
{
\\"version\\": \\"2018-05-29\\",
\\"statements\\": [\\"SELECT * FROM Tomatoes WHERE tomatoId=:tomatoId\\", \\"DELETE FROM Tomatoes WHERE tomatoId=:tomatoId\\"],
\\"variableMap\\": $util.toJson($variableMap)
}"
`;

exports[`verify generated templates with non-id named primary key 15`] = `"testFilePath/Mutation.deleteTomatoes.res.vtl"`;

exports[`verify generated templates with non-id named primary key 16`] = `
"#set( $output = $utils.rds.toJsonObject($ctx.result) )
#if( $output.isEmpty() )
$util.error(\\"Invalid response from RDS DataSource. See info for the full response.\\", \\"InvalidResponse\\", {}, $output)
#end
#set( $output = $output[0] )
#if( $output.isEmpty() )
#return
#end
$utils.toJson($output[0])"
`;

exports[`verify generated templates with non-id named primary key 17`] = `"testFilePath/Query.listTomatoes.req.vtl"`;

exports[`verify generated templates with non-id named primary key 18`] = `
"{
\\"version\\": \\"2018-05-29\\",
\\"statements\\": [\\"SELECT * FROM Tomatoes\\"],
\\"variableMap\\": {}
}"
`;

exports[`verify generated templates with non-id named primary key 19`] = `"testFilePath/Query.listTomatoes.res.vtl"`;

exports[`verify generated templates with non-id named primary key 20`] = `"$utils.toJson($utils.rds.toJsonObject($ctx.result)[0])"`;

exports[`verify generated templates with old pluralization 1`] = `"testFilePath/Mutation.createApples.req.vtl"`;

exports[`verify generated templates with old pluralization 2`] = `
Expand Down

0 comments on commit 9b886fc

Please sign in to comment.