Sha256: a31e9ad301a3cc7b38e30ea64492069e668221a4de1ad9f4ae4e0c12f78fc1c0

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

require 'spec_helper'

RSpec.describe ROM::SQL::Associations::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(:puzzles) { schema(infer: true) }

      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.()

      expect(relation.schema.map(&:to_sql_name)).
        to eql([Sequel.qualify(:puzzles, :id),
                Sequel.qualify(:puzzles, :author_id),
                Sequel.qualify(:puzzles, :solver_id),
                Sequel.qualify(:puzzles, :text)])

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rom-sql-2.0.0.beta2 spec/integration/associations/one_to_many/custom_fks_spec.rb
rom-sql-2.0.0.beta1 spec/integration/associations/one_to_many/custom_fks_spec.rb