Skip to content

Commit

Permalink
refactor(core): refactor testing of parameterize
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar60310 committed Aug 31, 2022
1 parent d5f772e commit c0f8d3f
Showing 1 changed file with 70 additions and 155 deletions.
@@ -1,42 +1,50 @@
import { createTestCompiler } from '../../testCompiler';

it('Should parameterize input parameters', async () => {
const queryTest = async (
template: string,
expectedQueries: string[],
expectedBinding: any[][],
customContext?: Record<string, any>
) => {
// Arrange
const { compiler, loader, builder, executor } = await createTestCompiler();
const { compiledData } = await compiler.compile(
`select * from users where id = {{ context.params.id }} or name = {{ context.params.name }}`
);
const { compiledData } = await compiler.compile(template);
builder.value.onFirstCall().resolves([{ id: 1, name: 'freda' }]);
// Action
loader.setSource('test', compiledData);
await compiler.execute('test', {
context: { params: { id: 1, name: 'freda' } },
context: customContext || { params: { id: 1, name: 'freda' } },
});
// Assert
expect(executor.createBuilder.firstCall.args[0]).toBe(
`select * from users where id = $1 or name = $2`
expectedQueries.forEach((query, index) =>
expect(executor.createBuilder.getCall(index).args[0]).toBe(query)
);
expectedBinding.forEach((binding, index) => {
expect(
Array.from(executor.createBuilder.getCall(index).args[1].values())
).toEqual(binding);
});
};

it('Should parameterize input parameters', async () => {
await queryTest(
`select * from users where id = {{ context.params.id }} or name = {{ context.params.name }}`,
[`select * from users where id = $1 or name = $2`],
[[1, 'freda']]
);
expect(executor.createBuilder.firstCall.args[1].get('$1')).toBe(1);
expect(executor.createBuilder.firstCall.args[1].get('$2')).toBe('freda');
});

it('Should parameterize all lookup or function call nodes', async () => {
// Arrange
const { compiler, loader, builder, executor } = await createTestCompiler();
const { compiledData } = await compiler.compile(
await queryTest(
`{% set someVar = context.params.id %}
{{ someVar }}
{{ someVar + 1 }}
{{ context.someFunction() }}
{{ context.someFunction() + '!' }}
{{ context.complexObj.func()().func2().a.b }}
`
);
builder.value.onFirstCall().resolves([{ id: 1, name: 'freda' }]);
// Action
loader.setSource('test', compiledData);
await compiler.execute('test', {
context: {
{{ context.complexObj.func()().func2().a.b }}`,
[`$1\n$11\n$2\n$2!\n$3`],
[[1, 'functionResult', 'complexResult']],
{
params: { id: 1, name: 'freda' },
someFunction: () => 'functionResult',
complexObj: {
Expand All @@ -46,185 +54,92 @@ it('Should parameterize all lookup or function call nodes', async () => {
}),
}),
},
},
});
// Assert
expect(executor.createBuilder.firstCall.args[0]).toBe(`$1\n$11\n$2\n$2!\n$3`);
expect(executor.createBuilder.firstCall.args[1].get('$1')).toBe(1);
expect(executor.createBuilder.firstCall.args[1].get('$2')).toBe(
'functionResult'
);
expect(executor.createBuilder.firstCall.args[1].get('$3')).toBe(
'complexResult'
}
);
});

it('Should reuse the parameter with same values', async () => {
// Arrange
const { compiler, loader, builder, executor } = await createTestCompiler();
const { compiledData } = await compiler.compile(
`{{ context.params.id }}{{ context.params.name }}{{ context.params.id }}`
await queryTest(
`{{ context.params.id }}{{ context.params.name }}{{ context.params.id }}`,
[`$1$2$1`],
[[1, 'freda']]
);
builder.value.onFirstCall().resolves([{ id: 1, name: 'freda' }]);
// Action
loader.setSource('test', compiledData);
await compiler.execute('test', {
context: { params: { id: 1, name: 'freda' } },
});
// Assert
expect(executor.createBuilder.firstCall.args[0]).toBe(`$1$2$1`);
expect(executor.createBuilder.firstCall.args[1].get('$1')).toBe(1);
expect(executor.createBuilder.firstCall.args[1].get('$2')).toBe('freda');
});

it('Should reset the index with new request', async () => {
// Arrange
const { compiler, loader, builder, executor } = await createTestCompiler();
const { compiledData } = await compiler.compile(
`
{% req user %}
await queryTest(
`{% req user %}
select * from users where name = {{ context.params.name }}
{% endreq %}
select * from groups where userId = {{ user.value()[0].id }}
`
select * from groups where userId = {{ user.value()[0].id }}`,
[
`select * from users where name = $1`,
`select * from groups where userId = $1`,
],
[['freda'], [1]]
);
builder.value.onFirstCall().resolves([{ id: 1, name: 'freda' }]);
// Action
loader.setSource('test', compiledData);
await compiler.execute('test', {
context: { params: { name: 'freda' } },
});
// Assert
expect(executor.createBuilder.firstCall.args[0]).toBe(
`select * from users where name = $1`
);
expect(executor.createBuilder.firstCall.args[1].get('$1')).toBe('freda');
expect(executor.createBuilder.secondCall.args[0]).toBe(
`select * from groups where userId = $1`
);
expect(executor.createBuilder.secondCall.args[1].get('$1')).toBe(1);
});

it('Should use raw value to handle application logic', async () => {
// Arrange
const { compiler, loader, builder, executor } = await createTestCompiler();
const { compiledData } = await compiler.compile(
await queryTest(
`{% if context.params.id == 1 %}This should be rendered - {{ context.params.id }}{% endif %}
{% set someVar = context.params.id + 1 %}
{% if someVar == 2 %}This should be rendered - {{ someVar }}{% endif %}
{% set someArray = [5,6,7] %}
{% for val in someArray %}
{{ val }}
{% endfor %}
`
);
builder.value.onFirstCall().resolves([{ id: 1, name: 'freda' }]);
// Action
loader.setSource('test', compiledData);
await compiler.execute('test', {
context: { params: { id: 1 } },
});
// Assert
expect(executor.createBuilder.firstCall.args[0]).toBe(
`This should be rendered - $1
`,
[
`This should be rendered - $1
This should be rendered - $2
$3
$4
$5`
$5`,
],
[[1, 2, 5, 6, 7]]
);
expect(executor.createBuilder.firstCall.args[1].get('$1')).toBe(1);
expect(executor.createBuilder.firstCall.args[1].get('$2')).toBe(2);
expect(executor.createBuilder.firstCall.args[1].get('$3')).toBe(5);
expect(executor.createBuilder.firstCall.args[1].get('$4')).toBe(6);
expect(executor.createBuilder.firstCall.args[1].get('$5')).toBe(7);
});

it('The binding keys should be in order which they are used', async () => {
// Arrange
const { compiler, loader, builder, executor } = await createTestCompiler();
const { compiledData } = await compiler.compile(
`{{ context.params.name }}{{ context.params.id }}`
it('The binding values should be in order which they are used', async () => {
await queryTest(
`{{ context.params.name }}{{ context.params.id }}`,
[`$1$2`],
[['freda', 1]]
);
builder.value.onFirstCall().resolves([{ id: 1, name: 'freda' }]);
// Action
loader.setSource('test', compiledData);
await compiler.execute('test', {
context: { params: { id: 1, name: 'freda' } },
});
// Assert
expect(Array.from(executor.createBuilder.firstCall.args[1].keys())).toEqual([
'$1',
'$2',
]);
});

it('Should render raw value when using raw filter', async () => {
// Arrange
const { compiler, loader, builder, executor } = await createTestCompiler();
const { compiledData } = await compiler.compile(
`{{ context.params.id | raw }}{{ context.params.name | upper | raw }}`
await queryTest(
`{{ context.params.id | raw }}{{ context.params.name | upper | raw }}`,
[`1FREDA`],
[]
);
builder.value.onFirstCall().resolves([{ id: 1, name: 'freda' }]);
// Action
loader.setSource('test', compiledData);
await compiler.execute('test', {
context: { params: { id: 1, name: 'freda' } },
});
// Assert
expect(executor.createBuilder.firstCall.args[0]).toBe('1FREDA');
});

it('Raw value should be wrapped again when accessing its children', async () => {
// Arrange
const { compiler, loader, builder, executor } = await createTestCompiler();
const { compiledData } = await compiler.compile(
`{{ (context.params.data | raw).name }}`
await queryTest(
`{{ (context.params.data | raw).name }}`,
[`$1`],
[['freda']],
{ params: { data: { id: 1, name: 'freda' } } }
);
builder.value.onFirstCall().resolves([{ id: 1, name: 'freda' }]);
// Action
loader.setSource('test', compiledData);
await compiler.execute('test', {
context: { params: { data: { id: 1, name: 'freda' } } },
});
// Assert
expect(executor.createBuilder.firstCall.args[0]).toBe('$1');
expect(executor.createBuilder.firstCall.args[1].get('$1')).toBe('freda');
});

it('Raw value should be wrapped again when raw filter is chaining again', async () => {
// Arrange
const { compiler, loader, builder, executor } = await createTestCompiler();
const { compiledData } = await compiler.compile(
`{{ context.params.name | raw | upper }}`
await queryTest(
`{{ context.params.name | raw | upper }}`,
[`$1`],
[['FREDA']]
);
builder.value.onFirstCall().resolves([{ id: 1, name: 'freda' }]);
// Action
loader.setSource('test', compiledData);
await compiler.execute('test', {
context: { params: { name: 'freda' } },
});
// Assert
expect(executor.createBuilder.firstCall.args[0]).toBe('$1');
expect(executor.createBuilder.firstCall.args[1].get('$1')).toBe('FREDA');
});

it('Raw value should be wrapped again when it is assigned to variables and is rendered', async () => {
// Arrange
const { compiler, loader, builder, executor } = await createTestCompiler();
const { compiledData } = await compiler.compile(
await queryTest(
`{% set someVar = (context.params.name | upper | raw) %}
{% if someVar == "FREDA" %}This should be rendered{% endif %}
{{ someVar }}`
);
builder.value.onFirstCall().resolves([{ id: 1, name: 'freda' }]);
// Action
loader.setSource('test', compiledData);
await compiler.execute('test', {
context: { params: { name: 'freda' } },
});
// Assert
expect(executor.createBuilder.firstCall.args[0]).toBe(
'This should be rendered\n$1'
{{ someVar }}`,
[`This should be rendered\n$1`],
[['FREDA']]
);
expect(executor.createBuilder.firstCall.args[1].get('$1')).toBe('FREDA');
});

0 comments on commit c0f8d3f

Please sign in to comment.