Skip to content

Commit

Permalink
Add tests for overwriting serializable_hash
Browse files Browse the repository at this point in the history
This technique is useful when a user wants to generate
super complicated JSON.
  • Loading branch information
okuramasafumi committed Aug 7, 2020
1 parent 02ba38e commit 5e3430e
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/usecases/user_defined_serializable_hash_test.rb
@@ -0,0 +1,32 @@
require_relative '../test_helper'

class UserDefinedSerializableHashTest < MiniTest::Test
class Foo
attr_accessor :id, :name

def initialize(id, name)
@id = id
@name = name
end
end

class FooResource
include Alba::Resource

def serializable_hash
grouped = @_object.group_by { |foo| foo.id.even? ? 'even' : 'odd' }
grouped.transform_values { |foos| foos.map { |foo| {name: foo.name} } }
end
end

def test_resource_class_with_overwriting_works
foo1 = Foo.new(1, 'name1')
foo2 = Foo.new(2, 'name2')
foo3 = Foo.new(3, 'name3')
foo4 = Foo.new(4, 'name4')
assert_equal(
'{"foos":{"odd":[{"name":"name1"},{"name":"name3"}],"even":[{"name":"name2"},{"name":"name4"}]}}',
FooResource.new([foo1, foo2, foo3, foo4]).serialize(with: proc { set key: :foos })
)
end
end

0 comments on commit 5e3430e

Please sign in to comment.