Skip to content

Commit

Permalink
Dont call back to query for total count
Browse files Browse the repository at this point in the history
  • Loading branch information
stevegeek committed Oct 7, 2024
1 parent f182c8d commit 1b9904f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 28 deletions.
4 changes: 2 additions & 2 deletions lib/quo/collection_backed_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Quo
class CollectionBackedQuery < Query
prop :total_count, _Nilable(Integer)
prop :total_count, _Nilable(Integer), reader: false

# Wrap an enumerable collection or a block that returns an enumerable collection
# @rbs data: untyped, props: Symbol => untyped, block: () -> untyped
Expand Down Expand Up @@ -43,7 +43,7 @@ def query
end

def results
Quo::CollectionResults.new(self, transformer: transformer)
Quo::CollectionResults.new(self, transformer: transformer, total_count: @total_count)
end

# @rbs override
Expand Down
5 changes: 3 additions & 2 deletions lib/quo/collection_results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
module Quo
class CollectionResults < Results
# @rbs override
def initialize(query, transformer: nil)
def initialize(query, transformer: nil, total_count: nil)
raise ArgumentError, "Query must be a CollectionBackedQuery" unless query.is_a?(Quo::CollectionBackedQuery)
@total_count = total_count
@query = query
@configured_query = query.unwrap
@transformer = transformer
Expand All @@ -27,7 +28,7 @@ def empty? #: bool
# of wrapped collection.
# @rbs override
def total_count #: Integer
@query.total_count || @query.unwrap_unpaginated.size
@total_count || @query.unwrap_unpaginated.size
end

# Gets the actual count of elements in the page of results (assuming paging is being used, otherwise the count of
Expand Down
26 changes: 2 additions & 24 deletions test/quo/minitest_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,11 @@
class Quo::MinitestHelpersTest < ActiveSupport::TestCase
include Quo::Minitest::Helpers

test "stub_query" do
stub_query(CommentNotSpamQuery, results: [1, 2]) do
test "fake_query" do
fake_query(CommentNotSpamQuery, results: [1, 2]) do
q = CommentNotSpamQuery.new(spam_score_threshold: 0.8)
assert_equal 2, q.results.count
assert_equal 1, q.results.first
end
end

test "mock_query with arguments" do
mock = mock_query(CommentNotSpamQuery, kwargs: {spam_score_threshold: 0.8}, results: [1, 2])
stub_query(CommentNotSpamQuery, mock: mock, results: [1, 2]) do
q = CommentNotSpamQuery.new(spam_score_threshold: 0.8)
assert_equal 2, q.results.count
assert_equal 1, q.results.first
assert_mock mock
end
end

test "raises when mock args don't match" do
mock = mock_query(CommentNotSpamQuery, kwargs: {spam_score_threshold: 0.8}, results: [1, 2])
stub_query(CommentNotSpamQuery, mock: mock, results: [1, 2]) do
assert_raises(ArgumentError) do
CommentNotSpamQuery.new
end
assert_raises(MockExpectationError) do
CommentNotSpamQuery.new(spam_score_threshold: 0.9)
end
end
end
end

0 comments on commit 1b9904f

Please sign in to comment.