1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
require "test_helper"
class IncludeExcludeTest < Minitest::Spec
Song = Struct.new(:title, :artist, :id)
Artist = Struct.new(:name, :id, :songs)
representer!(decorator: true) do
property :title
property :artist, class: Artist do
property :name
property :id
collection :songs, class: Song do
property :title
property :id
end
end
end
let(:song) { Song.new("Listless", Artist.new("7yearsbadluck", 1 )) }
let(:decorator) { representer.new(song) }
describe "#from_hash" do
it "accepts :exclude option" do
decorator.from_hash({"title"=>"Don't Smile In Trouble", "artist"=>{"id"=>2}}, exclude: [:title])
song.title.must_equal "Listless"
song.artist.must_equal Artist.new(nil, 2)
end
it "accepts :include option" do
decorator.from_hash({"title"=>"Don't Smile In Trouble", "artist"=>{"id"=>2}}, include: [:title])
song.title.must_equal "Don't Smile In Trouble"
song.artist.must_equal Artist.new("7yearsbadluck", 1)
end
it "accepts nested :exclude/:include option" do
decorator.from_hash({"title"=>"Don't Smile In Trouble", "artist"=>{"name"=>"Foo", "id"=>2, "songs"=>[{"id"=>1, "title"=>"Listless"}]}},
exclude: [:title],
artist: {
exclude: [:id],
songs: { include: [:title] }
}
)
song.title.must_equal "Listless"
song.artist.must_equal Artist.new("Foo", nil, [Song.new("Listless", nil, nil)])
end
end
describe "#to_hash" do
it "accepts :exclude option" do
decorator.to_hash(exclude: [:title]).must_equal({"artist"=>{"name"=>"7yearsbadluck", "id"=>1}})
end
it "accepts :include option" do
decorator.to_hash(include: [:title]).must_equal({"title"=>"Listless"})
end
it "accepts nested :exclude/:include option" do
decorator = representer.new(Song.new("Listless", Artist.new("7yearsbadluck", 1, [Song.new("C.O.A.B.I.E.T.L.")])))
decorator.to_hash(
exclude: [:title],
artist: {
exclude: [:id],
songs: { include: [:title] }
}
).must_equal({"artist"=>{"name"=>"7yearsbadluck", "songs"=>[{"title"=>"C.O.A.B.I.E.T.L."}]}})
end
end
it "xdoes not propagate private options to nested objects" do
Cover = Struct.new(:title, :original)
cover_rpr = Module.new do
include Representable::Hash
property :title
property :original, extend: self
end
# FIXME: we should test all representable-options (:include, :exclude, ?)
Cover.new("Roxanne", Cover.new("Roxanne (Don't Put On The Red Light)")).extend(cover_rpr).
to_hash(:include => [:original]).must_equal({"original"=>{"title"=>"Roxanne (Don't Put On The Red Light)"}})
end
end
|