Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 03d0fd587a
Fetching contributors…

Cannot retrieve contributors at this time

file 314 lines (273 sloc) 8.591 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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
require 'helper'

class ImmigrantTest < ActiveSupport::TestCase
  include TestMethods

  class MockModel < ActiveRecord::Base
    self.abstract_class = true
    class << self
      def primary_key
        connection.primary_key(table_name)
      end
      def connection
        @connection ||= MockConnection.new
      end
    end
  end
  
  class MockConnection
    def supports_primary_key? # AR <3.2
      true
    end
    def primary_key(table)
      table !~ /s_.*s\z/ ? 'id' : nil
    end
  end

  def teardown
    subclasses = ActiveSupport::DescendantsTracker.direct_descendants(MockModel)
    subclasses.each do |subclass|
      ImmigrantTest.send(:remove_const, subclass.to_s.sub(/.*::/, ''))
    end
    subclasses.replace([])
  end


  # basic scenarios
  
  test 'belongs_to should generate a foreign key' do
    class Author < MockModel; end
    class Book < MockModel
      belongs_to :guy, :class_name => 'Author', :foreign_key => 'author_id'
    end

    assert_equal(
      [foreign_key_definition(
         'books', 'authors',
         :column => 'author_id', :primary_key => 'id', :dependent => nil
       )],
      Immigrant.infer_keys([], [Author, Book]).first
    )
  end

  test 'has_one should generate a foreign key' do
    class Author < MockModel
      has_one :piece_de_resistance, :class_name => 'Book', :order => "id DESC"
    end
    class Book < MockModel; end

    assert_equal(
      [foreign_key_definition(
         'books', 'authors',
         :column => 'author_id', :primary_key => 'id', :dependent => nil
       )],
      Immigrant.infer_keys([], [Author, Book]).first
    )
  end

  test 'has_one :dependent => :delete should generate a foreign key with :dependent => :delete' do
    class Author < MockModel
      has_one :book, :order => "id DESC", :dependent => :delete
    end
    class Book < MockModel; end

    assert_equal(
      [foreign_key_definition(
         'books', 'authors',
         :column => 'author_id', :primary_key => 'id', :dependent => :delete
       )],
      Immigrant.infer_keys([], [Author, Book]).first
    )
  end

  test 'has_many should generate a foreign key' do
    class Author < MockModel
      has_many :babies, :class_name => 'Book'
    end
    class Book < MockModel; end

    assert_equal(
      [foreign_key_definition(
         'books', 'authors',
         :column => 'author_id', :primary_key => 'id', :dependent => nil
       )],
      Immigrant.infer_keys([], [Author, Book]).first
    )
  end

  test 'has_many :dependent => :delete_all should generate a foreign key with :dependent => :delete' do
    class Author < MockModel
      has_many :books, :dependent => :delete_all
    end
    class Book < MockModel; end

    assert_equal(
      [foreign_key_definition(
         'books', 'authors',
         :column => 'author_id', :primary_key => 'id', :dependent => :delete
       )],
      Immigrant.infer_keys([], [Author, Book]).first
    )
  end

  test 'has_and_belongs_to_many should generate two foreign keys' do
    class Author < MockModel
      has_and_belongs_to_many :fans
    end
    class Fan < MockModel; end

    assert_equal(
      [foreign_key_definition(
         'authors_fans', 'authors',
         :column => 'author_id', :primary_key => 'id', :dependent => nil
       ),
       foreign_key_definition(
         'authors_fans', 'fans',
         :column => 'fan_id', :primary_key => 'id', :dependent => nil
       )],
      Immigrant.infer_keys([], [Author, Fan]).first
    )
  end

  test 'conditional has_one/has_many associations should ignore :dependent' do
    class Author < MockModel
      has_many :articles, :conditions => "published", :dependent => :delete_all
      has_one :favorite_book, :class_name => 'Book',
              :conditions => "most_awesome", :dependent => :delete
    end
    class Book < MockModel; end
    class Article < MockModel; end

    assert_equal(
      [foreign_key_definition(
         'articles', 'authors',
         :column => 'author_id', :primary_key => 'id', :dependent => nil
       ),
       foreign_key_definition(
         'books', 'authors',
         :column => 'author_id', :primary_key => 'id', :dependent => nil
       )],
      Immigrant.infer_keys([], [Article, Author, Book]).first
    )
  end


  # (no) duplication
  
  test 'STI should not generate duplicate foreign keys' do
    class Company < MockModel; end
    class Employee < MockModel
      belongs_to :company
    end
    class Manager < Employee; end

    assert(Manager.reflections.present?)
    assert_equal(
      [foreign_key_definition(
         'employees', 'companies',
         :column => 'company_id', :primary_key => 'id', :dependent => nil
       )],
      Immigrant.infer_keys([], [Company, Employee, Manager]).first
    )
  end

  test 'complementary associations should not generate duplicate foreign keys' do
    class Author < MockModel
      has_many :books
    end
    class Book < MockModel
      belongs_to :author
    end

    assert_equal(
      [foreign_key_definition(
         'books', 'authors',
         :column => 'author_id', :primary_key => 'id', :dependent => nil
       )],
      Immigrant.infer_keys([], [Author, Book]).first
    )
  end

  test 'redundant associations should not generate duplicate foreign keys' do
    class Author < MockModel
      has_many :books
      has_many :favorite_books, :class_name => 'Book', :conditions => "awesome"
      has_many :bad_books, :class_name => 'Book', :conditions => "amateur_hour"
    end
    class Book < MockModel; end

    assert_equal(
      [foreign_key_definition(
         'books', 'authors',
         :column => 'author_id', :primary_key => 'id', :dependent => nil
       )],
      Immigrant.infer_keys([], [Author, Book]).first
    )
  end


  # skipped associations

  test 'associations should not generate foreign keys if they already exist, even if :dependent/name are different' do
    database_keys = [
      foreign_key_definition(
        'articles', 'authors',
        :column => 'author_id', :primary_key => 'id', :dependent => nil,
        :name => "doesn't_matter"
      ),
      foreign_key_definition(
        'books', 'authors', :column => 'author_id', :primary_key => 'id',
        :dependent => :delete
      )
    ]

    class Author < MockModel
      has_many :articles
      has_one :favorite_book, :class_name => 'Book',
              :conditions => "most_awesome"
    end
    class Book < MockModel; end
    class Article < MockModel; end

    assert_equal(
      [],
      Immigrant.infer_keys(database_keys, [Article, Author, Book]).first
    )
  end

  test 'finder_sql associations should not generate foreign keys' do
    class Author < MockModel
      has_many :books, :finder_sql => <<-SQL
