Sha256: 0180b4f37fabdb6f5fca22162a2ad7145a60023828f21208b5d13b10f4ccead1

Contents?: true

Size: 1.48 KB

Versions: 28

Compression:

Stored size: 1.48 KB

Contents

require "spec_helper"

describe "Simple::SQL::Connection::Scope#count_by" do
  let!(:users)        { 1.upto(10).map { |i| create(:user, role_id: i) } }
  let(:all_role_ids)  { SQL.all("SELECT DISTINCT role_id FROM users") }
  let(:scope)         { SQL.scope("SELECT * FROM users") }

  describe "enumerate_groups" do
    it "returns all groups" do
      expect(scope.enumerate_groups("role_id")).to contain_exactly(*all_role_ids)
      expect(scope.where("role_id < 4").enumerate_groups("role_id")).to contain_exactly(*(1.upto(3).to_a))
    end
  end

  describe "count_by" do
    it "counts all groups" do
      create(:user, role_id: 1)
      create(:user, role_id: 1)
      create(:user, role_id: 1)

      expect(scope.count_by("role_id")).to include(1 => 4)
      expect(scope.count_by("role_id")).to include(2 => 1)
      expect(scope.count_by("role_id").keys).to contain_exactly(*all_role_ids)
    end
  end

  describe "count_by_estimate" do
    before do
      # 10_000 is chosen "magically". It is large enough to switch to the fast algorithm,
      # but 
      allow(::Simple::SQL).to receive(:costs).and_return([0, 10_000])
    end
    
    it "counts all groups" do
      create(:user, role_id: 1)
      create(:user, role_id: 1)
      create(:user, role_id: 1)

      expect(scope.count_by_estimate("role_id")).to include(1 => 4)
      expect(scope.count_by_estimate("role_id")).to include(2 => 1)
      expect(scope.count_by_estimate("role_id").keys).to contain_exactly(*all_role_ids)
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
simple-sql-0.5.16 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.15 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.14 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.13 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.12 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.11 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.10 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.9 spec/simple/sql/count_by_groups_spec.rb