mirror of
https://github.com/paper-trail-gem/paper_trail.git
synced 2022-11-09 11:33:19 -05:00
Convert serializers/mixin_yaml_test to rspec
This commit is contained in:
parent
379d9187e9
commit
5cf844f288
2 changed files with 44 additions and 53 deletions
44
spec/paper_trail/serializers/custom_yaml_serializer_spec.rb
Normal file
44
spec/paper_trail/serializers/custom_yaml_serializer_spec.rb
Normal file
|
@ -0,0 +1,44 @@
|
|||
require "rails_helper"
|
||||
|
||||
module CustomYamlSerializer
|
||||
extend PaperTrail::Serializers::YAML
|
||||
|
||||
def self.load(string)
|
||||
parsed_value = super(string)
|
||||
if parsed_value.is_a?(Hash)
|
||||
parsed_value.reject { |k, v| (k.blank? || v.blank?) }
|
||||
else
|
||||
parsed_value
|
||||
end
|
||||
end
|
||||
|
||||
def self.dump(object)
|
||||
object.is_a?(Hash) ? super(object.reject { |_k, v| v.nil? }) : super
|
||||
end
|
||||
end
|
||||
|
||||
RSpec.describe CustomYamlSerializer do
|
||||
before do
|
||||
@hash = {}
|
||||
(1..4).each { |i| @hash["key#{i}"] = [FFaker::Lorem.word, nil].sample }
|
||||
@hash["tkey"] = nil
|
||||
@hash[""] = "foo"
|
||||
@hash_as_yaml = @hash.to_yaml
|
||||
end
|
||||
|
||||
context(".load") do
|
||||
it("deserializes YAML to Ruby, removing pairs with blank keys or values") do
|
||||
expect(CustomYamlSerializer.load(@hash_as_yaml)).to eq(
|
||||
@hash.reject { |k, v| (k.blank? || v.blank?) }
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context(".dump") do
|
||||
it("serializes Ruby to YAML, removing pairs with nil values") do
|
||||
expect(CustomYamlSerializer.dump(@hash)).to eq(
|
||||
@hash.reject { |_k, v| v.nil? }.to_yaml
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,53 +0,0 @@
|
|||
require "test_helper"
|
||||
|
||||
module CustomYamlSerializer
|
||||
extend PaperTrail::Serializers::YAML
|
||||
|
||||
def self.load(string)
|
||||
parsed_value = super(string)
|
||||
if parsed_value.is_a?(Hash)
|
||||
parsed_value.reject { |k, v| k.blank? || v.blank? }
|
||||
else
|
||||
parsed_value
|
||||
end
|
||||
end
|
||||
|
||||
def self.dump(object)
|
||||
object.is_a?(Hash) ? super(object.reject { |_k, v| v.nil? }) : super
|
||||
end
|
||||
end
|
||||
|
||||
class MixinYamlTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
# Setup a hash with random values, ensuring some values are nil
|
||||
@hash = {}
|
||||
(1..4).each do |i|
|
||||
@hash["key#{i}"] = [FFaker::Lorem.word, nil].sample
|
||||
end
|
||||
@hash["tkey"] = nil
|
||||
@hash[""] = "foo"
|
||||
@hash_as_yaml = @hash.to_yaml
|
||||
end
|
||||
|
||||
context "`load` class method" do
|
||||
should "exist" do
|
||||
assert CustomYamlSerializer.respond_to?(:load)
|
||||
end
|
||||
|
||||
should "`deserialize` YAML to Ruby, removing pairs with `blank` keys or values" do
|
||||
assert_equal @hash.reject { |k, v| k.blank? || v.blank? },
|
||||
CustomYamlSerializer.load(@hash_as_yaml)
|
||||
end
|
||||
end
|
||||
|
||||
context "`dump` class method" do
|
||||
should "exist" do
|
||||
assert CustomYamlSerializer.respond_to?(:dump)
|
||||
end
|
||||
|
||||
should "`serialize` Ruby to YAML, removing pairs with `nil` values" do
|
||||
assert_equal @hash.reject { |_k, v| v.nil? }.to_yaml,
|
||||
CustomYamlSerializer.dump(@hash)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue