primary_keys_test.rb 13.5 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
    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
    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
    assert_nil keyboard.id
66
    assert_nil keyboard.key_number
67 68 69 70
  end

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

76 77 78 79 80 81 82
  def test_update_with_non_primary_key_id_column
    subscriber = Subscriber.first
    subscriber.update(update_count: 1)
    subscriber.reload
    assert_equal 1, subscriber.update_count
  end

D
Initial  
David Heinemeier Hansson 已提交
83
  def test_string_key
84 85 86 87
    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 已提交
88 89 90 91 92

    subscriber = Subscriber.new
    subscriber.id = "jdoe"
    assert_equal("jdoe", subscriber.id)
    subscriber.name = "John Doe"
93
    subscriber.save!
94
    assert_equal("jdoe", subscriber.id)
D
Initial  
David Heinemeier Hansson 已提交
95 96 97 98 99

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

100 101 102 103 104 105
  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 已提交
106
  def test_find_with_more_than_one_string_key
107
    assert_equal 2, Subscriber.find(subscribers(:first).nick, subscribers(:second).nick).length
D
Initial  
David Heinemeier Hansson 已提交
108
  end
J
Jeremy Kemper 已提交
109

D
Initial  
David Heinemeier Hansson 已提交
110
  def test_primary_key_prefix
111
    old_primary_key_prefix_type = ActiveRecord::Base.primary_key_prefix_type
D
Initial  
David Heinemeier Hansson 已提交
112
    ActiveRecord::Base.primary_key_prefix_type = :table_name
113
    Topic.reset_primary_key
D
Initial  
David Heinemeier Hansson 已提交
114 115 116
    assert_equal "topicid", Topic.primary_key

    ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
117
    Topic.reset_primary_key
D
Initial  
David Heinemeier Hansson 已提交
118 119 120
    assert_equal "topic_id", Topic.primary_key

    ActiveRecord::Base.primary_key_prefix_type = nil
121
    Topic.reset_primary_key
D
Initial  
David Heinemeier Hansson 已提交
122
    assert_equal "id", Topic.primary_key
123 124
  ensure
    ActiveRecord::Base.primary_key_prefix_type = old_primary_key_prefix_type
D
Initial  
David Heinemeier Hansson 已提交
125
  end
J
Jeremy Kemper 已提交
126

127 128 129
  def test_delete_should_quote_pkey
    assert_nothing_raised { MixedCaseMonkey.delete(1) }
  end
130

131
  def test_update_counters_should_quote_pkey_and_quote_counter_columns
132
    assert_nothing_raised { MixedCaseMonkey.update_counters(1, fleaCount: 99) }
133
  end
134

135 136 137
  def test_find_with_one_id_should_quote_pkey
    assert_nothing_raised { MixedCaseMonkey.find(1) }
  end
138

139
  def test_find_with_multiple_ids_should_quote_pkey
140
    assert_nothing_raised { MixedCaseMonkey.find([1, 2]) }
141
  end
142

143 144 145
  def test_instance_update_should_quote_pkey
    assert_nothing_raised { MixedCaseMonkey.find(1).save }
  end
146

147
  def test_instance_destroy_should_quote_pkey
148 149
    assert_nothing_raised { MixedCaseMonkey.find(1).destroy }
  end
150

151 152 153
  def test_deprecate_supports_primary_key
    assert_deprecated { ActiveRecord::Base.connection.supports_primary_key? }
  end
154

155 156 157
  def test_primary_key_returns_value_if_it_exists
    klass = Class.new(ActiveRecord::Base) do
      self.table_name = "developers"
158 159
    end

160 161
    assert_equal "id", klass.primary_key
  end
162

163 164 165
  def test_primary_key_returns_nil_if_it_does_not_exist
    klass = Class.new(ActiveRecord::Base) do
      self.table_name = "developers_projects"
166
    end
167 168

    assert_nil klass.primary_key
169
  end
170 171

  def test_quoted_primary_key_after_set_primary_key
172
    k = Class.new(ActiveRecord::Base)
173 174 175 176
    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 已提交
177

178 179 180 181
  def test_auto_detect_primary_key_from_schema
    MixedCaseMonkey.reset_primary_key
    assert_equal "monkeyID", MixedCaseMonkey.primary_key
  end
182 183

  def test_primary_key_update_with_custom_key_name
184 185
    dashboard = Dashboard.create!(dashboard_id: "1")
    dashboard.id = "2"
186 187 188
    dashboard.save!

    dashboard = Dashboard.first
189
    assert_equal "2", dashboard.id
190
  end
191

192
  def test_create_without_primary_key_no_extra_query
193 194
    skip if current_adapter?(:OracleAdapter)

195
    klass = Class.new(ActiveRecord::Base) do
196
      self.table_name = "dashboards"
197 198 199 200 201
    end
    klass.create! # warmup schema cache
    assert_queries(3, ignore_none: true) { klass.create! }
  end

202 203 204 205 206 207
  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
208 209 210 211 212 213

    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
214
  end
215 216 217
end

class PrimaryKeyWithNoConnectionTest < ActiveRecord::TestCase
218
  self.use_transactional_tests = false
219

220 221 222
  unless in_memory_db?
    def test_set_primary_key_with_no_connection
      connection = ActiveRecord::Base.remove_connection
223

224
      model = Class.new(ActiveRecord::Base)
225
      model.primary_key = "foo"
226

227
      assert_equal "foo", model.primary_key
228

229
      ActiveRecord::Base.establish_connection(connection)
230

231
      assert_equal "foo", model.primary_key
232
    end
233
  end
D
Initial  
David Heinemeier Hansson 已提交
234
end
235

