Sha256: f47018ed70a243d20d662d54f70a4bd944534a0f37603a1b5d1a25f98ed900cd

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

require 'spec_helper'

RSpec.describe ROM::SQL::Association::OneToMany, '#call' do
  include_context 'database setup'

  before do
    inferrable_relations.concat %i(categories)
  end

  subject(:assoc) do
    relations[:categories].associations[:children]
  end

  with_adapters do
    before do
      conn.create_table(:categories) do
        primary_key :id
        foreign_key :parent_id, :categories, null: true
        column :name, String, null: false
      end

      conf.relation(:categories) do
        schema(infer: true) do
          associations do
            belongs_to :categories, as: :parent, foreign_key: :parent_id
            has_many :categories, as: :children, foreign_key: :parent_id
          end
        end
      end

      p1_id = relations[:categories].insert(name: 'P1')
      p2_id = relations[:categories].insert(name: 'P2')
      relations[:categories].insert(name: 'C3', parent_id: p2_id)
      relations[:categories].insert(name: 'C4', parent_id: p1_id)
      relations[:categories].insert(name: 'C5', parent_id: p1_id)
    end

    it 'prepares joined relations using custom FK for a self-ref association' do
      relation = assoc.call(relations)

      expect(relation.schema.map(&:to_sym)).
        to eql(%i[categories__id categories__parent_id categories__name])

      expect(relation.to_a).
        to eql([
                 { id: 3, parent_id: 2, name: 'C3' },
                 { id: 4, parent_id: 1, name: 'C4' },
                 { id: 5, parent_id: 1, name: 'C5' }
               ])
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-sql-1.3.1 spec/integration/association/one_to_many/self_ref_spec.rb
rom-sql-1.3.0 spec/integration/association/one_to_many/self_ref_spec.rb
rom-sql-1.2.2 spec/integration/association/one_to_many/self_ref_spec.rb