Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for creating multiple instances in one DB call #65

Open
apohllo opened this issue Mar 9, 2021 · 1 comment
Open

Allow for creating multiple instances in one DB call #65

apohllo opened this issue Mar 9, 2021 · 1 comment

Comments

@apohllo
Copy link

apohllo commented Mar 9, 2021

Testing features such as pagination requires presence of a number of objects in the repository. Creation of these entities takes more time, since each faked object is created separately. Yet ROM allows for calling commands with multiple instances, which is much more efficient. It would be a nice feature if instead of

10.times { Factory[:user] }

we had

Factory[:user, instances: 10]

which would be executed as a single DB call.

@flash-gordon
Copy link
Member

This is what I use in these cases

# user_repo.rb
require 'dry/effects'

class UserRepo < ROM::Repository[:users]
  include ::Dry::Effects.Reader(:page_size, default: 20)

  def list(page: 1)
    users.per_page(page_size).page(page).to_a
  end
end

# user_repo_spec.rb
require 'dry/effects'
require 'user_repo'

RSpec.describe UserRepo do
  include Dry::Effects::Handler.Reader(:page_size)
  subject(:repo) { described_class.new }

  describe 'pagination' do
    around { with_page_size(3, &_1) }

    let(:users) { Array.new(4) { Factory[:user] } }

    example 'getting second page' do
      expect(repo.list(page: 2)).to eql(users[3..])
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants