-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path03_typeid.test.sql
More file actions
369 lines (330 loc) · 12.8 KB
/
03_typeid.test.sql
File metadata and controls
369 lines (330 loc) · 12.8 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
-- Start transaction and plan the tests.
BEGIN;
SELECT plan(44);
create table tests (
"tid" typeid
);
-- Run the 'valid' tests.
-- - name: nil
-- typeid: "00000000000000000000000000"
-- prefix: ""
-- uuid: "00000000-0000-0000-0000-000000000000"
SELECT is(
typeid_parse('00000000000000000000000000'),
('', '00000000-0000-0000-0000-000000000000')::typeid,
'Parse valid: nil'
);
SELECT is(
typeid_print(('', '00000000-0000-0000-0000-000000000000')),
'00000000000000000000000000',
'Print valid: nil'
);
-- - name: one
-- typeid: "00000000000000000000000001"
-- prefix: ""
-- uuid: "00000000-0000-0000-0000-000000000001"
SELECT is(
typeid_parse('00000000000000000000000001'),
('', '00000000-0000-0000-0000-000000000001')::typeid,
'Parse valid: one'
);
SELECT is(
typeid_print(('', '00000000-0000-0000-0000-000000000001')),
'00000000000000000000000001',
'Print valid: one'
);
-- - name: ten
-- typeid: "0000000000000000000000000a"
-- prefix: ""
-- uuid: "00000000-0000-0000-0000-00000000000a"
SELECT is(
typeid_parse('0000000000000000000000000a'),
('', '00000000-0000-0000-0000-00000000000a')::typeid,
'Parse valid: ten'
);
SELECT is(
typeid_print(('', '00000000-0000-0000-0000-00000000000a')),
'0000000000000000000000000a',
'Print valid: ten'
);
-- - name: sixteen
-- typeid: "0000000000000000000000000g"
-- prefix: ""
-- uuid: "00000000-0000-0000-0000-000000000010"
SELECT is(
typeid_parse('0000000000000000000000000g'),
('', '00000000-0000-0000-0000-000000000010')::typeid,
'Parse valid: sixteen'
);
SELECT is(
typeid_print(('', '00000000-0000-0000-0000-000000000010')),
'0000000000000000000000000g',
'Print valid: sixteen'
);
-- - name: thirty-two
-- typeid: "00000000000000000000000010"
-- prefix: ""
-- uuid: "00000000-0000-0000-0000-000000000020"
SELECT is(
typeid_parse('00000000000000000000000010'),
('', '00000000-0000-0000-0000-000000000020')::typeid,
'Parse valid: thirty-two'
);
SELECT is(
typeid_print(('', '00000000-0000-0000-0000-000000000020')),
'00000000000000000000000010',
'Print valid: thirty-two'
);
-- - name: max-valid
-- typeid: "7zzzzzzzzzzzzzzzzzzzzzzzzz"
-- prefix: ""
-- uuid: "ffffffff-ffff-ffff-ffff-ffffffffffff"
SELECT is(
typeid_parse('7zzzzzzzzzzzzzzzzzzzzzzzzz'),
('', 'ffffffff-ffff-ffff-ffff-ffffffffffff')::typeid,
'Parse valid: max-valid'
);
SELECT is(
typeid_print(('', 'ffffffff-ffff-ffff-ffff-ffffffffffff')),
'7zzzzzzzzzzzzzzzzzzzzzzzzz',
'Print valid: max-valid'
);
-- - name: valid-alphabet
-- typeid: "prefix_0123456789abcdefghjkmnpqrs"
-- prefix: "prefix"
-- uuid: "0110c853-1d09-52d8-d73e-1194e95b5f19"
SELECT is(
typeid_parse('prefix_0123456789abcdefghjkmnpqrs'),
('prefix', '0110c853-1d09-52d8-d73e-1194e95b5f19')::typeid,
'Parse valid: valid-alphabet'
);
SELECT is(
typeid_print(('prefix', '0110c853-1d09-52d8-d73e-1194e95b5f19')),
'prefix_0123456789abcdefghjkmnpqrs',
'Print valid: valid-alphabet'
);
-- - name: valid-prefix-underscores
-- typeid: "pre_fix_0123456789abcdefghjkmnpqrs"
-- prefix: "pre_fix"
-- uuid: "0110c853-1d09-52d8-d73e-1194e95b5f19"
SELECT is(
typeid_parse('pre_fix_0123456789abcdefghjkmnpqrs'),
('pre_fix', '0110c853-1d09-52d8-d73e-1194e95b5f19')::typeid,
'Parse valid: valid-prefix-underscores'
);
SELECT is(
typeid_print(('pre_fix', '0110c853-1d09-52d8-d73e-1194e95b5f19')),
'pre_fix_0123456789abcdefghjkmnpqrs',
'Print valid: valid-alphabet-underscores'
);
SELECT is(
typeid_print(('pre_____fix', '0110c853-1d09-52d8-d73e-1194e95b5f19')),
'pre_____fix_0123456789abcdefghjkmnpqrs',
'Print valid: valid-alphabet-underscores'
);
-- - name: valid-uuidv7
-- typeid: "prefix_01h455vb4pex5vsknk084sn02q"
-- prefix: "prefix"
-- uuid: "01890a5d-ac96-774b-bcce-b302099a8057"
SELECT is(
typeid_parse('prefix_01h455vb4pex5vsknk084sn02q'),
('prefix', '01890a5d-ac96-774b-bcce-b302099a8057')::typeid,
'Parse valid: valid-uuidv7'
);
SELECT is(
typeid_print(('prefix', '01890a5d-ac96-774b-bcce-b302099a8057')),
'prefix_01h455vb4pex5vsknk084sn02q',
'Print valid: valid-uuidv7'
);
-- Run the 'invalid' tests.
-- - name: prefix-uppercase
-- typeid: "PREFIX_00000000000000000000000000"
-- description: "The prefix should be lowercase with no uppercase letters"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('PREFIX_00000000000000000000000000')); $$,
'typeid prefix must match the regular expression ^([a-z]([a-z_]{0,61}[a-z])?)?$',
'Parse invalid: prefix-uppercase'
);
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_generate('PREFIX')); $$,
'typeid prefix must match the regular expression ^([a-z]([a-z_]{0,61}[a-z])?)?$',
'Parse invalid: prefix-uppercase'
);
-- - name: prefix-numeric
-- typeid: "12345_00000000000000000000000000"
-- description: "The prefix can't have numbers, it needs to be alphabetic"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('12345_00000000000000000000000000')); $$,
'typeid prefix must match the regular expression ^([a-z]([a-z_]{0,61}[a-z])?)?$',
'Parse invalid: prefix-numeric'
);
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_generate('12345')); $$,
'typeid prefix must match the regular expression ^([a-z]([a-z_]{0,61}[a-z])?)?$',
'Parse invalid: prefix-numeric'
);
-- - name: prefix-period
-- typeid: "pre.fix_00000000000000000000000000"
-- description: "The prefix can't have symbols, it needs to be alphabetic"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('pre.fix_00000000000000000000000000')); $$,
'typeid prefix must match the regular expression ^([a-z]([a-z_]{0,61}[a-z])?)?$',
'Parse invalid: prefix-period'
);
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_generate('pre.fix')); $$,
'typeid prefix must match the regular expression ^([a-z]([a-z_]{0,61}[a-z])?)?$',
'Parse invalid: prefix-period'
);
-- - name: prefix-underscore
-- typeid: "pre_fix_00000000000000000000000000"
-- description: "The prefix can't have leading or trailing underscores"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_generate('_prefix')); $$,
'typeid prefix must match the regular expression ^([a-z]([a-z_]{0,61}[a-z])?)?$',
'Parse invalid: prefix-underscore'
);
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_generate('prefix_')); $$,
'typeid prefix must match the regular expression ^([a-z]([a-z_]{0,61}[a-z])?)?$',
'Parse invalid: prefix-underscore'
);
-- - name: prefix-non-ascii
-- typeid: "préfix_00000000000000000000000000"
-- description: "The prefix can only have ascii letters"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('préfix_00000000000000000000000000')); $$,
'typeid prefix must match the regular expression ^([a-z]([a-z_]{0,61}[a-z])?)?$',
'Parse invalid: prefix-non-ascii'
);
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_generate('préfix')); $$,
'typeid prefix must match the regular expression ^([a-z]([a-z_]{0,61}[a-z])?)?$',
'Parse invalid: prefix-non-ascii'
);
-- - name: prefix-spaces
-- typeid: " prefix_00000000000000000000000000"
-- description: "The prefix can't have any spaces"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse(' prefix_00000000000000000000000000')); $$,
'typeid prefix must match the regular expression ^([a-z]([a-z_]{0,61}[a-z])?)?$',
'Parse invalid: prefix-spaces'
);
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_generate(' prefix')); $$,
'typeid prefix must match the regular expression ^([a-z]([a-z_]{0,61}[a-z])?)?$',
'Parse invalid: prefix-spaces'
);
-- - name: prefix-64-chars
-- # 123456789 123456789 123456789 123456789 123456789 123456789 1234
-- typeid: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl_00000000000000000000000000"
-- description: "The prefix can't be 64 characters, it needs to be 63 characters or less"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl_00000000000000000000000000')); $$,
'typeid prefix must match the regular expression ^([a-z]([a-z_]{0,61}[a-z])?)?$',
'Parse invalid: prefix-64-chars'
);
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_generate('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl')); $$,
'typeid prefix must match the regular expression ^([a-z]([a-z_]{0,61}[a-z])?)?$',
'Parse invalid: prefix-64-chars'
);
-- - name: separator-empty-prefix
-- typeid: "_00000000000000000000000000"
-- description: "If the prefix is empty, the separator should not be there"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('_00000000000000000000000000')); $$,
'typeid prefix cannot be empty with a delimiter',
'Parse invalid: separator-empty-prefix'
);
-- - name: separator-empty
-- typeid: "_"
-- description: "A separator by itself should not be treated as the empty string"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('_')); $$,
'typeid prefix cannot be empty with a delimiter',
'Parse invalid: separator-empty'
);
-- - name: suffix-short
-- typeid: "prefix_1234567890123456789012345"
-- description: "The suffix can't be 25 characters, it needs to be exactly 26 characters"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('prefix_1234567890123456789012345')); $$,
'typeid suffix must be 26 characters',
'Parse invalid: suffix-short'
);
-- - name: suffix-long
-- typeid: "prefix_123456789012345678901234567"
-- description: "The suffix can't be 27 characters, it needs to be exactly 26 characters"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('prefix_123456789012345678901234567')); $$,
'typeid suffix must be 26 characters',
'Parse invalid: suffix-long'
);
-- - name: suffix-spaces
-- # This example has the right length, so that the failure is caused by the space
-- # and not the suffix length
-- typeid: "prefix_1234567890123456789012345 "
-- description: "The suffix can't have any spaces"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('prefix_1234567890123456789012345 ')); $$,
'typeid suffix must only use characters from the base32 alphabet',
'Parse invalid: suffix-spaces'
);
-- - name: suffix-uppercase
-- # This example is picked because it would be valid in lowercase
-- typeid: "prefix_0123456789ABCDEFGHJKMNPQRS"
-- description: "The suffix should be lowercase with no uppercase letters"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('prefix_0123456789ABCDEFGHJKMNPQRS')); $$,
'typeid suffix must only use characters from the base32 alphabet',
'Parse invalid: suffix-uppercase'
);
-- - name: suffix-hyphens
-- # This example has the right length, so that the failure is caused by the hyphens
-- # and not the suffix length
-- typeid: "prefix_123456789-123456789-123456"
-- description: "The suffix should be lowercase with no uppercase letters"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('prefix_123456789-123456789-123456')); $$,
'typeid suffix must only use characters from the base32 alphabet',
'Parse invalid: suffix-hyphens'
);
-- - name: suffix-wrong-alphabet
-- typeid: "prefix_ooooooiiiiiiuuuuuuulllllll"
-- description: "The suffix should only have letters from the spec's alphabet"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('prefix_ooooooiiiiiiuuuuuuulllllll')); $$,
'typeid suffix must only use characters from the base32 alphabet',
'Parse invalid: suffix-wrong-alphabet'
);
-- - name: suffix-ambiguous-crockford
-- # This example would be valid if we were using the crockford disambiguation rules
-- typeid: "prefix_i23456789ol23456789oi23456"
-- description: "The suffix should not have any ambiguous characters from the crockford encoding"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('prefix_i23456789ol23456789oi23456')); $$,
'typeid suffix must only use characters from the base32 alphabet',
'Parse invalid: suffix-ambiguous-crockford'
);
-- - name: suffix-hyphens-crockford
-- # This example would be valid if we were using the crockford hyphenation rules
-- typeid: "prefix_123456789-0123456789-0123456"
-- description: "The suffix can't ignore hyphens as in the crockford encoding"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('prefix_123456789-0123456789-0123456')); $$,
'typeid suffix must be 26 characters',
'Parse invalid: suffix-hyphens-crockford'
);
-- - name: suffix-overflow
-- # This is the first suffix that overflows into 129 bits
-- typeid: "prefix_8zzzzzzzzzzzzzzzzzzzzzzzzz"
-- description: "The suffix should encode at most 128-bits"
SELECT throws_ok(
$$ INSERT into tests (tid) VALUES (typeid_parse('prefix_8zzzzzzzzzzzzzzzzzzzzzzzzz')); $$,
'typeid suffix must start with 0-7',
'Parse invalid: suffix-overflow'
);
-- Finish the tests and clean up.
SELECT * FROM finish();
ROLLBACK;