-
-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathuuid_v7_test.exs
68 lines (52 loc) · 1.82 KB
/
uuid_v7_test.exs
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
defmodule Ash.Test.UUIDv7Test do
@moduledoc false
use ExUnit.Case, async: true
test "generate/1 is working" do
uuid = Ash.UUIDv7.generate()
assert <<_::64, ?-, _::32, ?-, "7", _::24, ?-, _::32, ?-, _::96>> = uuid
end
test "generate/1 is ordered" do
uuids =
for _ <- 1..100 do
uuid = Ash.UUIDv7.generate()
# only guaranteed sorted if >= 1 nanosecond apart
# can't sleep for one nanoseond AFAIK, so sleep for 1 ms
:timer.sleep(3)
uuid
end
assert uuids == Enum.sort(uuids)
end
test "bingenerate/1 is ordered" do
uuids =
for _ <- 1..100 do
uuid = Ash.UUIDv7.bingenerate()
# only guaranteed sorted if >= 1 nanosecond apart
# can't sleep for one nanoseond AFAIK, so sleep for 1 ms
:timer.sleep(3)
uuid
end
assert uuids == Enum.sort(uuids)
end
test "encode/1 is working" do
hex_uuid = Ash.UUIDv7.generate()
raw_uuid = Ash.UUIDv7.bingenerate()
encoded_hex_uuid = Ash.UUIDv7.encode(hex_uuid)
encoded_raw_uuid = Ash.UUIDv7.encode(raw_uuid)
assert is_binary(encoded_hex_uuid)
assert Ash.UUIDv7.decode(hex_uuid) == Ash.UUIDv7.decode(encoded_hex_uuid)
assert is_binary(encoded_raw_uuid)
assert raw_uuid == Ash.UUIDv7.decode(encoded_raw_uuid)
assert :error = Ash.UUIDv7.encode("error")
end
test "decode/1 is working" do
hex_uuid = Ash.UUIDv7.generate()
raw_uuid = Ash.UUIDv7.bingenerate()
decoded_hex_uuid = Ash.UUIDv7.decode(hex_uuid)
decoded_raw_uuid = Ash.UUIDv7.decode(raw_uuid)
assert is_binary(decoded_hex_uuid)
assert hex_uuid == Ash.UUIDv7.encode(decoded_hex_uuid)
assert is_binary(decoded_raw_uuid)
assert Ash.UUIDv7.encode(raw_uuid) == Ash.UUIDv7.encode(decoded_raw_uuid)
assert :error == Ash.UUIDv7.decode("error")
end
end