Sha256: 20f82e158d9209faed08fe074113562d3a3f2778f689cd6e34ddf14aeb9c5398

Contents?: true

Size: 1.48 KB

Versions: 29

Compression:

Stored size: 1.48 KB

Contents

require "spec_helper"

describe "Simple::SQL::Result counts" do
  let!(:users)          { 1.upto(USER_COUNT).map { create(:user) } }
  let(:min_user_id)     { SQL.ask "SELECT min(id) FROM users" }
  let(:scope)           { SQL.scope("SELECT * FROM users") }
  let(:paginated_scope) { scope.paginate(per: 1, page: 1) }

  describe "exact counting" do
    it "counts" do
      result = SQL.all(paginated_scope)
      expect(result.total_count).to eq(USER_COUNT)
      expect(result.current_page).to eq(1)
    end
  end
  
  describe "fast counting" do
    it "counts fast" do
      result = SQL.all(paginated_scope)

      expect(result.total_count_estimate).to eq(USER_COUNT)
      expect(result.current_page).to eq(1)
    end
  end

  context 'when running with a non-paginated paginated_scope' do
    it "raises errors" do
      result = SQL.all(scope)

      expect { result.total_count }.to raise_error(RuntimeError)
      expect { result.current_page }.to raise_error(RuntimeError)
      expect { result.total_count_estimate }.to raise_error(RuntimeError)
    end
  end


  context 'when running with an empty, paginated paginated_scope' do
    let(:scope)           { SQL.scope("SELECT * FROM users WHERE FALSE") }
    let(:paginated_scope) { scope.paginate(per: 1, page: 1) }

    it "returns correct results" do
      result = SQL.all(paginated_scope)

      expect(result.total_count).to eq(0)
      expect(result.total_count_estimate).to eq(0)
      expect(result.current_page).to eq(1)
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
simple-sql-0.5.37 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.36 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.35 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.34 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.33 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.32 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.31 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.30 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.29 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.28 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.27 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.26 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.25 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.24 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.23 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.22 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.21 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.20 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.19 spec/simple/sql/result_count_spec.rb
simple-sql-0.5.18 spec/simple/sql/result_count_spec.rb