Sha256: 1ac7cf9ea55a8f8b4319bcbac1694a23fae02f94ef60e5b2fedabe2bd39846ad

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

require 'helper'

describe 'Enhanced Finder Methods' do
  after do
    User.destroy_all
  end

  describe '.all' do
    it 'should return all records when no params' do
      User.all.to_sql.must_be_like %{
        SELECT "users".* FROM "users"
      }
    end

    it 'extracts the non-where scopes and applies' do
      test_sql = User.all(:limit => 1, :offset => 2, :order => :name, :age => 10).to_sql
      expected_sql = User.where(:age => 10).limit(1).offset(2).order(:name).to_sql
      test_sql.must_be_like expected_sql
    end
  end

  describe '.first' do
    it 'calls #first on .all' do
      expected = User.create(:age => 10)
      User.create(:age => 11)
      User.first(:age => 10).must_equal expected
    end
  end

  describe '.count' do
    it 'calls #count on .all' do
      User.create(:age => 10)
      User.create(:age => 11)
      User.count(:age => 10).must_equal 1
    end
  end

  describe 'hash contains :conditions' do
    before do
      @expected = User.create(:name => 'test')
      User.create(:name => 'no test')
    end

    it 'falls back for .all' do
      User.all(:conditions => "name = 'test'").must_equal [@expected]
    end

    it 'falls back for .first' do
      User.first(:conditions => "name = 'test'").must_equal @expected
    end

    it 'falls back for .count' do
      User.count(:conditions => "name = 'test'").must_equal 1
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
better_ar-0.0.3 test/test_finder_methods.rb