Sha256: 0add5b2f274baad179d379391952c4a4cfabab3d4ecfcd9ed2769a1cb3e1e3e1

Contents?: true

Size: 1.61 KB

Versions: 8

Compression:

Stored size: 1.61 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

      it 'works even when PK is not projected' do
        expect(relation.select(:name).by_pk(1).to_a).to eql([name: 'Jane'])
      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

      it 'works even when PK is not projected' do
        expect(relation.by_pk(1, 1).select { `1`.as(:num) }.to_a).to eql([num: 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

8 entries across 8 versions & 1 rubygems

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