-
Notifications
You must be signed in to change notification settings - Fork 2
/
Index.spec.ts
180 lines (145 loc) · 4.15 KB
/
Index.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import { Index, isIndex, INDEX_UNIQUE, processEntityIndexes } from './Index';
import { Entity } from '../entity/Entity';
import { DataTypeString } from '../datatype/dataTypes';
import { passOrThrow } from '../util';
describe('Index', () => {
it('should have a type', () => {
function fn() {
// eslint-disable-next-line no-new
new Index();
}
expect(fn).toThrowErrorMatchingSnapshot();
});
it('should have a valid type', () => {
function fn() {
// eslint-disable-next-line no-new
new Index({
type: 'something',
});
}
expect(fn).toThrowErrorMatchingSnapshot();
});
it('should have a list of attributes', () => {
function fn1() {
// eslint-disable-next-line no-new
new Index({
type: INDEX_UNIQUE,
});
}
expect(fn1).toThrowErrorMatchingSnapshot();
function fn2() {
// eslint-disable-next-line no-new
new Index({
type: INDEX_UNIQUE,
attributes: [],
});
}
expect(fn2).toThrowErrorMatchingSnapshot();
});
it('should accept attribute names only', () => {
function fn1() {
// eslint-disable-next-line no-new
new Index({
type: INDEX_UNIQUE,
attributes: [null],
});
}
expect(fn1).toThrowErrorMatchingSnapshot();
function fn2() {
// eslint-disable-next-line no-new
new Index({
type: INDEX_UNIQUE,
attributes: [((123 as unknown) as string)],
});
}
expect(fn2).toThrowErrorMatchingSnapshot();
});
it('should accept unique attribute names only', () => {
function fn() {
// eslint-disable-next-line no-new
new Index({
type: INDEX_UNIQUE,
attributes: ['a', 'b', 'a'],
});
}
expect(fn).toThrowErrorMatchingSnapshot();
});
it('should accept a correct definition', () => {
const index1 = new Index({
type: INDEX_UNIQUE,
attributes: ['a'],
});
const index2 = new Index({
type: INDEX_UNIQUE,
attributes: ['a', 'b', 'c'],
});
expect(index1.attributes).toEqual(['a']);
expect(index2.attributes).toEqual(['a', 'b', 'c']);
expect(String(index1)).toEqual('unique');
});
describe('isIndex', () => {
it('should recognize objects of type Index', () => {
const index = new Index({
type: INDEX_UNIQUE,
attributes: ['a'],
});
function fn() {
passOrThrow(isIndex(index), () => 'This error will never happen');
}
expect(fn).not.toThrow();
});
it('should recognize non-Index objects', () => {
function fn() {
passOrThrow(
isIndex({}) || isIndex(function test() {}) || isIndex(Error),
() => 'Not an Index object',
);
}
expect(fn).toThrowErrorMatchingSnapshot();
});
});
describe('processEntityIndexes', () => {
const entity = new Entity({
name: 'SomeEntityName',
description: 'Just some description',
attributes: {
someAttribute: {
type: DataTypeString,
description: 'Just some description',
required: true,
},
},
});
entity.getIndexes();
it('should throw if provided with an invalid list of indexes', () => {
const indexes = {
unique: [{}],
};
function fn() {
processEntityIndexes(entity, (indexes as unknown) as Index[]);
}
expect(fn).toThrowErrorMatchingSnapshot();
});
it('should throw if provided with an invalid index', () => {
const indexes = [{ foo: 'bar' }];
function fn() {
processEntityIndexes(entity, (indexes as unknown) as Index[]);
}
expect(fn).toThrowErrorMatchingSnapshot();
});
it('should throw if attribute used in index does not exist', () => {
const indexes = [
new Index({
type: INDEX_UNIQUE,
attributes: ['someAttribute', 'notHere'],
}),
];
function fn() {
processEntityIndexes(entity, indexes);
}
expect(fn).toThrowErrorMatchingSnapshot();
});
});
});