Skip to content

Commit

Permalink
Refine snapshot tests
Browse files Browse the repository at this point in the history
Improve readability of test results
Reduce duplicated loop
  • Loading branch information
kachick committed Mar 20, 2024
1 parent 9fae027 commit d7fdafb
Showing 1 changed file with 13 additions and 40 deletions.
53 changes: 13 additions & 40 deletions test/many_data/test_snapshots.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

require('perfect_toml')

# @TODO: Rewrite with data driven test, as for each ULID "test_ulid_271DRTPTX9Y1SHE8V0WAQ6XSK3"

# https://github.com/kachick/ruby-ulid/issues/89
class TestSnapshots < Test::Unit::TestCase
toml = PerfectTOML.load_file("#{__dir__}/fixtures/snapshots_2024-01-10_07-59.toml", symbolize_names: true)
Expand All @@ -31,18 +29,20 @@ def assert_example(ulid, example)
end
end

def test_from_integer
EXAMPLES.each do |example|
ulid = ULID.from_integer(example.integer)
assert_example(ulid, example)
end
EXAMPLES.each do |example|
data(example.string, example)
end

def test_parse
EXAMPLES.each do |example|
ulid = ULID.parse(example.string)
assert_example(ulid, example)
end
def test_decoders(example)
ulid_parsed = ULID.parse(example.string)
ulid_from_integer = ULID.from_integer(example.integer)
assert_equal(ulid_parsed, ulid_from_integer)
assert_example(ulid_parsed, example)

# @TODO: Update snapshot formats with https://github.com/kachick/ruby-ulid/pull/341
# Handling UUID should consider the difference.
# See https://github.com/kachick/ruby-ulid/pull/341 for further detail
# ulid_from_uuidv4 = ULID.from_uuidish(ulid_parsed.to_uuidish)
# assert_equal(ulid_parsed, ulid_from_uuidv4)
end

def test_sortable
Expand All @@ -57,31 +57,4 @@ def test_sortable
assert_instance_of(ULID, ulid_objects.sample)
assert_equal(ulid_strings.shuffle.sort, ulid_objects.shuffle.sort.map(&:to_s))
end

# ref: https://github.com/kachick/ruby-ulid/pull/341
def test_from_uuidv4
irreversible_ulid_to_uuid = {}
EXAMPLES.each do |example|
ulid = ULID.from_uuidv4(example.uuidv4)
assert_equal(example.uuidv4, ulid.to_uuidv4, 'Loading results should be same')
unless example.string == ulid.to_s
irreversible_ulid_to_uuid[ulid.to_s] = [example.string, example.uuidv4]
next
end
assert_example(ulid, example)
end
irreversible_rate = Rational(irreversible_ulid_to_uuid.size, EXAMPLES.size)
assert do
((9/10r)...1).cover?(irreversible_rate)
end
irreversible_ulid_to_uuid.each_value do |original_encoded, _|
original_ulid = ULID.parse(original_encoded)

assert_raises(ULID::IrreversibleUUIDError) do
original_ulid.to_uuidv4
end

assert_equal(original_ulid, ULID.from_uuidish(original_ulid.to_uuidish))
end
end
end

0 comments on commit d7fdafb

Please sign in to comment.