primary_keys_test.rb 12.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 123
  def test_delete_should_quote_pkey
    assert_nothing_raised { MixedCaseMonkey.delete(1) }
  end
  def test_update_counters_should_quote_pkey_and_quote_counter_columns
124
    assert_nothing_raised { MixedCaseMonkey.update_counters(1, fleaCount: 99) }
125 126 127 128 129
  end
  def test_find_with_one_id_should_quote_pkey
    assert_nothing_raised { MixedCaseMonkey.find(1) }
  end
  def test_find_with_multiple_ids_should_quote_pkey
130
    assert_nothing_raised { MixedCaseMonkey.find([1, 2]) }
131 132 133 134
  end
  def test_instance_update_should_quote_pkey
    assert_nothing_raised { MixedCaseMonkey.find(1).save }
  end
135
  def test_instance_destroy_should_quote_pkey
136 137
    assert_nothing_raised { MixedCaseMonkey.find(1).destroy }
  end
138

139 140 141
  if ActiveRecord::Base.connection.supports_primary_key?
    def test_primary_key_returns_value_if_it_exists
      klass = Class.new(ActiveRecord::Base) do
142
        self.table_name = "developers"
143
      end
144

145
      assert_equal "id", klass.primary_key
146 147
    end

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

      assert_nil klass.primary_key
154 155
    end
  end
156 157

  def test_quoted_primary_key_after_set_primary_key
158
    k = Class.new(ActiveRecord::Base)
159 160 161 162
    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 已提交
163

164 165 166 167
  def test_auto_detect_primary_key_from_schema
    MixedCaseMonkey.reset_primary_key
    assert_equal "monkeyID", MixedCaseMonkey.primary_key
  end
168 169

  def test_primary_key_update_with_custom_key_name
170 171
    dashboard = Dashboard.create!(dashboard_id: "1")
    dashboard.id = "2"
172 173 174
    dashboard.save!

    dashboard = Dashboard.first
175
    assert_equal "2", dashboard.id
176
  end
177

178 179
  def test_create_without_primary_key_no_extra_query
    klass = Class.new(ActiveRecord::Base) do
180
      self.table_name = "dashboards"
181 182 183 184 185
    end
    klass.create! # warmup schema cache
    assert_queries(3, ignore_none: true) { klass.create! }
  end

186 187 188 189 190 191
  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
192 193 194 195 196 197

    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
198
  end
199 200 201
end

class PrimaryKeyWithNoConnectionTest < ActiveRecord::TestCase
202
  self.use_transactional_tests = false
203

204 205 206
  unless in_memory_db?
    def test_set_primary_key_with_no_connection
      connection = ActiveRecord::Base.remove_connection
207

208
      model = Class.new(ActiveRecord::Base)
209
      model.primary_key = "foo"
210

211
      assert_equal "foo", model.primary_key
212

213
      ActiveRecord::Base.establish_connection(connection)
214

215
      assert_equal "foo", model.primary_key
216
    end
217
  end
D
Initial  
David Heinemeier Hansson 已提交
218
end
219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
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

  def test_primary_key_with_auto_increment
235 236 237
    @connection.create_table(:auto_increments, id: :integer, auto_increment: true, force: true)
    assert_auto_incremented
  end
238

239 240 241
  def test_primary_key_with_auto_increment_and_bigint
    @connection.create_table(:auto_increments, id: :bigint, auto_increment: true, force: true)
    assert_auto_incremented
242
  end
243 244 245 246 247 248 249 250 251 252 253 254

  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
255 256
end

R
Ryuta Kamizono 已提交
257
class PrimaryKeyAnyTypeTest < ActiveRecord::TestCase
258 259
  include SchemaDumpingHelper

260
  self.use_transactional_tests = false
R
Ryuta Kamizono 已提交
261 262 263 264 265 266 267 268 269 270

  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
271
    @connection.drop_table(:barcodes, if_exists: true)
