require 'assert' require 'mr/fake_query' require 'mr/fake_record' require 'mr/model' require 'mr/query' class MR::FakeQuery class UnitTests < Assert::Context desc "MR::FakeQuery" setup do # +1 so we always have at least 2 results @results = (Factory.integer(3) + 1).times.map do FakeTestModel.new.tap(&:save) end @query = MR::FakeQuery.new(@results) end subject{ @query } should have_readers :results, :first, :count should have_imeths :results!, :first!, :count!, :paged should "know its results and count" do assert_equal @results, subject.results assert_equal @results.size, subject.count end should "default its results and count" do query = MR::FakeQuery.new(nil) assert_equal [], query.results assert_equal 0, query.count end should "know its first result" do assert_equal @results.first, subject.first query = MR::FakeQuery.new([nil, []].sample) assert_nil query.first end should "alias its results, first, and count methods" do assert_same subject.results, subject.results! assert_same subject.first, subject.first! assert_same subject.count, subject.count! end should "return an instance of a `FakePagedQuery` with #paged" do assert_instance_of MR::FakePagedQuery, subject.paged end end class FakePagedQueryTests < UnitTests desc "MR::FakePagedQuery" setup do @page_num = Factory.integer(@results.size) @page_size = 1 @paged_query_class = MR::FakePagedQuery @paged_query = @paged_query_class.new(@query, @page_num, @page_size) end subject{ @paged_query } should have_readers :page_num, :page_size, :page_offset, :total_count should have_imeths :total_count!, :has_next_page?, :is_last_page? should "be a kind of MR::FakeQuery" do assert_kind_of MR::FakeQuery, subject end should "know its page num/size/offset" do assert_equal @page_num, subject.page_num assert_equal @page_size, subject.page_size exp = MR::PagedQuery::PageOffset.new(@page_num, @page_size) assert_equal exp, subject.page_offset end should "know its paged results" do exp = @results[subject.page_offset, subject.page_size] assert_equal exp, subject.results end should "know its paged result count" do assert_equal @page_size, subject.count end should "know its total number of results" do assert_equal @results.size, subject.total_count end should "alias its total count" do assert_same subject.total_count, subject.total_count! end should "know if it has a next page or is the last page" do unpaged_results = @results.dup Assert.stub(@results, :dup){ unpaged_results } page_num = Factory.integer(10) page_size = Factory.integer(10) page_offset = MR::PagedQuery::PageOffset.new(page_num, page_size) page_end = page_offset + page_size total_count = Factory.integer(page_size - 1) + page_end Assert.stub(unpaged_results, :size){ total_count } paged_query = @paged_query_class.new(@query, page_num, page_size) assert_true paged_query.has_next_page? assert_false paged_query.is_last_page? total_count = Factory.integer(page_size - 1) + page_offset Assert.stub(unpaged_results, :size){ total_count } paged_query = @paged_query_class.new(@query, page_num, page_size) assert_false paged_query.has_next_page? assert_true paged_query.is_last_page? Assert.stub(unpaged_results, :size){ page_end } paged_query = @paged_query_class.new(@query, page_num, page_size) assert_false paged_query.has_next_page? assert_true paged_query.is_last_page? end end class FakeTestRecord include MR::FakeRecord end class FakeTestModel include MR::Model record_class FakeTestRecord end end