primary_keys_test.rb 13.1 KB
Newer Older
1
require "cases/helper"
2 3 4 5 6 7 8 9
require "support/schema_dumping_helper"
require "models/topic"
require "models/reply"
require "models/subscriber"
require "models/movie"
require "models/keyboard"
require "models/mixed_case_monkey"
require "models/dashboard"
10
require "models/non_primary_key"
D
Initial  
David Heinemeier Hansson 已提交
11

12
class PrimaryKeysTest < ActiveRecord::TestCase
13
  fixtures :topics, :subscribers, :movies, :mixed_case_monkeys
D
Initial  
David Heinemeier Hansson 已提交
14

15
  def test_to_key_with_default_primary_key
16
    topic = Topic.new
17
    assert_nil topic.to_key
18
    topic = Topic.find(1)
19
    assert_equal [1], topic.to_key
20 21 22 23
  end

  def test_to_key_with_customized_primary_key
    keyboard = Keyboard.new
24
    assert_nil keyboard.to_key
25 26
    keyboard.save
    assert_equal keyboard.to_key, [keyboard.id]
27 28
  end

29 30 31 32 33
  def test_read_attribute_with_custom_primary_key
    keyboard = Keyboard.create!
    assert_equal keyboard.key_number, keyboard.read_attribute(:id)
  end

34 35 36
  def test_to_key_with_primary_key_after_destroy
    topic = Topic.find(1)
    topic.destroy
37
    assert_equal [1], topic.to_key
38 39
  end

D
Initial  
David Heinemeier Hansson 已提交
40 41
  def test_integer_key
    topic = Topic.find(1)
42
    assert_equal(topics(:first).author_name, topic.author_name)
D
Initial  
David Heinemeier Hansson 已提交
43
    topic = Topic.find(2)
44
    assert_equal(topics(:second).author_name, topic.author_name)
D
Initial  
David Heinemeier Hansson 已提交
45 46 47

    topic = Topic.new
    topic.title = "New Topic"
48
    assert_nil topic.id
49
    assert_nothing_raised { topic.save! }
D
Initial  
David Heinemeier Hansson 已提交
50 51 52 53 54 55
    id = topic.id

    topicReloaded = Topic.find(id)
    assert_equal("New Topic", topicReloaded.title)
  end

56
  def test_customized_primary_key_auto_assigns_on_save
57
    Keyboard.delete_all
58
    keyboard = Keyboard.new(name: "HHKB")
59
    assert_nothing_raised { keyboard.save! }
60
    assert_equal keyboard.id, Keyboard.find_by_name("HHKB").id
61 62
  end

63
  def test_customized_primary_key_can_be_get_before_saving
64
    keyboard = Keyboard.new
65 66
    assert_nil keyboard.id
    assert_nothing_raised { assert_nil keyboard.key_number }
67 68 69 70
  end

  def test_customized_string_primary_key_settable_before_save
    subscriber = Subscriber.new
71 72 73
    assert_nothing_raised { subscriber.id = "webster123" }
    assert_equal "webster123", subscriber.id
    assert_equal "webster123", subscriber.nick
74 75
  end

D
Initial  
David Heinemeier Hansson 已提交
76
  def test_string_key
77 78 79 80
    subscriber = Subscriber.find(subscribers(:first).nick)
    assert_equal(subscribers(:first).name, subscriber.name)
    subscriber = Subscriber.find(subscribers(:second).nick)
    assert_equal(subscribers(:second).name, subscriber.name)
D
Initial  
David Heinemeier Hansson 已提交
81 82 83 84 85

    subscriber = Subscriber.new
    subscriber.id = "jdoe"
    assert_equal("jdoe", subscriber.id)
    subscriber.name = "John Doe"
86
    assert_nothing_raised { subscriber.save! }
87
    assert_equal("jdoe", subscriber.id)
D
Initial  
David Heinemeier Hansson 已提交
88 89 90 91 92

    subscriberReloaded = Subscriber.find("jdoe")
    assert_equal("John Doe", subscriberReloaded.name)
  end

93 94 95 96 97 98
  def test_id_column_that_is_not_primary_key
    NonPrimaryKey.create!(id: 100)
    actual = NonPrimaryKey.find_by(id: 100)
    assert_match %r{<NonPrimaryKey id: 100}, actual.inspect
  end

D
Initial  
David Heinemeier Hansson 已提交
99
  def test_find_with_more_than_one_string_key
