test/twin/sync_test.rb in disposable-0.3.2 vs test/twin/sync_test.rb in disposable-0.4.0

- old
+ new

@@ -92,30 +92,58 @@ album.songs[0].title.must_equal "Southbound" album.songs[1].title.must_equal "The Boys Are Back In Town" album.songs[1].composer.name.must_equal "Lynott" end - # save with block creates nested_hash and doesn't sync. - it do - twin = Twin::Album.new(album) + # save with block. + describe "#to_nested_hash" do + let (:twin) { Twin::Album.new(album) } - # this usually happens in Contract::Validate or in from_* in a representer - fill_out!(twin) + it "creates nested_hash and doesn't sync" do + # this usually happens in Contract::Validate or in from_* in a representer + fill_out!(twin) - nested_hash = nil - twin.sync do |hash| - nested_hash = hash + nested_hash = nil + twin.sync do |hash| + nested_hash = hash + end + + nested_hash.must_equal({"name"=>"Live And Dangerous", "songs"=>[{"title"=>"Southbound", "composer"=>nil}, {"title"=>"The Boys Are Back In Town", "composer"=>{"name"=>"Lynott"}}], "artist"=>{"name"=>"Thin Lizzy"}}) + + # nothing written to model. + album.name.must_equal nil + album.songs[0].title.must_equal nil + album.songs[1].title.must_equal nil + album.songs[1].composer.name.must_equal nil + album.artist.name.must_equal nil end - nested_hash.must_equal({"name"=>"Live And Dangerous", "songs"=>[{"title"=>"Southbound"}, {"title"=>"The Boys Are Back In Town", "composer"=>{"name"=>"Lynott"}}], "artist"=>{"name"=>"Thin Lizzy"}}) + describe "nil values" do + it "includes nil values, including nil collections" do + twin = Twin::Album.new(Model::Album.new(nil, + nil, # uninitialized nil collection. + nil) + ) - # nothing written to model. - album.name.must_equal nil - album.songs[0].title.must_equal nil - album.songs[1].title.must_equal nil - album.songs[1].composer.name.must_equal nil - album.artist.name.must_equal nil + nested_hash = nil + twin.sync { |hash| nested_hash = hash } + + nested_hash.must_equal({"name"=>nil, "artist"=>nil}) + end + + it "includes empty collections" do + twin = Twin::Album.new(Model::Album.new(nil, + [], # empty collection. + nil) + ) + + nested_hash = nil + twin.sync { |hash| nested_hash = hash } + + nested_hash.must_equal({"name"=>nil, "songs"=>[], "artist"=>nil}) + end + end end def fill_out!(twin) twin.name = "Live And Dangerous" @@ -123,6 +151,6 @@ twin.songs[1].title = "The Boys Are Back In Town" twin.songs[1].composer.name = "Lynott" twin.artist.name = "Thin Lizzy" end end -end \ No newline at end of file +end