Annotate

spec/util_uuid_spec.lua @ 13317:e6a5f196fc1f

util.uuid: Add UUIDv7 Allows sorting by id as a substitute for sorting by timestamp since it has the timestamp in the encoded in the first part, and only things that happen extremely close together may get out of order by such a sort, which might not matter. From draft-ietf-uuidrev-rfc4122bis formerly draft-peabody-dispatch-new-uuid-format
author Kim Alvefur <zash@zash.se>
date Sun, 15 Aug 2021 14:44:21 +0200
parent 12604:bd9e006a7a74
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1 -- This tests the format, not the randomness
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 local uuid = require "util.uuid";
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 describe("util.uuid", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 describe("#generate()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 it("should work follow the UUID pattern", function()
12604
bd9e006a7a74 various: Update IETF RFC URLs for tools.ietf.org transition
Kim Alvefur <zash@zash.se>
parents: 8241
diff changeset
8 -- https://www.rfc-editor.org/rfc/rfc4122.html#section-4.4
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local pattern = "^" .. table.concat({
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 string.rep("%x", 8),
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 string.rep("%x", 4),
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 "4" .. -- version
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 string.rep("%x", 3),
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 "[89ab]" .. -- reserved bits of 1 and 0
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 string.rep("%x", 3),
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 string.rep("%x", 12),
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 }, "%-") .. "$";
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 for _ = 1, 100 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 assert.is_string(uuid.generate():match(pattern));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 end
13317
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
23
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
24 assert.truthy(uuid.generate() ~= uuid.generate(), "does not generate the same UUIDv4 twice")
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
25 end);
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
26 end);
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
27 describe("#v7", function()
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
28 it("should also follow the UUID pattern", function()
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
29 local pattern = "^" .. table.concat({
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
30 string.rep("%x", 8),
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
31 string.rep("%x", 4),
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
32 "7" .. -- version
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
33 string.rep("%x", 3),
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
34 "[89ab]" .. -- reserved bits of 1 and 0
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
35 string.rep("%x", 3),
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
36 string.rep("%x", 12),
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
37 }, "%-") .. "$";
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
38
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
39 local one = uuid.v7(); -- one before the loop to ensure some time passes
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
40 for _ = 1, 100 do
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
41 assert.is_string(uuid.v7():match(pattern));
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
42 end
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
43 -- one after the loop when some time should have passed
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12604
diff changeset
44 assert.truthy(one < uuid.v7(), "should be ordererd")
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 end);