Sha256: fe1df38514d6ce76963347015eb34a531fe9c36bd626fda935f806c4f6041e7d

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

RSpec.describe ROM::Repository::Root, '#aggregate' do
  subject(:repo) do
    Class.new(ROM::Repository[:users]) do
      relations :tasks, :posts, :labels
    end.new(rom)
  end

  include_context 'database'
  include_context 'relations'
  include_context 'seeds'

  it 'exposes nodes via `node` method' do
    jane = repo.
             aggregate(:posts).
             node(:posts) { |posts| posts.where(title: 'Another one') }.
             where(name: 'Jane').one

    expect(jane.name).to eql('Jane')
    expect(jane.posts).to be_empty

    repo.posts.insert author_id: 1, title: 'Another one'

    jane = repo.
             aggregate(:posts).
             node(:posts) { |posts| posts.where(title: 'Another one') }.
             where(name: 'Jane').one

    expect(jane.name).to eql('Jane')
    expect(jane.posts.size).to be(1)
    expect(jane.posts[0].title).to eql('Another one')
  end

  it 'exposes nested nodes via `node` method' do
    jane = repo.
             aggregate(posts: :labels).
             node(posts: :labels) { |labels| labels.where(name: 'red') }.
             where(name: 'Jane').one

    expect(jane.name).to eql('Jane')
    expect(jane.posts.size).to be(1)
    expect(jane.posts[0].labels.size).to be(1)
    expect(jane.posts[0].labels[0].name).to eql('red')
  end

  it 'raises arg error when invalid relation name was passed to `node` method' do
    expect { repo.aggregate(:posts).node(:poztz) {} }.
      to raise_error(ArgumentError, ':poztz is not a valid aggregate node name')
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rom-repository-1.3.2 spec/integration/repository/aggregate_spec.rb
rom-repository-1.3.1 spec/integration/repository/aggregate_spec.rb
rom-repository-1.3.0 spec/integration/repository/aggregate_spec.rb
rom-repository-1.2.0 spec/integration/repository/aggregate_spec.rb
rom-repository-1.1.0 spec/integration/repository/aggregate_spec.rb