Skip to content

Commit

Permalink
Add test for batching items with hash-identical keys
Browse files Browse the repository at this point in the history
  • Loading branch information
DouweM committed Dec 3, 2018
1 parent 119be9d commit b8e2af3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
32 changes: 30 additions & 2 deletions spec/batch_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,42 @@
end
end

laoded_author = batch_loader.call(Author, 1)
loader_author = batch_loader.call(Author, 1)
loader_reader = batch_loader.call(Reader, 2)

expect(Author).to receive(:where).with(id: [1]).once.and_call_original
expect(laoded_author).to eq(author)
expect(loader_author).to eq(author)
expect(Reader).to receive(:where).with(id: [2]).once.and_call_original
expect(loader_reader).to eq(reader)
end

it 'batches multiple items with hash-identical keys' do
user = Author.new(id: 1)
same_user = Reader.new(id: 1)
other_user = Reader.new(id: 2)

post_1 = Post.save(user_id: 1, title: "First post")
post_2 = Post.save(user_id: 1, title: "Second post")
post_3 = Post.save(user_id: 2, title: "First post")

batch_loader = ->(user, title) do
BatchLoader.for(title).batch(key: user) do |titles, loader, args|
args[:key].posts.select { |p| titles.include?(p.title) }.each { |post| loader.call(post.title, post) }
end
end

loader_1 = batch_loader.call(user, "First post")
loader_2 = batch_loader.call(same_user, "Second post")
loader_3 = batch_loader.call(other_user, "First post")

expect(user).to receive(:posts).once.and_call_original
expect(same_user).not_to receive(:posts)
expect(other_user).to receive(:posts).once.and_call_original

expect(loader_1).to eq(post_1)
expect(loader_2).to eq(post_2)
expect(loader_3).to eq(post_3)
end
end

context 'loader' do
Expand Down
21 changes: 17 additions & 4 deletions spec/fixtures/models.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class Post
attr_accessor :user_id
attr_accessor :user_id, :title

class << self
def save(user_id:)
def save(user_id:, title: nil)
ensure_init_store
@posts << new(user_id: user_id)
new(user_id: user_id, title: title).tap { |post| @posts << post }
end

def all
Expand All @@ -23,8 +23,9 @@ def ensure_init_store
end
end

def initialize(user_id:)
def initialize(user_id:, title: nil)
self.user_id = user_id
self.title = title || "Untitled"
end

def user_lazy(cache: true)
Expand Down Expand Up @@ -68,6 +69,18 @@ def batch
"Batch from User"
end

def hash
[User, id].hash
end

def posts
Post.all.select { |p| p.user_id == id }
end

def eql?(other)
other.is_a?(User) && id == other.id
end

private

def some_private_method
Expand Down

0 comments on commit b8e2af3

Please sign in to comment.