aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/dynamic_finder_match_test.rb
blob: e5768703176d88e9b716ffb6fe30f82ceeb0baa6 (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require "cases/helper"

module ActiveRecord
  class DynamicFinderMatchTest < ActiveRecord::TestCase
    def test_find_or_create_by
      match = DynamicFinderMatch.match("find_or_create_by_age_and_sex_and_location")
      assert_not_nil match
      assert !match.finder?
      assert match.instantiator?
      assert_equal :first, match.finder
      assert_equal :create, match.instantiator
      assert_equal %w(age sex location), match.attribute_names
    end

    def test_find_or_initialize_by
      match = DynamicFinderMatch.match("find_or_initialize_by_age_and_sex_and_location")
      assert_not_nil match
      assert !match.finder?
      assert match.instantiator?
      assert_equal :first, match.finder
      assert_equal :new, match.instantiator
      assert_equal %w(age sex location), match.attribute_names
    end

    def test_find_no_match
      assert_nil DynamicFinderMatch.match("not_a_finder")
    end

    def find_by_bang
      match = DynamicFinderMatch.match("find_by_age_and_sex_and_location!")
      assert_not_nil match
      assert match.finder?
      assert match.bang?
      assert_equal :first, match.finder
      assert_equal %w(age sex location), match.attribute_names
    end

    def test_find_by
      match = DynamicFinderMatch.match("find_by_age_and_sex_and_location")
      assert_not_nil match
      assert match.finder?
      assert_equal :first, match.finder
      assert_equal %w(age sex location), match.attribute_names
    end

    def test_find_by_with_symbol
      m = DynamicFinderMatch.match(:find_by_foo)
      assert_equal :first, m.finder
      assert_equal %w{ foo }, m.attribute_names
    end

    def test_find_all_by_with_symbol
      m = DynamicFinderMatch.match(:find_all_by_foo)
      assert_equal :all, m.finder
      assert_equal %w{ foo }, m.attribute_names
    end

    def test_find_all_by
      match = DynamicFinderMatch.match("find_all_by_age_and_sex_and_location")
      assert_not_nil match
      assert match.finder?
      assert_equal :all, match.finder
      assert_equal %w(age sex location), match.attribute_names
    end

    def test_find_last_by
      m = DynamicFinderMatch.match(:find_last_by_foo)
      assert_equal :last, m.finder
      assert_equal %w{ foo }, m.attribute_names
    end

    def test_find_by!
      m = DynamicFinderMatch.match(:find_by_foo!)
      assert_equal :first, m.finder
      assert m.bang?, 'should be banging'
      assert_equal %w{ foo }, m.attribute_names
    end

    def test_find_or_create
      m = DynamicFinderMatch.match(:find_or_create_by_foo)
      assert_equal :first, m.finder
      assert_equal %w{ foo }, m.attribute_names
      assert_equal :create, m.instantiator
    end

    def test_find_or_initialize
      m = DynamicFinderMatch.match(:find_or_initialize_by_foo)
      assert_equal :first, m.finder
      assert_equal %w{ foo }, m.attribute_names
      assert_equal :new, m.instantiator
    end

    def test_garbage
      assert !DynamicFinderMatch.match(:fooo), 'should be false'
      assert !DynamicFinderMatch.match(:find_by), 'should be false'
    end
  end
end