<commit_before>require 'spec_helper'
require 'oat/adapters/json_api'

describe Oat::Adapters::JsonAPI do

  include Fixtures

  let(:serializer) { serializer_class.new(user, {:name => 'some_controller'}, Oat::Adapters::JsonAPI) }
  let(:hash) { serializer.to_hash }

  describe '#to_hash' do
    it 'produces a JSON-API compliant hash' do
      # the user being serialized
      users = hash.fetch(:users)
      expect(users.size).to be 1
      expect(users.first).to include(
        :id => user.id,
        :name => user.name,
        :age => user.age,
        :controller_name => 'some_controller',
        :message_from_above => nil
      )

      expect(users.first.fetch(:links)).to include(
        :self => "http://foo.bar.com/#{user.id}",
        # these links are added by embedding entities
        :manager => manager.id,
        :friends => [friend.id]
      )

      # embedded friends
      linked_friends = hash.fetch(:linked).fetch(:friends)
      expect(linked_friends.size).to be 1
      expect(linked_friends.first).to include(
        :id => friend.id,
        :name => friend.name,
        :age => friend.age,
        :controller_name => 'some_controller',
        :message_from_above => "Merged into parent's context"
      )

      expect(linked_friends.first.fetch(:links)).to include(
        :self => "http://foo.bar.com/#{friend.id}",
        :empty => nil,
        :friends => []
      )

      # embedded manager
      linked_managers = hash.fetch(:linked).fetch(:managers)
      expect(linked_managers.size).to be 1
      expect(linked_managers.first).to include(
        :id => manager.id,
        :name => manager.name,
        :age => manager.age,
        :links => { :self => "http://foo.bar.com/#{manager.id}" }
      )
    end

    context 'with a nil entity relationship' do
      let(:manager) { nil }

      it 'produces a JSON-API compliant hash' do
        # the user being serialized
        users = hash.fetch(:users)
        expect(users.size).to be 1
        expect(users.first).to include(
          :id => user.id,
          :name => user.name,
          :age => user.age,
          :controller_name => 'some_controller',
          :message_from_above => nil
        )

        expect(users.first.fetch(:links)).not_to include(:manager)
        expect(users.first.fetch(:links)).to include(
          :self => "http://foo.bar.com/#{user.id}",
          # these links are added by embedding entities
          :friends => [friend.id]
        )
        # embedded friends
        linked_friends = hash.fetch(:linked).fetch(:friends)
        expect(linked_friends.size).to be 1
        expect(linked_friends.first).to include(
          :id => friend.id,
          :name => friend.name,
          :age => friend.age,
          :controller_name => 'some_controller',
          :message_from_above => "Merged into parent's context"
        )

        expect(linked_friends.first.fetch(:links)).to include(
          :self => "http://foo.bar.com/#{friend.id}",
          :empty => nil,
          :friends => []
        )

        # embedded manager
        hash.fetch(:linked).fetch(:managers).should be_empty
      end
    end
  end
end<commit_msg>Only test one thing per test<commit_after>require 'spec_helper'
require 'oat/adapters/json_api'

describe Oat::Adapters::JsonAPI do

  include Fixtures

  let(:serializer) { serializer_class.new(user, {:name => 'some_controller'}, Oat::Adapters::JsonAPI) }
  let(:hash) { serializer.to_hash }

  describe '#to_hash' do
    context 'top level' do
      subject(:users){ hash.fetch(:users) }
      its(:size) { should eq(1) }

      it 'contains the correct user properties' do
        expect(users.first).to include(
          :id => user.id,
          :name => user.name,
          :age => user.age,
          :controller_name => 'some_controller',
          :message_from_above => nil
        )
      end

      it 'contains the correct user links' do
        expect(users.first.fetch(:links)).to include(
          :self => "http://foo.bar.com/#{user.id}",
          # these links are added by embedding entities
          :manager => manager.id,
          :friends => [friend.id]
        )
      end
    end

    context 'linked' do
      context 'using #entities' do
        subject(:linked_friends){ hash.fetch(:linked).fetch(:friends) }

        its(:size) { should eq(1) }

        it 'contains the correct properties' do
          expect(linked_friends.first).to include(
            :id => friend.id,
            :name => friend.name,
            :age => friend.age,
            :controller_name => 'some_controller',
            :message_from_above => "Merged into parent's context"
          )
        end

        it 'contains the correct links' do
          expect(linked_friends.first.fetch(:links)).to include(
            :self => "http://foo.bar.com/#{friend.id}",
            :empty => nil,
            :friends => []
          )
        end
      end

      context 'using #entity' do
        subject(:linked_managers){ hash.fetch(:linked).fetch(:managers) }
        its(:size) { should eq(1) }

        it "contains the correct properties and links" do
          expect(linked_managers.first).to include(
            :id => manager.id,
            :name => manager.name,
            :age => manager.age,
            :links => { :self => "http://foo.bar.com/#{manager.id}" }
          )
        end
      end
    end

    context 'with a nil entity relationship' do
      let(:manager) { nil }
      let(:users) { hash.fetch(:users) }

      it 'excludes the entity from user links' do
        expect(users.first.fetch(:links)).not_to include(:manager)
      end

      it 'excludes the entity from the linked hash' do
        hash.fetch(:linked).fetch(:managers).should be_empty
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.13460546673543064, 'fraction_numerical': 0.0013752793536187038, 'mean_word_length': 2.1145610278372593, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6250363', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1678, 'n_words': 392}