Sha256: ff4163bcc9de5de89f6ac7559ec7c7e667a22775f7671b79ce15efff9d11f769

Contents?: true

Size: 1.55 KB

Versions: 14

Compression:

Stored size: 1.55 KB

Contents

RSpec.describe ROM::Relation, '#left_join' do
  subject(:relation) { relations[:users] }

  let(:tasks) { relations[:tasks] }

  include_context 'users and tasks'

  with_adapters do
    it 'joins relations using left outer join' do
      relation.insert id: 3, name: 'Jade'

      result = relation.
                 left_join(:tasks, user_id: :id).
                 select(:name, tasks[:title])

      expect(result.schema.map(&:name)).to eql(%i[name title])

      expect(result.to_a).to match_array([
        { name: 'Joe', title: "Joe's task" },
        { name: 'Jane', title: "Jane's task" },
        { name: 'Jade', title: nil }
      ])
    end

    context 'with associations' do
      before do
        conf.relation(:users) do
          schema(infer: true) do
            associations { has_many :tasks }
          end
        end

        conf.relation(:tasks) do
          schema(infer: true) do
            associations { belongs_to :user }
          end
        end

        relation.insert id: 3, name: 'Jade'
      end

      it 'joins relation with join keys inferred' do
        result = relation.
                   left_join(tasks).
                   select(:name, tasks[:title])

        expect(result.schema.map(&:name)).to eql(%i[name title])

        expect(result.to_a).to eql([
                                     { name: 'Jane', title: "Jane's task" },
                                     { name: 'Joe', title: "Joe's task" },
                                     { name: 'Jade', title: nil }
                                   ])
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rom-sql-1.2.1 spec/unit/relation/left_join_spec.rb
rom-sql-1.2.0 spec/unit/relation/left_join_spec.rb
rom-sql-1.1.2 spec/unit/relation/left_join_spec.rb
rom-sql-1.1.1 spec/unit/relation/left_join_spec.rb
rom-sql-1.1.0 spec/unit/relation/left_join_spec.rb
rom-sql-1.0.3 spec/unit/relation/left_join_spec.rb
rom-sql-1.0.2 spec/unit/relation/left_join_spec.rb
rom-sql-1.0.1 spec/unit/relation/left_join_spec.rb
rom-sql-1.0.0 spec/unit/relation/left_join_spec.rb
rom-sql-1.0.0.rc2 spec/unit/relation/left_join_spec.rb
rom-sql-1.0.0.rc1 spec/unit/relation/left_join_spec.rb
rom-sql-1.0.0.beta3 spec/unit/relation/left_join_spec.rb
rom-sql-1.0.0.beta2 spec/unit/relation/left_join_spec.rb
rom-sql-1.0.0.beta1 spec/unit/relation/left_join_spec.rb