-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from k2nr/bquorning/improve-ulid-validator
Test and improve #valid_ulid? method
- Loading branch information
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require "test_helper" | ||
|
||
class ULID::Rails::Type::DataTest < Minitest::Test | ||
def test_has_26_valid_characters | ||
id = "01BX5ZZKBKACTAV9WEVGEMMVRZ" | ||
|
||
assert ULID::Rails::Type::Data.valid_ulid?(id) | ||
end | ||
|
||
def test_length_less_than_26 | ||
id = "A" * 25 | ||
|
||
refute ULID::Rails::Type::Data.valid_ulid?(id) | ||
end | ||
|
||
def test_length_greater_than_26 | ||
id = "A" * 27 | ||
|
||
refute ULID::Rails::Type::Data.valid_ulid?(id) | ||
end | ||
|
||
# Crockford's Base32 is used as shown. This alphabet excludes | ||
# the letters I, L, O, and U to avoid confusion and abuse. | ||
# https://github.com/ulid/spec#encoding | ||
def test_has_invalid_characters | ||
invalid_characters = %w[I L O U] | ||
|
||
invalid_characters.each do |char| | ||
id = char * 26 | ||
|
||
refute ULID::Rails::Type::Data.valid_ulid?(id) | ||
end | ||
end | ||
|
||
# The first character in a ULID must be 0, 7, or in between. | ||
# Anything larger than 7, and the id would not fit into 16 bytes. | ||
def test_does_not_exceed_16_bytes | ||
valid_first_characters = ("0".."7").to_a | ||
|
||
valid_first_characters.each do |char| | ||
id = "#{char}#{"A" * 25}" | ||
|
||
assert ULID::Rails::Type::Data.valid_ulid?(id) | ||
end | ||
|
||
invalid_first_characters = ("8".."9").to_a + ("A".."Z").to_a | ||
|
||
invalid_first_characters.each do |char| | ||
id = "#{char}#{"A" * 25}" | ||
|
||
refute ULID::Rails::Type::Data.valid_ulid?(id) | ||
end | ||
end | ||
end |