Sha256: 2045ca32bbe76454e4bc762d5e5a4dea59f0566e6e80e0d4b02a9901129df4da

Contents?: true

Size: 1.52 KB

Versions: 9

Compression:

Stored size: 1.52 KB

Contents

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

  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

9 entries across 9 versions & 1 rubygems

Version Path
rom-sql-1.3.5 spec/unit/relation/left_join_spec.rb
rom-sql-1.3.4 spec/unit/relation/left_join_spec.rb
rom-sql-2.0.0.beta2 spec/unit/relation/left_join_spec.rb
rom-sql-2.0.0.beta1 spec/unit/relation/left_join_spec.rb
rom-sql-1.3.3 spec/unit/relation/left_join_spec.rb
rom-sql-1.3.2 spec/unit/relation/left_join_spec.rb
rom-sql-1.3.1 spec/unit/relation/left_join_spec.rb
rom-sql-1.3.0 spec/unit/relation/left_join_spec.rb
rom-sql-1.2.2 spec/unit/relation/left_join_spec.rb