100
    assert_equal 2, Subscriber.find(subscribers(:first).nick, subscribers(:second).nick).length
D
Initial  
David Heinemeier Hansson 已提交
101
  end
J
Jeremy Kemper 已提交
102

D
Initial  
David Heinemeier Hansson 已提交
103
  def test_primary_key_prefix
104
    old_primary_key_prefix_type = ActiveRecord::Base.primary_key_prefix_type
D
Initial  
David Heinemeier Hansson 已提交
105
    ActiveRecord::Base.primary_key_prefix_type = :table_name
106
    Topic.reset_primary_key
D
Initial  
David Heinemeier Hansson 已提交
107 108 109
    assert_equal "topicid", Topic.primary_key

    ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
110
    Topic.reset_primary_key
D
Initial  
David Heinemeier Hansson 已提交
111 112 113
    assert_equal "topic_id", Topic.primary_key

    ActiveRecord::Base.primary_key_prefix_type = nil
114
    Topic.reset_primary_key
D
Initial  
David Heinemeier Hansson 已提交
115
    assert_equal "id", Topic.primary_key
116 117
  ensure
    ActiveRecord::Base.primary_key_prefix_type = old_primary_key_prefix_type
D
Initial  
David Heinemeier Hansson 已提交
118
  end
J
Jeremy Kemper 已提交
119

120 121 122
  def test_delete_should_quote_pkey
    assert_nothing_raised { MixedCaseMonkey.delete(1) }
  end
123

124
  def test_update_counters_should_quote_pkey_and_quote_counter_columns
125
    assert_nothing_raised { MixedCaseMonkey.update_counters(1, fleaCount: 99) }
126
  end
127

128 129 130
  def test_find_with_one_id_should_quote_pkey
    assert_nothing_raised { MixedCaseMonkey.find(1) }
  end
131

132
  def test_find_with_multiple_ids_should_quote_pkey
133
    assert_nothing_raised { MixedCaseMonkey.find([1, 2]) }
134
  end
135

136 137 138
  def test_instance_update_should_quote_pkey
    assert_nothing_raised { MixedCaseMonkey.find(1).save }
  end
139

140
  def test_instance_destroy_should_quote_pkey
141 142
    assert_nothing_raised { MixedCaseMonkey.find(1).destroy }
  end
143

144 145 146
  def test_deprecate_supports_primary_key
    assert_deprecated { ActiveRecord::Base.connection.supports_primary_key? }
  end
147

148 149 150
  def test_primary_key_returns_value_if_it_exists
    klass = Class.new(ActiveRecord::Base) do
      self.table_name = "developers"
151 152
    end

153 154
    assert_equal "id", klass.primary_key
  end
155

156 157 158
  def test_primary_key_returns_nil_if_it_does_not_exist
    klass = Class.new(ActiveRecord::Base) do
      self.table_name = "developers_projects"
159
    end
160 161

    assert_nil klass.primary_key
162
  end
163 164

  def test_quoted_primary_key_after_set_primary_key
165
    k = Class.new(ActiveRecord::Base)
166 167 168 169
    assert_equal k.connection.quote_column_name("id"), k.quoted_primary_key
    k.primary_key = "foo"
    assert_equal k.connection.quote_column_name("foo"), k.quoted_primary_key
  end
J
Jon Leighton 已提交
170

171 172 173 174
  def test_auto_detect_primary_key_from_schema
    MixedCaseMonkey.reset_primary_key
    assert_equal "monkeyID", MixedCaseMonkey.primary_key
  end
175 176

  def test_primary_key_update_with_custom_key_name
177 178
    dashboard = Dashboard.create!(dashboard_id: "1")
    dashboard.id = "2"
179 180 181
    dashboard.save!

    dashboard = Dashboard.first
182
    assert_equal "2", dashboard.id
183
  end
184

185 186
  def test_create_without_primary_key_no_extra_query
    klass = Class.new(ActiveRecord::Base) do
187
      self.table_name = "dashboards"
188 189 190 191 192
    end
    klass.create! # warmup schema cache
    assert_queries(3, ignore_none: true) { klass.create! }
  end

193 194 195 196 197 198
  if current_adapter?(:PostgreSQLAdapter)
    def test_serial_with_quoted_sequence_name
      column = MixedCaseMonkey.columns_hash[MixedCaseMonkey.primary_key]
      assert_equal "nextval('\"mixed_case_monkeys_monkeyID_seq\"'::regclass)", column.default_function
      assert column.serial?
    end
