-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fake queries should look and feel like real ones in test
- Loading branch information
Showing
4 changed files
with
153 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
# rbs_inline: enabled | ||
|
||
module Quo | ||
module Fakes | ||
class CollectionBackedFake < CollectionBackedQuery | ||
prop :results, _Any, reader: false | ||
prop :page_count, _Nilable(Integer), reader: false | ||
|
||
def collection | ||
@results | ||
end | ||
|
||
def results | ||
klass = Class.new(CollectionResults) do | ||
def page_count | ||
@query.page_count | ||
end | ||
end | ||
klass.new(self) | ||
end | ||
|
||
def page_count | ||
@page_count || validated_query.size | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
# rbs_inline: enabled | ||
|
||
module Quo | ||
module Fakes | ||
class RelationBackedFake < RelationBackedQuery | ||
prop :results, _Any, reader: false | ||
prop :page_count, _Nilable(Integer), reader: false | ||
prop :total_count, _Nilable(Integer), reader: false | ||
|
||
def query | ||
@results | ||
end | ||
|
||
def results | ||
klass = Class.new(RelationResults) do | ||
def page_count | ||
@query.page_count | ||
end | ||
|
||
def total_count | ||
@query.total_count | ||
end | ||
end | ||
klass.new(self) | ||
end | ||
|
||
def page_count | ||
@page_count || validated_query.size | ||
end | ||
|
||
def total_count | ||
@total_count || validated_query.size | ||
end | ||
|
||
private | ||
|
||
def validated_query | ||
query | ||
end | ||
|
||
def underlying_query | ||
validated_query | ||
end | ||
|
||
def configured_query | ||
validated_query | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../test_helper" | ||
require "quo/minitest/helpers" | ||
|
||
class Quo::FakeQueryTest < ActiveSupport::TestCase | ||
include Quo::Minitest::Helpers | ||
|
||
def create_query_class | ||
Class.new(Quo::CollectionBackedQuery) do | ||
include Quo::Preloadable | ||
|
||
def collection | ||
[1, 2, 3] | ||
end | ||
end | ||
end | ||
|
||
test "RelationBackedFake acts like a RelationBackedQuery" do | ||
fake_query(UnreadCommentsQuery, results: [1, 2]) do | ||
q = UnreadCommentsQuery.new | ||
assert q.results.is_a?(Quo::RelationResults) | ||
assert q.is_a?(Quo::RelationBackedQuery) | ||
assert_equal 2, q.results.count | ||
assert_equal 1, q.results.first | ||
assert_nothing_raised do | ||
q.includes(:foo).order(:bar).limit(10).preload(:x).results.first | ||
end | ||
end | ||
end | ||
|
||
test "CollectionBackedFake acts like a CollectionBackedQuery" do | ||
klass = create_query_class | ||
fake_query(klass, results: [1, 2]) do | ||
q = klass.new | ||
assert q.results.is_a?(Quo::CollectionResults) | ||
assert q.is_a?(Quo::CollectionBackedQuery) | ||
assert_equal 2, q.results.count | ||
assert_equal 1, q.results.first | ||
assert_nothing_raised do | ||
q.preload(:x).results.first | ||
end | ||
end | ||
end | ||
end |