Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 259 lines (225 sloc) 7.859 kb
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')

class AssociationMatcherTest < Test::Unit::TestCase # :nodoc:

  context "belong_to" do
    setup do
      @matcher = belong_to(:parent)
    end

    should "accept a good association with the default foreign key" do
      define_model :parent
      define_model :child, :parent_id => :integer do
        belongs_to :parent
      end
      assert_accepts @matcher, Child.new
    end

    should "reject a nonexistent association" do
      define_model :child
      assert_rejects @matcher, Child.new
    end

    should "reject an association of the wrong type" do
      define_model :parent, :child_id => :integer
      child_class = define_model :child do
        has_one :parent
      end
      assert_rejects @matcher, Child.new
    end

    should "reject an association that has a nonexistent foreign key" do
      define_model :parent
      define_model :child do
        belongs_to :parent
      end
      assert_rejects @matcher, Child.new
    end

    should "accept an association with an existing custom foreign key" do
      define_model :parent
      define_model :child, :guardian_id => :integer do
        belongs_to :parent, :foreign_key => 'guardian_id'
      end
      assert_accepts @matcher, Child.new
    end

    should "accept a polymorphic association" do
      define_model :child, :parent_type => :string,
                                :parent_id => :integer do
        belongs_to :parent, :polymorphic => true
      end
      assert_accepts @matcher, Child.new
    end

    should "accept an association with a valid :dependent option" do
      define_model :parent
      define_model :child, :parent_id => :integer do
        belongs_to :parent, :dependent => :destroy
      end
      assert_accepts @matcher.dependent(:destroy), Child.new
    end

    should "reject an association with a bad :dependent option" do
      define_model :parent
      define_model :child, :parent_id => :integer do
        belongs_to :parent
      end
      assert_rejects @matcher.dependent(:destroy), Child.new
    end
  end

  context "have_many" do
    setup do
      @matcher = have_many(:children)
    end

    should "accept a valid association without any options" do
      define_model :child, :parent_id => :integer
      define_model :parent do
        has_many :children
      end
      assert_accepts @matcher, Parent.new
    end

    should "accept a valid association with a :through option" do
      define_model :child
      define_model :conception, :child_id => :integer,
                                     :parent_id => :integer do
        belongs_to :child
      end
      define_model :parent do
        has_many :conceptions
        has_many :children, :through => :conceptions
      end
      assert_accepts @matcher, Parent.new
    end

    should "accept a valid association with an :as option" do
      define_model :child, :guardian_type => :string,
                                :guardian_id => :integer
      define_model :parent do
        has_many :children, :as => :guardian
      end
      assert_accepts @matcher, Parent.new
    end

    should "reject an association that has a nonexistent foreign key" do
      define_model :child
      define_model :parent do
        has_many :children
      end
      assert_rejects @matcher, Parent.new
    end

    should "reject an association with a bad :as option" do
      define_model :child, :caretaker_type => :string,
                                :caretaker_id => :integer
      define_model :parent do
        has_many :children, :as => :guardian
      end
      assert_rejects @matcher, Parent.new
    end

    should "reject an association that has a bad :through option" do
      define_model :child, :parent_id => :integer
      define_model :parent do
        has_many :children
      end
      assert_rejects @matcher.through(:conceptions), Parent.new
    end

    should "reject an association that has the wrong :through option" do
      define_model :child
      define_model :conception, :child_id => :integer,
                                     :parent_id => :integer do
        belongs_to :child
      end
      define_model :parent do
        has_many :conceptions
        has_many :children, :through => :conceptions
      end
      assert_rejects @matcher.through(:relationships), Parent.new
    end

    should "accept an association with a valid :dependent option" do
      define_model :child, :parent_id => :integer
      define_model :parent do
        has_many :children, :dependent => :destroy
      end
      assert_accepts @matcher.dependent(:destroy), Parent.new
    end

    should "reject an association with a bad :dependent option" do
      define_model :child, :parent_id => :integer
      define_model :parent do
        has_many :children
      end
      assert_rejects @matcher.dependent(:destroy), Parent.new
    end
  end

  context "have_one" do
    setup do
      @matcher = have_one(:profile)
    end

    should "accept a valid association without any options" do
      define_model :profile, :person_id => :integer
      define_model :person do
        has_one :profile
      end
      assert_accepts @matcher, Person.new
    end

    should "accept a valid association with an :as option" do
      define_model :profile, :profilable_id => :integer,
                                  :profilable_type => :string
      define_model :person do
        has_one :profile, :as => :profilable
      end
      assert_accepts @matcher, Person.new
    end

    should "reject an association that has a nonexistent foreign key" do
      define_model :profile
      define_model :person do
        has_one :profile
      end
      assert_rejects @matcher, Person.new
    end

    should "reject an association with a bad :as option" do
      define_model :profile, :profilable_id => :integer,
                                  :profilable_type => :string
      define_model :person do
        has_one :profile, :as => :describable
      end
      assert_rejects @matcher, Person.new
    end

    should "accept an association with a valid :dependent option" do
      define_model :profile, :person_id => :integer
      define_model :person do
        has_one :profile, :dependent => :destroy
      end
      assert_accepts @matcher.dependent(:destroy), Person.new
    end

    should "reject an association with a bad :dependent option" do
      define_model :profile, :person_id => :integer
      define_model :person do
        has_one :profile
      end
      assert_rejects @matcher.dependent(:destroy), Person.new
    end
  end

  context "have_and_belong_to_many" do
    setup do
      @matcher = have_and_belong_to_many(:relatives)
    end

    should "accept a valid association" do
      define_model :relatives
      define_model :person do
        has_and_belongs_to_many :relatives
      end
      define_model :people_relative, :person_id => :integer,
                                          :relative_id => :integer
      assert_accepts @matcher, Person.new
    end

    should "reject a nonexistent association" do
      define_model :relatives
      define_model :person
      define_model :people_relative, :person_id => :integer,
                                          :relative_id => :integer
      assert_rejects @matcher, Person.new
    end

    should "reject an association with a nonexistent join table" do
      define_model :relatives
      define_model :person do
        has_and_belongs_to_many :relatives
      end
      assert_rejects @matcher, Person.new
    end

    should "reject an association of the wrong type" do
      define_model :relatives, :person_id => :integer
      define_model :person do
        has_many :relatives
      end
      assert_rejects @matcher, Person.new
    end
  end

end
Something went wrong with that request. Please try again.