Sha256: c0d15a697a7c1b2af6864283b3e7c77bbc285091cf9047d498c7b9d264016800

Contents?: true

Size: 1.33 KB

Versions: 9

Compression:

Stored size: 1.33 KB

Contents

RSpec.describe ROM::Relation, '#by_pk' do
  include_context 'users and tasks'

  with_adapters do
    context 'with a single PK' do
      subject(:relation) { relations[:users] }

      it 'restricts a relation by its PK' do
        expect(relation.by_pk(1).to_a).to eql([id: 1, name: 'Jane'])
      end

      it 'is available as a view' do
        expect(relation.by_pk).to be_curried
      end

      it 'qualifies pk attr' do
        expect(relation.qualified.by_pk(1).select(:id).join(:tasks, user_id: :id).one).to eql(id: 1)
      end
    end

    context 'with a composite PK' do
      subject(:relation) { relations[:task_tags] }

      it 'restricts a relation by is PK' do
        expect(relation.by_pk(1, 1).to_a).to eql([{ tag_id: 1, task_id: 1 }])
      end
    end

    context 'without PK' do
      subject(:relation) { relations[:people] }

      before do
        conn.drop_table?(:people)

        conn.create_table(:people) do
          column :name, String
        end

        conf.relation(:people) do
          schema do
            attribute :name, ROM::SQL::Types::String
          end
        end
      end

      it 'raises a meaningful exception' do
        expect { relation.by_pk(1) }.to \
          raise_error(
            ROM::SQL::MissingPrimaryKeyError,
            'Missing primary key for :people')
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rom-sql-1.2.2 spec/unit/relation/by_pk_spec.rb
rom-sql-1.2.1 spec/unit/relation/by_pk_spec.rb
rom-sql-1.2.0 spec/unit/relation/by_pk_spec.rb
rom-sql-1.1.2 spec/unit/relation/by_pk_spec.rb
rom-sql-1.1.1 spec/unit/relation/by_pk_spec.rb
rom-sql-1.1.0 spec/unit/relation/by_pk_spec.rb
rom-sql-1.0.3 spec/unit/relation/by_pk_spec.rb
rom-sql-1.0.2 spec/unit/relation/by_pk_spec.rb
rom-sql-1.0.1 spec/unit/relation/by_pk_spec.rb