236 237 238 239 240 241 242 243 244 245 246 247 248 249
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

250 251
  def test_primary_key_with_integer
    @connection.create_table(:auto_increments, id: :integer, force: true)
252 253
    assert_auto_incremented
  end
254

255 256
  def test_primary_key_with_bigint
    @connection.create_table(:auto_increments, id: :bigint, force: true)
257
    assert_auto_incremented
258
  end
259 260 261 262 263 264 265 266 267 268 269 270

  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
271 272
end

R
Ryuta Kamizono 已提交
273
class PrimaryKeyAnyTypeTest < ActiveRecord::TestCase
274 275
  include SchemaDumpingHelper

276
  self.use_transactional_tests = false
R
Ryuta Kamizono 已提交
277 278 279 280 281 282 283 284 285 286

  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
287
    @connection.drop_table(:barcodes, if_exists: true)
R
Ryuta Kamizono 已提交
288 289 290 291 292
  end

  def test_any_type_primary_key
    assert_equal "code", Barcode.primary_key

293
    column = Barcode.column_for_attribute(Barcode.primary_key)
294
    assert_not column.null
295 296
    assert_equal :string, column.type
    assert_equal 42, column.limit
R
Ryuta Kamizono 已提交
297
  end
298 299 300 301 302

  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
303 304 305 306 307 308 309 310

  if current_adapter?(:Mysql2Adapter) && subsecond_precision_supported?
    test "schema typed primary key column" do
      @connection.create_table(:scheduled_logs, id: :timestamp, precision: 6, force: true)
      schema = dump_table_schema("scheduled_logs")
      assert_match %r/create_table "scheduled_logs", id: :timestamp, precision: 6/, schema
    end
  end
R
Ryuta Kamizono 已提交
311 312
end

313 314 315 316 317 318 319
class CompositePrimaryKeyTest < ActiveRecord::TestCase
  include SchemaDumpingHelper

  self.use_transactional_tests = false

  def setup
    @connection = ActiveRecord::Base.connection
320
    @connection.schema_cache.clear!
321 322 323 324
    @connection.create_table(:barcodes, primary_key: ["region", "code"], force: true) do |t|
      t.string :region
      t.integer :code
    end
325 326 327 328
    @connection.create_table(:barcodes_reverse, primary_key: ["code", "region"], force: true) do |t|
      t.string :region
      t.integer :code
    end
329 330 331 332 333 334 335 336 337 338
  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

339 340
  def test_composite_primary_key_out_of_order
    skip if current_adapter?(:SQLite3Adapter)
341
    assert_equal ["code", "region"], @connection.primary_keys("barcodes_reverse")
342 343
  end

344
  def test_primary_key_issues_warning
345 346 347 348 349
    model = Class.new(ActiveRecord::Base) do
      def self.table_name
        "barcodes"
      end
    end
350
    warning = capture(:stderr) do
351
      assert_nil model.primary_key
352
    end
353
    assert_match(/WARNING: Active Record does not support composite primary key\./, warning)
354 355
  end

356
  def test_dumping_composite_primary_key
357 358 359
    schema = dump_table_schema "barcodes"
    assert_match %r{create_table "barcodes", primary_key: \["region", "code"\]}, schema
  end
360 361 362 363 364 365

  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
366 367
end

368 369
class PrimaryKeyIntegerNilDefaultTest < ActiveRecord::TestCase
  include SchemaDumpingHelper
370

371
  self.use_transactional_tests = false
372

373 374 375
  def setup
    @connection = ActiveRecord::Base.connection
  end
376

377 378 379
  def teardown
    @connection.drop_table :int_defaults, if_exists: true
  end
380

381 382 383 384 385 386
  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
387

388 389 390 391
  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
392
  end
393
end
394

395 396 397
if current_adapter?(:PostgreSQLAdapter, :Mysql2Adapter)
  class PrimaryKeyIntegerTest < ActiveRecord::TestCase
    include SchemaDumpingHelper
398

399
    self.use_transactional_tests = false
400

401 402
    class Widget < ActiveRecord::Base
    end
403

404 405
    setup do
      @connection = ActiveRecord::Base.connection
406
      @pk_type = current_adapter?(:PostgreSQLAdapter) ? :serial : :integer
407
    end
408

409 410 411
    teardown do
      @connection.drop_table :widgets, if_exists: true
    end
412

413
    test "primary key column type with serial/integer" do
414
      @connection.create_table(:widgets, id: @pk_type, force: true)
415 416 417
      column = @connection.columns(:widgets).find { |c| c.name == "id" }
      assert_equal :integer, column.type
      assert_not column.bigint?
418 419
    end

420
    test "primary key with serial/integer are automatically numbered" do
421
      @connection.create_table(:widgets, id: @pk_type, force: true)
422 423 424
      widget = Widget.create!
      assert_not_nil widget.id
    end
425

426
    test "schema dump primary key with serial/integer" do
427
      @connection.create_table(:widgets, id: @pk_type, force: true)
428
      schema = dump_table_schema "widgets"
429
      assert_match %r{create_table "widgets", id: :#{@pk_type}, force: :cascade}, schema
430
    end
431

A
Abdelkader Boudih 已提交
432
    if current_adapter?(:Mysql2Adapter)
433 434 435 436 437
      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
438
        assert_not column.bigint?
439 440 441 442 443
        assert column.unsigned?

        schema = dump_table_schema "widgets"
        assert_match %r{create_table "widgets", id: :integer, unsigned: true, force: :cascade}, schema
      end
444 445 446 447 448 449 450 451 452 453 454 455

      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
456
    end
457 458
  end
end