spec/integration/formats/json_api_spec.rb in ivy-serializers-0.1.0 vs spec/integration/formats/json_api_spec.rb in ivy-serializers-0.2.0

- old
+ new

@@ -3,25 +3,41 @@ RSpec.describe Ivy::Serializers::Formats::JSONAPI do let(:format) { described_class.new(document) } describe '#as_json' do let(:registry) { Ivy::Serializers::Registry.new } - let(:document) { Ivy::Serializers::Documents.create(registry, :post, post) } + let(:document) { Ivy::Serializers::Documents.create(registry, :posts, resource) } subject { format.as_json } context 'with default mapping' do let(:post_class) { double('Post', :name => 'Post') } let(:post) { double('post', :class => post_class, :id => 1) } - it { should eq({ - :data => { - :type => 'post', - :id => '1', - :links => {} - } - }) } + context 'for an individual resource' do + let(:resource) { post } + + it { should eq({ + :data => { + :type => 'post', + :id => '1', + :links => {} + } + }) } + end + + context 'for a resource collection' do + let(:resource) { [post] } + + it { should eq({ + :data => [{ + :type => 'post', + :id => '1', + :links => {} + }] + }) } + end end context 'with an attribute' do let(:post_class) { double('Post', :name => 'Post') } let(:post) { double('post', :class => post_class, :id => 1, :title => 'title') } @@ -31,35 +47,69 @@ registry.map post_class do attribute :title end end - it { should eq({ - :data => { - :type => 'post', - :id => '1', - :title => 'title', - :links => {} - } - }) } + context 'for an individual resource' do + let(:resource) { post } + + it { should eq({ + :data => { + :type => 'post', + :id => '1', + :title => 'title', + :links => {} + } + }) } + end + + context 'for a resource collection' do + let(:resource) { [post] } + + it { should eq({ + :data => [{ + :type => 'post', + :id => '1', + :title => 'title', + :links => {} + }] + }) } + end end context 'with a block provided' do before do registry.map post_class do attribute(:headline) { |post| post.title } end end - it { should eq({ - :data => { - :type => 'post', - :id => '1', - :headline => 'title', - :links => {} - } - }) } + context 'for an individual resource' do + let(:resource) { post } + + it { should eq({ + :data => { + :type => 'post', + :id => '1', + :headline => 'title', + :links => {} + } + }) } + end + + context 'for a resource collection' do + let(:resource) { [post] } + + it { should eq({ + :data => [{ + :type => 'post', + :id => '1', + :headline => 'title', + :links => {} + }] + }) } + end end end context 'with a belongs_to relationship' do let(:author_class) { double('Author', :name => 'Author') } @@ -72,89 +122,177 @@ registry.map post_class do belongs_to :author end end - it { should eq({ - :data => { - :type => 'post', - :id => '1', - :links => { - :author => { - :linkage => {:id => '1', :type => 'author'} + context 'for an individual resource' do + let(:resource) { post } + + it { should eq({ + :data => { + :type => 'post', + :id => '1', + :links => { + :author => { + :linkage => {:id => '1', :type => 'author'} + } } } - } - }) } + }) } + end + + context 'for a resource collection' do + let(:resource) { [post] } + + it { should eq({ + :data => [{ + :type => 'post', + :id => '1', + :links => { + :author => { + :linkage => {:id => '1', :type => 'author'} + } + } + }] + }) } + end end context 'with a block provided' do before do registry.map post_class do belongs_to(:user) { |post| post.author } end end - it { should eq({ - :data => { - :type => 'post', - :id => '1', - :links => { - :user => { - :linkage => {:id => '1', :type => 'author'} + context 'for an individual resource' do + let(:resource) { post } + + it { should eq({ + :data => { + :type => 'post', + :id => '1', + :links => { + :user => { + :linkage => {:id => '1', :type => 'author'} + } } } - } - }) } + }) } + end + + context 'for a resource collection' do + let(:resource) { [post] } + + it { should eq({ + :data => [{ + :type => 'post', + :id => '1', + :links => { + :user => { + :linkage => {:id => '1', :type => 'author'} + } + } + }] + }) } + end end context 'with the :embed_in_root option' do before do registry.map post_class do belongs_to :author, :embed_in_root => true end end - it { should eq({ - :data => { - :type => 'post', - :id => '1', - :links => { - :author => { - :linkage => {:id => '1', :type => 'author'} + context 'for an individual resource' do + let(:resource) { post } + + it { should eq({ + :data => { + :type => 'post', + :id => '1', + :links => { + :author => { + :linkage => {:id => '1', :type => 'author'} + } } + }, + + :included => { + :authors => [{ + :id => '1', + :links => {}, + :type => 'author' + }] } - }, + }) } + end - :included => { - :authors => [{ + context 'for a resource collection' do + let(:resource) { [post] } + + it { should eq({ + :data => [{ + :type => 'post', :id => '1', - :links => {}, - :type => 'author' - }] - } - }) } + :links => { + :author => { + :linkage => {:id => '1', :type => 'author'} + } + } + }], + + :included => { + :authors => [{ + :id => '1', + :links => {}, + :type => 'author' + }] + } + }) } + end end context 'with the :polymorphic option' do before do registry.map post_class do belongs_to :author, :polymorphic => true end end - it { should eq({ - :data => { - :type => 'post', - :id => '1', - :links => { - :author => { - :linkage => {:id => '1', :type => 'author'} + context 'for an individual resource' do + let(:resource) { post } + + it { should eq({ + :data => { + :type => 'post', + :id => '1', + :links => { + :author => { + :linkage => {:id => '1', :type => 'author'} + } } } - } - }) } + }) } + end + + context 'for a resource collection' do + let(:resource) { [post] } + + it { should eq({ + :data => [{ + :type => 'post', + :id => '1', + :links => { + :author => { + :linkage => {:id => '1', :type => 'author'} + } + } + }] + }) } + end end end context 'with a has_many relationship' do let(:comment_class) { double('Comment', :name => 'Comment') } @@ -167,88 +305,176 @@ registry.map post_class do has_many :comments end end - it { should eq({ - :data => { - :type => 'post', - :id => '1', - :links => { - :comments => { - :linkage => [{:id => '1', :type => 'comment'}] + context 'for an individual resource' do + let(:resource) { post } + + it { should eq({ + :data => { + :type => 'post', + :id => '1', + :links => { + :comments => { + :linkage => [{:id => '1', :type => 'comment'}] + } } } - } - }) } + }) } + end + + context 'for a resource collection' do + let(:resource) { [post] } + + it { should eq({ + :data => [{ + :type => 'post', + :id => '1', + :links => { + :comments => { + :linkage => [{:id => '1', :type => 'comment'}] + } + } + }] + }) } + end end context 'with a block provided' do before do registry.map post_class do has_many(:replies) { |post| post.comments } end end - it { should eq({ - :data => { - :type => 'post', - :id => '1', - :links => { - :replies => { - :linkage => [{:id => '1', :type => 'comment'}] + context 'for an individual resource' do + let(:resource) { post } + + it { should eq({ + :data => { + :type => 'post', + :id => '1', + :links => { + :replies => { + :linkage => [{:id => '1', :type => 'comment'}] + } } } - } - }) } + }) } + end + + context 'for a resource collection' do + let(:resource) { [post] } + + it { should eq({ + :data => [{ + :type => 'post', + :id => '1', + :links => { + :replies => { + :linkage => [{:id => '1', :type => 'comment'}] + } + } + }] + }) } + end end context 'with the :embed_in_root option' do before do registry.map post_class do has_many :comments, :embed_in_root => true end end - it { should eq({ - :data => { - :type => 'post', - :id => '1', - :links => { - :comments => { - :linkage => [{:id => '1', :type => 'comment'}] + context 'for an individual resource' do + let(:resource) { post } + + it { should eq({ + :data => { + :type => 'post', + :id => '1', + :links => { + :comments => { + :linkage => [{:id => '1', :type => 'comment'}] + } } + }, + + :included => { + :comments => [{ + :type => 'comment', + :id => '1', + :links => {} + }] } - }, + }) } + end - :included => { - :comments => [{ - :type => 'comment', + context 'for a resource collection' do + let(:resource) { [post] } + + it { should eq({ + :data => [{ + :type => 'post', :id => '1', - :links => {} - }] - } - }) } + :links => { + :comments => { + :linkage => [{:id => '1', :type => 'comment'}] + } + } + }], + + :included => { + :comments => [{ + :type => 'comment', + :id => '1', + :links => {} + }] + } + }) } + end end context 'with the :polymorphic option' do before do registry.map post_class do has_many :comments, :polymorphic => true end end - it { should eq({ - :data => { - :type => 'post', - :id => '1', - :links => { - :comments => { - :linkage => [{:id => '1', :type => 'comment'}] + context 'for an individual resource' do + let(:resource) { post } + + it { should eq({ + :data => { + :type => 'post', + :id => '1', + :links => { + :comments => { + :linkage => [{:id => '1', :type => 'comment'}] + } } } - } - }) } + }) } + end + + context 'for a resource collection' do + let(:resource) { [post] } + + it { should eq({ + :data => [{ + :type => 'post', + :id => '1', + :links => { + :comments => { + :linkage => [{:id => '1', :type => 'comment'}] + } + } + }] + }) } + end end end end end