File: finder_test.rb

package info (click to toggle)
ruby-activerecord-deprecated-finders 1.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 188 kB
  • ctags: 94
  • sloc: ruby: 1,026; makefile: 2
file content (69 lines) | stat: -rw-r--r-- 2,111 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'helper'

describe 'finders' do
  before do
    @posts = 3.times.map { |i| Post.create id: i + 1 }
  end

  after do
    Post.delete_all
  end

  it 'supports find(:all) with no options' do
    assert_deprecated { Post.find(:all) }.must_equal @posts
  end

  it 'supports find(:all) with options' do
    assert_deprecated { Post.find(:all, conditions: 'id >= 2') }.must_equal [@posts[1], @posts[2]]
  end

  it 'supports all with options' do
    assert_deprecated { Post.all(conditions: 'id >= 2') }.must_equal [@posts[1], @posts[2]]
  end

  it 'returns an array from all when there are options' do
    posts = ActiveSupport::Deprecation.silence { Post.all(conditions: 'id >= 2') }
    posts.class.must_equal Array
  end

  it 'returns a relation from all when there are no options' do
    posts = Post.all
    posts.is_a?(ActiveRecord::Relation).must_equal true
  end

  it 'deprecates #all on a relation' do
    relation = Post.where('id >= 2')
    assert_deprecated { relation.all }.must_equal [@posts[1], @posts[2]]
  end

  it 'supports find(:first) with no options' do
    assert_deprecated { Post.order(:id).find(:first) }.must_equal @posts.first
  end

  it 'supports find(:first) with options' do
    assert_deprecated { Post.order(:id).find(:first, conditions: 'id >= 2') }.must_equal @posts[1]
  end

  it 'supports first with options' do
    assert_deprecated { Post.order(:id).first(conditions: 'id >= 2') }.must_equal @posts[1]
  end

  it 'supports find(:last) with no options' do
    assert_deprecated { Post.order(:id).find(:last) }.must_equal @posts.last
  end

  it 'supports find(:last) with options' do
    assert_deprecated { Post.order(:id).find(:last, conditions: 'id <= 2') }.must_equal @posts[1]
  end

  it 'supports last with options' do
    assert_deprecated { Post.order(:id).last(conditions: 'id <= 2') }.must_equal @posts[1]
  end

  it 'support find(1) etc with options' do
    assert_deprecated do
      Post.find(1, conditions: '1=1').must_equal Post.find(1)
      lambda { Post.find(1, conditions: '0=1') }.must_raise(ActiveRecord::RecordNotFound)
    end
  end
end