Sha256: b82705092349ac8f9ae4a8b1defe867caccdd59bca0d8e354068a614192461ab

Contents?: true

Size: 1.29 KB

Versions: 10

Compression:

Stored size: 1.29 KB

Contents

require 'spec_helper'

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

  before do
    inferrable_relations.concat %i(puzzles)
  end

  subject(:assoc) do
    relations[:users].associations[:solved_puzzles]
  end

  with_adapters do
    before do
      conn.create_table(:puzzles) do
        primary_key :id
        foreign_key :author_id, :users, null: false
        foreign_key :solver_id, :users, null: true
        column :text, String, null: false
      end

      conf.relation(:users) do
        schema(infer: true) do
          associations do
            has_many :puzzles, as: :created_puzzles, foreign_key: :author_id
            has_many :puzzles, as: :solved_puzzles, foreign_key: :solver_id
          end
        end
      end

      relations[:puzzles].insert(author_id: joe_id, text: 'P1')
      relations[:puzzles].insert(author_id: joe_id, solver_id: jane_id, text: 'P2')
    end

    after do
      conn.drop_table(:puzzles)
    end

    it 'prepares joined relations using custom FK' do
      relation = assoc.call(relations)

      expect(relation.schema.map(&:to_sym)).
        to eql(%i[puzzles__id puzzles__author_id puzzles__solver_id puzzles__text])

      expect(relation.first).to eql(id: 2, author_id: 2, solver_id: 1, text: 'P2')
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rom-sql-1.3.1 spec/integration/association/one_to_many/custom_fks_spec.rb
rom-sql-1.3.0 spec/integration/association/one_to_many/custom_fks_spec.rb
rom-sql-1.2.2 spec/integration/association/one_to_many/custom_fks_spec.rb
rom-sql-1.2.1 spec/integration/association/one_to_many/custom_fks_spec.rb
rom-sql-1.2.0 spec/integration/association/one_to_many/custom_fks_spec.rb
rom-sql-1.1.2 spec/integration/association/one_to_many/custom_fks_spec.rb
rom-sql-1.1.1 spec/integration/association/one_to_many/custom_fks_spec.rb
rom-sql-1.1.0 spec/integration/association/one_to_many/custom_fks_spec.rb
rom-sql-1.0.3 spec/integration/association/one_to_many/custom_fks_spec.rb
rom-sql-1.0.2 spec/integration/association/one_to_many/custom_fks_spec.rb