class PrimaryKeysTest

Public Instance Methods

test_auto_detect_primary_key_from_schema() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 186
def test_auto_detect_primary_key_from_schema
  MixedCaseMonkey.reset_primary_key
  assert_equal "monkeyID", MixedCaseMonkey.primary_key
end
test_create_without_primary_key_no_extra_query() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 200
def test_create_without_primary_key_no_extra_query
  skip if current_adapter?(:OracleAdapter)

  klass = Class.new(ActiveRecord::Base) do
    self.table_name = "dashboards"
  end
  klass.create! # warmup schema cache
  assert_queries(3, ignore_none: true) { klass.create! }
end
test_customized_primary_key_auto_assigns_on_save() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 58
def test_customized_primary_key_auto_assigns_on_save
  Keyboard.delete_all
  keyboard = Keyboard.new(name: "HHKB")
  keyboard.save!
  assert_equal keyboard.id, Keyboard.find_by_name("HHKB").id
end
test_customized_primary_key_can_be_get_before_saving() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 65
def test_customized_primary_key_can_be_get_before_saving
  keyboard = Keyboard.new
  assert_nil keyboard.id
  assert_nil keyboard.key_number
end
test_customized_string_primary_key_settable_before_save() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 71
def test_customized_string_primary_key_settable_before_save
  subscriber = Subscriber.new
  subscriber.id = "webster123"
  assert_equal "webster123", subscriber.id
  assert_equal "webster123", subscriber.nick
end
test_delete_should_quote_pkey() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 135
def test_delete_should_quote_pkey
  assert_nothing_raised { MixedCaseMonkey.delete(1) }
end
test_deprecate_supports_primary_key() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 159
def test_deprecate_supports_primary_key
  assert_deprecated { ActiveRecord::Base.connection.supports_primary_key? }
end
test_find_with_more_than_one_string_key() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 114
def test_find_with_more_than_one_string_key
  assert_equal 2, Subscriber.find(subscribers(:first).nick, subscribers(:second).nick).length
end
test_find_with_multiple_ids_should_quote_pkey() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 147
def test_find_with_multiple_ids_should_quote_pkey
  assert_nothing_raised { MixedCaseMonkey.find([1, 2]) }
end
test_find_with_one_id_should_quote_pkey() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 143
def test_find_with_one_id_should_quote_pkey
  assert_nothing_raised { MixedCaseMonkey.find(1) }
end
test_id_column_that_is_not_primary_key() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 108
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
test_instance_destroy_should_quote_pkey() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 155
def test_instance_destroy_should_quote_pkey
  assert_nothing_raised { MixedCaseMonkey.find(1).destroy }
end
test_instance_update_should_quote_pkey() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 151
def test_instance_update_should_quote_pkey
  assert_nothing_raised { MixedCaseMonkey.find(1).save }
end
test_integer_key() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 42
def test_integer_key
  topic = Topic.find(1)
  assert_equal(topics(:first).author_name, topic.author_name)
  topic = Topic.find(2)
  assert_equal(topics(:second).author_name, topic.author_name)

  topic = Topic.new
  topic.title = "New Topic"
  assert_nil topic.id
  topic.save!
  id = topic.id

  topicReloaded = Topic.find(id)
  assert_equal("New Topic", topicReloaded.title)
end
test_primary_key_prefix() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 118
def test_primary_key_prefix
  old_primary_key_prefix_type = ActiveRecord::Base.primary_key_prefix_type
  ActiveRecord::Base.primary_key_prefix_type = :table_name
  Topic.reset_primary_key
  assert_equal "topicid", Topic.primary_key

  ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
  Topic.reset_primary_key
  assert_equal "topic_id", Topic.primary_key

  ActiveRecord::Base.primary_key_prefix_type = nil
  Topic.reset_primary_key
  assert_equal "id", Topic.primary_key
ensure
  ActiveRecord::Base.primary_key_prefix_type = old_primary_key_prefix_type
end
test_primary_key_returns_nil_if_it_does_not_exist() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 171
def test_primary_key_returns_nil_if_it_does_not_exist
  klass = Class.new(ActiveRecord::Base) do
    self.table_name = "developers_projects"
  end

  assert_nil klass.primary_key
end
test_primary_key_returns_value_if_it_exists() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 163
def test_primary_key_returns_value_if_it_exists
  klass = Class.new(ActiveRecord::Base) do
    self.table_name = "developers"
  end

  assert_equal "id", klass.primary_key
end
test_primary_key_update_with_custom_key_name() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 191
def test_primary_key_update_with_custom_key_name
  dashboard = Dashboard.create!(dashboard_id: "1")
  dashboard.id = "2"
  dashboard.save!

  dashboard = Dashboard.first
  assert_equal "2", dashboard.id
end
test_quoted_primary_key_after_set_primary_key() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 179
def test_quoted_primary_key_after_set_primary_key
  k = Class.new(ActiveRecord::Base)
  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
test_read_attribute_with_custom_primary_key() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 31
def test_read_attribute_with_custom_primary_key
  keyboard = Keyboard.create!
  assert_equal keyboard.key_number, keyboard.read_attribute(:id)
end
test_serial_with_quoted_sequence_name() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 211
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
test_serial_with_unquoted_sequence_name() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 217
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
test_string_key() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 91
def test_string_key
  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)

  subscriber = Subscriber.new
  subscriber.id = "jdoe"
  assert_equal("jdoe", subscriber.id)
  subscriber.name = "John Doe"
  subscriber.save!
  assert_equal("jdoe", subscriber.id)

  subscriberReloaded = Subscriber.find("jdoe")
  assert_equal("John Doe", subscriberReloaded.name)
end
test_to_key_with_customized_primary_key() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 24
def test_to_key_with_customized_primary_key
  keyboard = Keyboard.new
  assert_nil keyboard.to_key
  keyboard.save
  assert_equal keyboard.to_key, [keyboard.id]
end
test_to_key_with_default_primary_key() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 17
def test_to_key_with_default_primary_key
  topic = Topic.new
  assert_nil topic.to_key
  topic = Topic.find(1)
  assert_equal [1], topic.to_key
end
test_to_key_with_primary_key_after_destroy() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 36
def test_to_key_with_primary_key_after_destroy
  topic = Topic.find(1)
  topic.destroy
  assert_equal [1], topic.to_key
end
test_update_columns_with_non_primary_key_id_column() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 85
def test_update_columns_with_non_primary_key_id_column
  subscriber = Subscriber.first
  subscriber.update_columns(id: 1)
  assert_not_equal 1, subscriber.nick
end
test_update_counters_should_quote_pkey_and_quote_counter_columns() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 139
def test_update_counters_should_quote_pkey_and_quote_counter_columns
  assert_nothing_raised { MixedCaseMonkey.update_counters(1, fleaCount: 99) }
end
test_update_with_non_primary_key_id_column() click to toggle source
# File activerecord/test/cases/primary_keys_test.rb, line 78
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