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.36 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.35 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.34 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.33 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.32 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.31 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.30 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.29 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.28 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.27 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.26 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.25 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.24 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.23 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.22 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.21 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.20 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.19 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.18 spec/simple/sql/count_by_groups_spec.rb
simple-sql-0.5.17 spec/simple/sql/count_by_groups_spec.rb