Sha256: a486118374fac2c0ec192036a870c015361d346708930d4908866153c962cd4b

Contents?: true

Size: 2 KB

Versions: 1

Compression:

Stored size: 2 KB

Contents

RSpec.describe 'struct builder', '#call' do
  subject(:builder) { ROM::Repository::StructBuilder.new }

  def attr_double(name, type, **opts)
    double(
      name: name,
      aliased?: false,
      wrapped?: false,
      foreign_key?: false,
      to_read_type: ROM::Types.const_get(type),
      **opts
    )
  end

  let(:input) do
    [:users, [:header, [
                [:attribute, attr_double(:id, :Int)],
                [:attribute, attr_double(:name, :String)]]]]
  end

  before { builder[*input] }

  it 'generates a struct for a given relation name and columns' do
    struct = builder.class.cache[input.hash]

    user = struct.new(id: 1, name: 'Jane')

    expect(user.id).to be(1)
    expect(user.name).to eql('Jane')

    expect(user[:id]).to be(1)
    expect(user[:name]).to eql('Jane')

    expect(Hash[user]).to eql(id: 1, name: 'Jane')

    expect(user.inspect).to eql('#<ROM::Struct[User] id=1 name="Jane">')
    expect(user.to_s).to match(/\A#<ROM::Struct\[User\]:0x[0-9a-f]+>\z/)
  end

  it 'stores struct in the cache' do
    expect(builder.class.cache[input.hash]).to be(builder[*input])
  end

  context 'with reserved keywords as attribute names' do
    let(:input) do
      [:users, [:header, [
                  [:attribute, attr_double(:id, :Int)],
                  [:attribute, attr_double(:name, :String)],
                  [:attribute, attr_double(:alias, :String)],
                  [:attribute, attr_double(:until, :Time)]]]]
    end

    it 'allows to build a struct class without complaining' do
      struct = builder.class.cache[input.hash]

      user = struct.new(id: 1, name: 'Jane', alias: 'JD', until: Time.new(2030))

      expect(user.id).to be(1)
      expect(user.name).to eql('Jane')
      expect(user.alias).to eql('JD')
      expect(user.until).to eql(Time.new(2030))
    end
  end

  it 'raise a friendly error on missing keys' do
    struct = builder.class.cache[input.hash]

    expect { struct.new(id: 1) }.to raise_error(
      Dry::Struct::Error, /:name is missing/
    )
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-repository-1.0.2 spec/unit/struct_builder_spec.rb