199 200 201 202 203 204

    def test_serial_with_unquoted_sequence_name
      column = Topic.columns_hash[Topic.primary_key]
      assert_equal "nextval('topics_id_seq'::regclass)", column.default_function
      assert column.serial?
    end
205
  end
206 207 208
end

class PrimaryKeyWithNoConnectionTest < ActiveRecord::TestCase
209
  self.use_transactional_tests = false
210

211 212 213
  unless in_memory_db?
    def test_set_primary_key_with_no_connection
      connection = ActiveRecord::Base.remove_connection
214

215
      model = Class.new(ActiveRecord::Base)
216
      model.primary_key = "foo"
217

218
      assert_equal "foo", model.primary_key
219

220
      ActiveRecord::Base.establish_connection(connection)
221

222
      assert_equal "foo", model.primary_key
223
    end
224
  end
D
Initial  
David Heinemeier Hansson 已提交
225
end
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240
class PrimaryKeyWithAutoIncrementTest < ActiveRecord::TestCase
  self.use_transactional_tests = false

  class AutoIncrement < ActiveRecord::Base
  end

  def setup
    @connection = ActiveRecord::Base.connection
  end

  def teardown
    @connection.drop_table(:auto_increments, if_exists: true)
  end

241 242
  def test_primary_key_with_integer
    @connection.create_table(:auto_increments, id: :integer, force: true)
243 244
    assert_auto_incremented
  end
245

246 247
  def test_primary_key_with_bigint
    @connection.create_table(:auto_increments, id: :bigint, force: true)
248
    assert_auto_incremented
249
  end
250 251 252 253 254 255 256 257 258 259 260 261

  private
    def assert_auto_incremented
      record1 = AutoIncrement.create!
      assert_not_nil record1.id

      record1.destroy

      record2 = AutoIncrement.create!
      assert_not_nil record2.id
      assert_operator record2.id, :>, record1.id
    end
262 263
end

R
Ryuta Kamizono 已提交
264
class PrimaryKeyAnyTypeTest < ActiveRecord::TestCase
265 266
  include SchemaDumpingHelper

267
  self.use_transactional_tests = false
R
Ryuta Kamizono 已提交
268 269 270 271 272 273 274 275 276 277

  class Barcode < ActiveRecord::Base
  end

  setup do
    @connection = ActiveRecord::Base.connection
    @connection.create_table(:barcodes, primary_key: "code", id: :string, limit: 42, force: true)
  end

  teardown do
278
    @connection.drop_table(:barcodes, if_exists: true)
R
Ryuta Kamizono 已提交
279 280 281 282 283
  end

  def test_any_type_primary_key
    assert_equal "code", Barcode.primary_key

284
    column = Barcode.column_for_attribute(Barcode.primary_key)
285
    assert_not column.null
286 287
    assert_equal :string, column.type
    assert_equal 42, column.limit
R
Ryuta Kamizono 已提交
288
  end
289 290 291 292 293

  test "schema dump primary key includes type and options" do
    schema = dump_table_schema "barcodes"
    assert_match %r{create_table "barcodes", primary_key: "code", id: :string, limit: 42}, schema
  end
R
Ryuta Kamizono 已提交
294 295
end

296 297 298 299 300 301 302
class CompositePrimaryKeyTest < ActiveRecord::TestCase
  include SchemaDumpingHelper

  self.use_transactional_tests = false

  def setup
    @connection = ActiveRecord::Base.connection
303
    @connection.schema_cache.clear!
304 305 306 307
    @connection.create_table(:barcodes, primary_key: ["region", "code"], force: true) do |t|
      t.string :region
      t.integer :code
    end
308 309 310 311
    @connection.create_table(:barcodes_reverse, primary_key: ["code", "region"], force: true) do |t|
      t.string :region
      t.integer :code
    end
312 313 314 315 316 317 318 319 320 321
  end

  def teardown
    @connection.drop_table(:barcodes, if_exists: true)
  end

  def test_composite_primary_key
    assert_equal ["region", "code"], @connection.primary_keys("barcodes")
  end

322 323
  def test_composite_primary_key_out_of_order
    skip if current_adapter?(:SQLite3Adapter)
324
    assert_equal ["code", "region"], @connection.primary_keys("barcodes_reverse")
325 326
  end

327
  def test_primary_key_issues_warning
328 329 330 331 332
    model = Class.new(ActiveRecord::Base) do
      def self.table_name
        "barcodes"
      end
    end