R
Ryuta Kamizono 已提交
272 273 274 275 276
  end

  def test_any_type_primary_key
    assert_equal "code", Barcode.primary_key

277
    column = Barcode.column_for_attribute(Barcode.primary_key)
278
    assert_not column.null
279 280
    assert_equal :string, column.type
    assert_equal 42, column.limit
R
Ryuta Kamizono 已提交
281
  end
282 283 284 285 286

  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 已提交
287 288
end

289 290 291 292 293 294 295
class CompositePrimaryKeyTest < ActiveRecord::TestCase
  include SchemaDumpingHelper

  self.use_transactional_tests = false

  def setup
    @connection = ActiveRecord::Base.connection
296
    @connection.schema_cache.clear!
297 298 299 300 301 302 303 304 305 306 307 308 309 310
    @connection.create_table(:barcodes, primary_key: ["region", "code"], force: true) do |t|
      t.string :region
      t.integer :code
    end
  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

311
  def test_primary_key_issues_warning
312 313 314 315 316
    model = Class.new(ActiveRecord::Base) do
      def self.table_name
        "barcodes"
      end
    end
317
    warning = capture(:stderr) do
318
      assert_nil model.primary_key
319
    end
320
    assert_match(/WARNING: Active Record does not support composite primary key\./, warning)
321 322
  end

323 324 325 326 327 328
  def test_collectly_dump_composite_primary_key
    schema = dump_table_schema "barcodes"
    assert_match %r{create_table "barcodes", primary_key: \["region", "code"\]}, schema
  end
end

329 330
class PrimaryKeyIntegerNilDefaultTest < ActiveRecord::TestCase
  include SchemaDumpingHelper
331

332
  self.use_transactional_tests = false
333

334 335 336
  def setup
    @connection = ActiveRecord::Base.connection
  end
337

338 339 340
  def teardown
    @connection.drop_table :int_defaults, if_exists: true
  end
341

342 343 344 345 346 347
  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
348

349 350 351 352
  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
353
  end
354
end
355

356 357 358
if current_adapter?(:PostgreSQLAdapter, :Mysql2Adapter)
  class PrimaryKeyIntegerTest < ActiveRecord::TestCase
    include SchemaDumpingHelper
359

360
    self.use_transactional_tests = false
361

362 363
    class Widget < ActiveRecord::Base
    end
364

365 366 367 368 369 370 371 372
    setup do
      @connection = ActiveRecord::Base.connection
      if current_adapter?(:PostgreSQLAdapter)
        @connection.create_table(:widgets, id: :serial, force: true)
      else
        @connection.create_table(:widgets, id: :integer, force: true)
      end
    end
373

374 375 376
    teardown do
      @connection.drop_table :widgets, if_exists: true
    end
377

378 379 380 381
    test "primary key column type with serial/integer" do
      column = @connection.columns(:widgets).find { |c| c.name == "id" }
      assert_equal :integer, column.type
      assert_not column.bigint?
382 383
    end

384 385 386 387
    test "primary key with serial/integer are automatically numbered" do
      widget = Widget.create!
      assert_not_nil widget.id
    end
388

389 390 391 392 393 394 395
    test "schema dump primary key with serial/integer" do
      schema = dump_table_schema "widgets"
      if current_adapter?(:PostgreSQLAdapter)
        assert_match %r{create_table "widgets", id: :serial, force: :cascade}, schema
      else
        assert_match %r{create_table "widgets", id: :integer, force: :cascade}, schema
      end
396
    end
397

A
Abdelkader Boudih 已提交
398
    if current_adapter?(:Mysql2Adapter)
399 400 401 402 403 404 405 406 407 408 409
      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
        assert_equal 4, column.limit
        assert column.unsigned?

        schema = dump_table_schema "widgets"
        assert_match %r{create_table "widgets", id: :integer, unsigned: true, force: :cascade}, schema
      end
410
    end
411 412
  end
end