-
Notifications
You must be signed in to change notification settings - Fork 1
/
query-segment.test.js
79 lines (77 loc) · 3.52 KB
/
query-segment.test.js
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
import QuerySegment from './query-segment.js';
describe('#constructor', () => {
it('throws when a non-null, non-undefined, non-string segment is specified.', () => {
let badValues = [123, true, new Date()];
for (let v of badValues) {
expect(() => new QuerySegment(v)).toThrow(/segment.+string/i);
}
});
it('sets the initial segment value.', () => {
let qs = new QuerySegment('SELECT * FROM');
expect(qs.segment).toBe('SELECT * FROM');
expect(qs.params.size).toBe(0);
});
it('sets params map from another map.', () => {
let qs = new QuerySegment(null, new Map([['a', 1], ['b', 2], ['c', 3]]));
expect(qs.params.size).toBe(3);
expect(qs.params.get('a')).toBe(1);
expect(qs.params.get('b')).toBe(2);
expect(qs.params.get('c')).toBe(3);
});
it('sets params map from an array matrix.', () => {
let qs = new QuerySegment(null, [['a', 1], ['b', 2], ['c', 3]]);
expect(qs.params.size).toBe(3);
expect(qs.params.get('a')).toBe(1);
expect(qs.params.get('b')).toBe(2);
expect(qs.params.get('c')).toBe(3);
});
});
describe('#concat', () => {
it('throws when the parameter has already been added or already exists.', () => {
let qs = new QuerySegment('SELECT * FROM Contacts WHERE FirstName LIKE @w0');
qs.params.set('@w0', 'John');
expect(() => qs.concat(new QuerySegment(null, [['@w0', 'Bob']]))).toThrow(/collision/i);
expect(() => qs.concat(new QuerySegment(null, [['@w1', 'Yoda']]))).not.toThrow();
expect(() => qs.concat(new QuerySegment(null, [['@w1', 'Froddo']]))).toThrow(/collision/i);
});
it('combines the segments and params from other QuerySegments.', () => {
let qs = new QuerySegment();
qs.concat(new QuerySegment('SELECT'), new QuerySegment(' * FROM'));
qs.concat(new QuerySegment(' WHERE FirstName LIKE @w0 AND LastName = @w1 AND Abc = @w2', new Map([
['@w0', 1],
['@w1', 2],
['@w2', 3]
])));
expect(qs.segment).toBe('SELECT * FROM WHERE FirstName LIKE @w0 AND LastName = @w1 AND Abc = @w2');
expect(qs.params.size).toBe(3);
expect(qs.params.get('@w0')).toBe(1);
expect(qs.params.get('@w1')).toBe(2);
expect(qs.params.get('@w2')).toBe(3);
});
it('combines the segments and params from strings.', () => {
let qs = new QuerySegment();
qs.concat('SELECT * ', 'FROM Moose');
qs.concat(' WHERE FirstName LIKE \'Bob\'');
expect(qs.segment).toBe('SELECT * FROM Moose WHERE FirstName LIKE \'Bob\'');
expect(qs.params.size).toBe(0);
});
it('combines the segments and params from strings and segments, skipping nulls.', () => {
let qs = new QuerySegment();
qs.concat('SELECT * ', null, 'FROM Moose', new QuerySegment(' WHERE FirstName LIKE @w0 AND LastName = @w1 AND Abc = @w2', new Map([
['@w0', 1],
['@w1', 2],
['@w2', 3]
])), null);
expect(qs.segment).toBe('SELECT * FROM Moose WHERE FirstName LIKE @w0 AND LastName = @w1 AND Abc = @w2');
expect(qs.params.size).toBe(3);
expect(qs.params.get('@w0')).toBe(1);
expect(qs.params.get('@w1')).toBe(2);
expect(qs.params.get('@w2')).toBe(3);
});
it('reinitializes the params map if needed.', () => {
let qs = new QuerySegment();
qs.params = null;
qs.concat('SELECT * ', 'FROM Moose');
expect(qs.params).toBeInstanceOf(Map);
});
});