-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathbuilder.spec.ts
147 lines (132 loc) · 4.09 KB
/
builder.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { arrayToStream, streamToArray } from '@vulcan-sql/core';
import { createTestCompiler } from '../../testCompiler';
it('Extension should execute correct query and set/export the variable', async () => {
// Arrange
const { compiler, loader, builder, executor } = await createTestCompiler();
const { compiledData } = await compiler.compile(`
{% req userCount main %}
select count(*) as count from user where user.id = {{ params.userId }};
{% endreq %}
`);
builder.value.onFirstCall().resolves({
getColumns: () => [],
getData: () => arrayToStream([{ count: 1 }]),
});
// Action
loader.setSource('test', compiledData);
const result = await compiler.execute('test', {
params: { userId: 'user-id' },
});
const resultData = await streamToArray(result.getData());
// Assert
expect(executor.createBuilder.firstCall.args[0]).toBe(
`select count(*) as count from user where user.id = $1;`
);
expect(executor.createBuilder.firstCall.args[1].get('$1')).toBe(`user-id`);
expect(resultData).toEqual([{ count: 1 }]);
});
it('If argument is not a symbol, extension should throw', async () => {
// Arrange
const { compiler } = await createTestCompiler();
// Action, Assert
await expect(
compiler.compile(`
{% req "userCount" %}
select count(*) as count from user where user.id = '{{ params.userId }}';
{% endreq %}
`)
).rejects.toThrow(`Expected a symbol, but got string`);
});
it('If argument is missing, extension should throw', async () => {
// Arrange
const { compiler } = await createTestCompiler();
// Action, Assert
await expect(
compiler.compile(`
{% req %}
select count(*) as count from user where user.id = '{{ params.userId }}';
{% endreq %}
`)
).rejects.toThrow(`Expected a variable`);
});
it('If the main denotation is replaces other keywords than "main", extension should throw an error', async () => {
// Arrange
const { compiler } = await createTestCompiler();
// Action, Assert
await expect(
compiler.compile(`
{% req user super %}
some statement
{% endreq %}
`)
).rejects.toThrow(`Expected a symbol "main"`);
});
it('If argument have too many elements, extension should throw an error', async () => {
// Arrange
const { compiler } = await createTestCompiler();
// Action, Assert
await expect(
compiler.compile(`
{% req user main more %}
select count(*) as count from user where user.id = '{{ params.userId }}';
{% endreq %}
`)
).rejects.toThrow(`Expected a block end, but got symbol`);
});
it('The main denotation should be parsed into the second args node', async () => {
// Arrange
const { compiler } = await createTestCompiler();
// Action
const { ast: astWithMainBuilder } = compiler.generateAst(
`{% req user main %} some statement {% endreq %}`
);
// Assert
expect((astWithMainBuilder as any).children[0].args.children[1].value).toBe(
'true'
);
});
it('Extension should throw an error if there are tow main builders', async () => {
// Arrange
const { compiler } = await createTestCompiler();
// Act, Arrange
await expect(
compiler.compile(
`
{% req user main %} select * from users; {% endreq %}
{% req user2 main %} select * from users; {% endreq %}
`
)
).rejects.toThrowError(`Only one main builder is allowed.`);
});
it('Extension should throw an error if there are multiple builders using same name', async () => {
// Arrange
const { compiler } = await createTestCompiler();
// Act, Arrange
await expect(
compiler.compile(
`
{% req user %} select * from users; {% endreq %}
{% req user %} select * from users; {% endreq %}
`
)
).rejects.toThrowError(
`We can't declare multiple builder with same name. Duplicated name: user (declared at 1:7 and 2:7)`
);
});
it('Extension should reset after compiled each template', async () => {
// Arrange
const { compiler } = await createTestCompiler();
compiler.compile(
`
{% req user main %} select * from users; {% endreq %}
`
);
// Act, Arrange
await expect(
compiler.compile(
`
{% req user main %} select * from users; {% endreq %}
`
)
).resolves.not.toThrow();
});