333
    warning = capture(:stderr) do
334
      assert_nil model.primary_key
335
    end
336
    assert_match(/WARNING: Active Record does not support composite primary key\./, warning)
337 338
  end

339
  def test_dumping_composite_primary_key
340 341 342
    schema = dump_table_schema "barcodes"
    assert_match %r{create_table "barcodes", primary_key: \["region", "code"\]}, schema
  end
343 344 345 346 347 348

  def test_dumping_composite_primary_key_out_of_order
    skip if current_adapter?(:SQLite3Adapter)
    schema = dump_table_schema "barcodes_reverse"
    assert_match %r{create_table "barcodes_reverse", primary_key: \["code", "region"\]}, schema
  end
349 350
end

351 352
class PrimaryKeyIntegerNilDefaultTest < ActiveRecord::TestCase
  include SchemaDumpingHelper
353

354
  self.use_transactional_tests = false
355

356 357 358
  def setup
    @connection = ActiveRecord::Base.connection
  end
359

360 361 362
  def teardown
    @connection.drop_table :int_defaults, if_exists: true
  end
363

364 365 366 367 368 369
  def test_schema_dump_primary_key_integer_with_default_nil
    skip if current_adapter?(:SQLite3Adapter)
    @connection.create_table(:int_defaults, id: :integer, default: nil, force: true)
    schema = dump_table_schema "int_defaults"
    assert_match %r{create_table "int_defaults", id: :integer, default: nil}, schema
  end
370

371 372 373 374
  def test_schema_dump_primary_key_bigint_with_default_nil
    @connection.create_table(:int_defaults, id: :bigint, default: nil, force: true)
    schema = dump_table_schema "int_defaults"
    assert_match %r{create_table "int_defaults", id: :bigint, default: nil}, schema
375
  end
376
end
377

378 379 380
if current_adapter?(:PostgreSQLAdapter, :Mysql2Adapter)
  class PrimaryKeyIntegerTest < ActiveRecord::TestCase
    include SchemaDumpingHelper
381

382
    self.use_transactional_tests = false
383

384 385
    class Widget < ActiveRecord::Base
    end
386

387 388
    setup do
      @connection = ActiveRecord::Base.connection
389
      @pk_type = current_adapter?(:PostgreSQLAdapter) ? :serial : :integer
390
    end
391

392 393 394
    teardown do
      @connection.drop_table :widgets, if_exists: true
    end
395

396
    test "primary key column type with serial/integer" do
397
      @connection.create_table(:widgets, id: @pk_type, force: true)
398 399 400
      column = @connection.columns(:widgets).find { |c| c.name == "id" }
      assert_equal :integer, column.type
      assert_not column.bigint?
401 402
    end

403
    test "primary key with serial/integer are automatically numbered" do
404
      @connection.create_table(:widgets, id: @pk_type, force: true)
405 406 407
      widget = Widget.create!
      assert_not_nil widget.id
    end
408

409
    test "schema dump primary key with serial/integer" do
410
      @connection.create_table(:widgets, id: @pk_type, force: true)
411
      schema = dump_table_schema "widgets"
412
      assert_match %r{create_table "widgets", id: :#{@pk_type}, force: :cascade}, schema
413
    end
414

A
Abdelkader Boudih 已提交
415
    if current_adapter?(:Mysql2Adapter)
416 417 418 419 420
      test "primary key column type with options" do
        @connection.create_table(:widgets, id: :primary_key, limit: 4, unsigned: true, force: true)
        column = @connection.columns(:widgets).find { |c| c.name == "id" }
        assert column.auto_increment?
        assert_equal :integer, column.type
421
        assert_not column.bigint?
422 423 424 425 426
        assert column.unsigned?

        schema = dump_table_schema "widgets"
        assert_match %r{create_table "widgets", id: :integer, unsigned: true, force: :cascade}, schema
      end
427 428 429 430 431 432 433 434 435 436 437 438

      test "bigint primary key with unsigned" do
        @connection.create_table(:widgets, id: :bigint, unsigned: true, force: true)
        column = @connection.columns(:widgets).find { |c| c.name == "id" }
        assert column.auto_increment?
        assert_equal :integer, column.type
        assert column.bigint?
        assert column.unsigned?

        schema = dump_table_schema "widgets"
        assert_match %r{create_table "widgets", id: :bigint, unsigned: true, force: :cascade}, schema
      end
439
    end
440 441
  end
end