Skip to content

Commit

Permalink
Merge pull request #513 from kachick/update-snap-shots
Browse files Browse the repository at this point in the history
Refine snapshot tests
  • Loading branch information
kachick authored Mar 20, 2024
2 parents d7fdafb + ad5ceaf commit 5d9a81c
Show file tree
Hide file tree
Showing 6 changed files with 3,065 additions and 47 deletions.
3 changes: 1 addition & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -891,8 +891,7 @@ Lint/RedundantStringCoercion:
Enabled: false

Lint/StructNewOverride:
Exclude:
- 'test/many_data/fixtures/example.rb'
Enabled: true

Lint/BinaryOperatorWithIdenticalOperands:
Exclude:
Expand Down
20 changes: 20 additions & 0 deletions scripts/add_uuidish_in_snapshots.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# coding: us-ascii
# frozen_string_literal: true

require('bundler/setup')
require_relative('../lib/ulid')
require('perfect_toml')

# Needless to rollback. This is rough script.
ENV['TZ'] = 'UTC'

path = ARGV.first
parsed_hash = PerfectTOML.load_file(path, symbolize_names: true)
updated_hash = parsed_hash.transform_values { |table|
ulid = ULID.from_integer(table.fetch(:integer))
# Specifying some redundant attributes to adjust the key order
table.delete(:uuidv4)
table.delete(:octets)
table.merge(uuidish: ulid.to_uuidish, uuidv4: ulid.to_uuidv4(force: true), octets: ulid.octets)
}
PerfectTOML.save_file(path, updated_hash)
17 changes: 7 additions & 10 deletions scripts/generate_snapshots.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

require('bundler/setup')
require_relative('../lib/ulid')
require_relative('../test/many_data/fixtures/example')

require('time') # To use `Time.parse` for readability, Do not depend on tests

Expand All @@ -29,28 +28,27 @@
# limit_of_the_ulid = (ULID.max.to_time - 1000000)..ULID.max.to_time
limit_of_toml = Time.parse('9999/1/1')..Time.parse('9999/12/31')

examples = [ancient, recently, distant_future, limit_of_toml].flat_map do |period|
examples = [ancient, recently, distant_future, limit_of_toml].each_with_object({}) do |period, acc|
ulids = ULID.sample(1000, period:)
if ulids.uniq!
raise('Very rare case happened or bug exists')
end

ulids.map do |ulid|
ulids.sort.each do |ulid|
unless period.cover?(ulid.to_time)
raise(ULID::Error, 'Crucial bug exists!')
end

Example.new(
period:,
acc[ulid.to_s] = {
to_time: ulid.to_time,
string: ulid.to_s,
integer: ulid.to_i,
timestamp: ulid.timestamp,
randomness: ulid.randomness,
octets: ulid.octets,
inspect: ulid.inspect,
uuidish: ulid.to_uuidish,
uuidv4: ulid.to_uuidv4(force: true)
)
}
end
end

Expand All @@ -59,13 +57,12 @@
end

puts('The generated samples are below')
p(examples.sample(20))
p(examples.values.sample(20))

filename = "snapshots_#{Time.now.strftime('%Y-%m-%d_%H-%M')}.toml"
output_path = "#{File.expand_path('.')}/test/many_data/fixtures/#{filename}"

toml_prepared = examples.sort_by(&:integer).to_h { |example| [example.string, example.to_h.except(:string, :period)] }
PerfectTOML.save_file(output_path, toml_prepared)
PerfectTOML.save_file(output_path, examples)

puts('-' * 72)

Expand Down
6 changes: 0 additions & 6 deletions test/many_data/fixtures/example.rb

This file was deleted.

Loading

0 comments on commit 5d9a81c

Please sign in to comment.