SELECT *
FROM books
WHERE author_id = \#{id}
ORDER BY RANDOM() LIMIT 5'
SQL
    end
    class Book < MockModel; end

    assert_equal(
      [],
      Immigrant.infer_keys([], [Author, Book]).first
    )
  end

  test 'polymorphic associations should not generate foreign keys' do
    class Property < MockModel
      belongs_to :owner, :polymorphic => true
    end
    class Person < MockModel
      has_many :properties, :as => :owner
    end
    class Corporation < MockModel
      has_many :properties, :as => :owner
    end

    assert_equal(
      [],
      Immigrant.infer_keys([], [Corporation, Person, Property]).first
    )
  end

  test 'has_many :through should not generate foreign keys' do
    class Author < MockModel
      has_many :authors_fans
      has_many :fans, :through => :authors_fans
    end
    class AuthorsFan < MockModel
      belongs_to :author
      belongs_to :fan
    end
    class Fan < MockModel
      has_many :authors_fans
      has_many :authors, :through => :authors_fans
    end

    assert_equal(
      [foreign_key_definition(
         'authors_fans', 'authors',
         :column => 'author_id', :primary_key => 'id', :dependent => nil
       ),
       foreign_key_definition(
         'authors_fans', 'fans',
         :column => 'fan_id', :primary_key => 'id', :dependent => nil
       )],
      Immigrant.infer_keys([], [Author, AuthorsFan, Fan]).first
    )
  end

  test 'broken associations should not cause errors' do
    class Author < MockModel; end
    class Book < MockModel
      belongs_to :author
      belongs_to :invalid
    end

    assert_equal(
      [foreign_key_definition(
         'books', 'authors',
         :column => 'author_id', :primary_key => 'id', :dependent => nil
       )],
      Immigrant.infer_keys([], [Author, Book]).first
    )
  end
end
Something went wrong with that request. Please try again.