Sha256: d2f8df0b8d2c637d20a0a4faf13705018ba4a415004ee7f37eec6c9530844c98
Contents?: true
Size: 1.27 KB
Versions: 1
Compression:
Stored size: 1.27 KB
Contents
require 'test_helper' module CustomJsonSerializer extend PaperTrail::Serializers::Json def self.load(string) parsed_value = super(string) parsed_value.is_a?(Hash) ? parsed_value.reject { |k,v| k.blank? || v.blank? } : parsed_value end def self.dump(object) object.is_a?(Hash) ? super(object.reject { |k,v| v.nil? }) : super end end class MixinJsonTest < ActiveSupport::TestCase setup do # Setup a hash with random values, ensuring some values are nil @hash = {} (1..4).each do |i| @hash["key#{i}"] = [Faker::Lorem.word, nil].sample end @hash['tkey'] = nil @hash[''] = 'foo' @hash_as_json = @hash.to_json end context '`load` class method' do should 'exist' do assert CustomJsonSerializer.respond_to?(:load) end should '`deserialize` JSON to Ruby, removing pairs with `blank` keys or values' do assert_equal @hash.reject { |k,v| k.blank? || v.blank? }, CustomJsonSerializer.load(@hash_as_json) end end context '`dump` class method' do should 'exist' do assert CustomJsonSerializer.respond_to?(:dump) end should '`serialize` Ruby to JSON, removing pairs with `nil` values' do assert_equal @hash.reject { |k,v| v.nil? }.to_json, CustomJsonSerializer.dump(@hash) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
paper_trail-2.7.1 | test/unit/serializers/mixin_json_test.rb |