require 'helper' describe 'ActiveRecord::Relation Finder Methods' do after do User.destroy_all end describe '.all' do 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 it 'finds implicit joins by reflection' do test_sql = User.all(:records => { :name => 'test' }).to_sql expected_sql = User.joins(:records).where(:records => { :name => 'test' }).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 = User.create(:name => 'no test') end describe 'with conditions' do 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 describe 'without conditions' do it 'falls back for .all' do User.all.must_equal [@expected, @user] end it 'falls back for .first' do User.first.must_equal @expected end it 'falls back for .count' do User.count.must_equal 2 end end end end describe 'ActiveRecord::Associations::AssociationCollection Finder Methods' do before do @user = User.create @record_1 = @user.records.create(:name => 'one') @record_2 = @user.records.create(:name => 'two') end after do User.destroy_all Record.destroy_all end describe '.all' do it 'extracts the non-where scopes and applies' do test_sql = @user.records.all(:limit! => 1, :offset! => 2, :order! => :name, :name => 'one').to_sql expected_sql = @user.records.where(:name => 'one').limit(1).offset(2).order(:name).to_sql test_sql.must_be_like expected_sql end it 'finds implicit joins by reflection' do test_sql = @user.records.all(:reports => { :name => 'test' }).to_sql expected_sql = @user.records.joins(:reports).where(:reports => { :name => 'test' }).to_sql test_sql.must_be_like expected_sql end end describe '.first' do it 'calls #first on .all' do @user.records.first(:name => 'two').must_equal @record_2 end end describe '.count' do it 'calls #count on .all' do @user.records.count(:name => 'one').must_equal 1 end end describe 'hash contains :conditions' do describe 'with conditions' do it 'falls back for .all' do @user.records.all(:conditions => "name = 'one'").must_equal [@record_1] end it 'falls back for .first' do @user.records.create(:name => 'one') @user.records.first(:conditions => "name = 'one'").must_equal @record_1 end # Note a valid test... need to rewrite. # it 'falls back for .count' do # @user.records.count(:conditions => "name = 'one'").must_equal 1 # end end describe 'without conditions' do it 'falls back for .all' do @user.records.all.must_equal [@record_1, @record_2] end it 'falls back for .first' do @user.records.first.must_equal @record_1 end it 'falls back for .count' do @user.records.count.must_equal 2 end end end end