test/nested_resource.rb in arrest-0.0.6 vs test/nested_resource.rb in arrest-0.0.7

- old
+ new

@@ -24,11 +24,10 @@ } actual = WithNested.new(input) assert_equal 'parent', actual.parent_name assert_equal false, actual.bool - puts actual.inspect assert actual.respond_to? :nested_object, "The parent object should have an accessor for the nested object" assert_equal 'iamnested', actual.nested_object.name assert_equal true, actual.nested_object.bool end @@ -53,9 +52,100 @@ 'bool' => true } } assert_equal_hashes expected, actual.to_hash + + end + + def test_many_to_hash + input = { + :parent_name => 'parent', + :bool => false, + :nested_objects => [ + { + :name => 'iamnested_one', + :bool => true + },{ + :name => 'iamnested_two', + :bool => false + } + ] + } + + actual = WithManyNested.new(input) + + # we expect camel cased keys + expected = { + 'parentName' => 'parent', + 'bool' => false, + 'nestedObjects' => [ + { + 'name' => 'iamnested_one', + 'bool' => true + },{ + 'name' => 'iamnested_two', + 'bool' => false + } + ] + } + + assert_equal_hashes expected, actual.to_hash + + end + + def test_belongd_to_to_hash + new_zoo = Zoo.new({:name => "Foo"}) + new_zoo.save + + input = { + :parent_name => 'parent', + :bool => false, + :nested_object => { + :name => 'iamnested', + :bool => true, + :zoo_id => new_zoo.id + } + } + + actual = WithNestedBelongingTo.new(input) + + # we expect camel cased keys + expected = { + 'parentName' => 'parent', + 'bool' => false, + 'nestedObject' => { + 'name' => 'iamnested', + 'bool' => true, + 'zooId' => new_zoo.id + + } + } + + assert_equal_hashes expected, actual.to_hash + + zoo = actual.nested_object.zoo + assert_equal "Foo", zoo.name + + end + + def test_custom_belongs_to + + new_zoo = Zoo.new({:name => "Foo"}) + new_zoo.save + + c = CustomNamedBelongsTo.new({:name => 'Bar', :schinken => new_zoo.id, :batzen => new_zoo.id}) + + c.save + assert_not_nil c.id, "Persisted object should have id" + assert_equal "Foo", c.zoo_thing.name + assert_equal "Foo", c.zoo.name + + + assert_not_nil c.id, "Persisted zoo should have id" + c_reloaded = CustomNamedBelongsTo.all.first + assert_equal "Foo", c_reloaded.zoo_thing.name + assert_equal "Foo", c_reloaded.zoo.name end def assert_equal_hashes expected, actual assert_equal_hashes_ expected, actual, ''