aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/finder_respond_to_test.rb
blob: 235805a67ce749de3ccb8fd9549d3cc67d73327b (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
require "cases/helper"
require 'models/topic'

class FinderRespondToTest < ActiveRecord::TestCase

  fixtures :topics

  def test_should_preserve_normal_respond_to_behaviour_and_respond_to_newly_added_method
    class << Topic; self; end.send(:define_method, :method_added_for_finder_respond_to_test) { }
    assert_respond_to Topic, :method_added_for_finder_respond_to_test
  ensure
    class << Topic; self; end.send(:remove_method, :method_added_for_finder_respond_to_test)
  end

  def test_should_preserve_normal_respond_to_behaviour_and_respond_to_standard_object_method
    assert_respond_to Topic, :to_s
  end

  def test_should_respond_to_find_by_one_attribute_before_caching
    ensure_topic_method_is_not_cached(:find_by_title)
    assert_respond_to Topic, :find_by_title
  end

  def test_should_respond_to_find_all_by_one_attribute
    ensure_topic_method_is_not_cached(:find_all_by_title)
    assert_respond_to Topic, :find_all_by_title
  end

  def test_should_respond_to_find_all_by_two_attributes
    ensure_topic_method_is_not_cached(:find_all_by_title_and_author_name)
    assert_respond_to Topic, :find_all_by_title_and_author_name
  end

  def test_should_respond_to_find_by_two_attributes
    ensure_topic_method_is_not_cached(:find_by_title_and_author_name)
    assert_respond_to Topic, :find_by_title_and_author_name
  end

  def test_should_respond_to_find_or_initialize_from_one_attribute
    ensure_topic_method_is_not_cached(:find_or_initialize_by_title)
    assert_respond_to Topic, :find_or_initialize_by_title
  end

  def test_should_respond_to_find_or_initialize_from_two_attributes
    ensure_topic_method_is_not_cached(:find_or_initialize_by_title_and_author_name)
    assert_respond_to Topic, :find_or_initialize_by_title_and_author_name
  end

  def test_should_respond_to_find_or_create_from_one_attribute
    ensure_topic_method_is_not_cached(:find_or_create_by_title)
    assert_respond_to Topic, :find_or_create_by_title
  end

  def test_should_respond_to_find_or_create_from_two_attributes
    ensure_topic_method_is_not_cached(:find_or_create_by_title_and_author_name)
    assert_respond_to Topic, :find_or_create_by_title_and_author_name
  end

  def test_should_not_respond_to_find_by_one_missing_attribute
    assert !Topic.respond_to?(:find_by_undertitle)
  end

  def test_should_not_respond_to_find_by_invalid_method_syntax
    assert !Topic.respond_to?(:fail_to_find_by_title)
    assert !Topic.respond_to?(:find_by_title?)
    assert !Topic.respond_to?(:fail_to_find_or_create_by_title)
    assert !Topic.respond_to?(:find_or_create_by_title?)
  end

  private

  def ensure_topic_method_is_not_cached(method_id)
    class << Topic; self; end.send(:remove_method, method_id) if Topic.public_methods.any? { |m| m.to_s == method_id.to_s }
  end

end