primary_keys_test.rb 6.5 KB
Newer Older
1
require "cases/helper"
J
Jeremy Kemper 已提交
2 3 4 5 6
require 'models/topic'
require 'models/subscriber'
require 'models/movie'
require 'models/keyboard'
require 'models/mixed_case_monkey'
D
Initial  
David Heinemeier Hansson 已提交
7

8
class PrimaryKeysTest < ActiveRecord::TestCase
9
  fixtures :topics, :subscribers, :movies, :mixed_case_monkeys
D
Initial  
David Heinemeier Hansson 已提交
10

11
  def test_to_key_with_default_primary_key
12
    topic = Topic.new
13
    assert_nil topic.to_key
14
    topic = Topic.find(1)
15
    assert_equal [1], topic.to_key
16 17 18 19
  end

  def test_to_key_with_customized_primary_key
    keyboard = Keyboard.new
20
    assert_nil keyboard.to_key
21 22
    keyboard.save
    assert_equal keyboard.to_key, [keyboard.id]
23 24
  end

25 26 27 28 29
  def test_read_attribute_with_custom_primary_key
    keyboard = Keyboard.create!
    assert_equal keyboard.key_number, keyboard.read_attribute(:id)
  end

30 31 32
  def test_to_key_with_primary_key_after_destroy
    topic = Topic.find(1)
    topic.destroy
33
    assert_equal [1], topic.to_key
34 35
  end

D
Initial  
David Heinemeier Hansson 已提交
36 37
  def test_integer_key
    topic = Topic.find(1)
38
    assert_equal(topics(:first).author_name, topic.author_name)
D
Initial  
David Heinemeier Hansson 已提交
39
    topic = Topic.find(2)
40
    assert_equal(topics(:second).author_name, topic.author_name)
D
Initial  
David Heinemeier Hansson 已提交
41 42 43

    topic = Topic.new
    topic.title = "New Topic"
44
    assert_nil topic.id
45
    assert_nothing_raised { topic.save! }
D
Initial  
David Heinemeier Hansson 已提交
46 47 48 49 50 51
    id = topic.id

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

52
  def test_customized_primary_key_auto_assigns_on_save
53
    Keyboard.delete_all
54
    keyboard = Keyboard.new(:name => 'HHKB')
55
    assert_nothing_raised { keyboard.save! }
56 57 58
    assert_equal keyboard.id, Keyboard.find_by_name('HHKB').id
  end

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

  def test_customized_string_primary_key_settable_before_save
    subscriber = Subscriber.new
    assert_nothing_raised { subscriber.id = 'webster123' }
    assert_equal 'webster123', subscriber.id
    assert_equal 'webster123', subscriber.nick
  end

D
Initial  
David Heinemeier Hansson 已提交
72
  def test_string_key
73 74 75 76
    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 已提交
77 78 79 80 81

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

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

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

D
Initial  
David Heinemeier Hansson 已提交
93 94
  def test_primary_key_prefix
    ActiveRecord::Base.primary_key_prefix_type = :table_name
95
    Topic.reset_primary_key
D
Initial  
David Heinemeier Hansson 已提交
96 97 98
    assert_equal "topicid", Topic.primary_key

    ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
99
    Topic.reset_primary_key
D
Initial  
David Heinemeier Hansson 已提交
100 101 102
    assert_equal "topic_id", Topic.primary_key

    ActiveRecord::Base.primary_key_prefix_type = nil
103
    Topic.reset_primary_key
D
Initial  
David Heinemeier Hansson 已提交
104 105
    assert_equal "id", Topic.primary_key
  end
J
Jeremy Kemper 已提交
106

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  def test_delete_should_quote_pkey
    assert_nothing_raised { MixedCaseMonkey.delete(1) }
  end
  def test_update_counters_should_quote_pkey_and_quote_counter_columns
    assert_nothing_raised { MixedCaseMonkey.update_counters(1, :fleaCount => 99) }
  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
    assert_nothing_raised { MixedCaseMonkey.find([1,2]) }
  end
  def test_instance_update_should_quote_pkey
    assert_nothing_raised { MixedCaseMonkey.find(1).save }
  end
122
  def test_instance_destroy_should_quote_pkey
123 124
    assert_nothing_raised { MixedCaseMonkey.find(1).destroy }
  end
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

  def test_supports_primary_key
    assert_nothing_raised NoMethodError do
      ActiveRecord::Base.connection.supports_primary_key?
    end
  end

  def test_primary_key_returns_value_if_it_exists
    if ActiveRecord::Base.connection.supports_primary_key?
      assert_equal 'id', ActiveRecord::Base.connection.primary_key('developers')
    end
  end

  def test_primary_key_returns_nil_if_it_does_not_exist
    if ActiveRecord::Base.connection.supports_primary_key?
      assert_nil ActiveRecord::Base.connection.primary_key('developers_projects')
    end
  end
143 144 145 146 147 148 149

  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
J
Jon Leighton 已提交
150

J
Jon Leighton 已提交
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
  def test_two_models_with_same_table_but_different_primary_key
    k1 = Class.new(ActiveRecord::Base)
    k1.table_name = 'posts'
    k1.primary_key = 'id'

    k2 = Class.new(ActiveRecord::Base)
    k2.table_name = 'posts'
    k2.primary_key = 'title'

    assert k1.columns.find { |c| c.name == 'id' }.primary
    assert !k1.columns.find { |c| c.name == 'title' }.primary
    assert k1.columns_hash['id'].primary
    assert !k1.columns_hash['title'].primary

    assert !k2.columns.find { |c| c.name == 'id' }.primary
    assert k2.columns.find { |c| c.name == 'title' }.primary
    assert !k2.columns_hash['id'].primary
    assert k2.columns_hash['title'].primary
  end

  def test_models_with_same_table_have_different_columns
    k1 = Class.new(ActiveRecord::Base)
    k1.table_name = 'posts'

    k2 = Class.new(ActiveRecord::Base)
    k2.table_name = 'posts'

    k1.columns.zip(k2.columns).each do |col1, col2|
      assert !col1.equal?(col2)
    end
J
Jon Leighton 已提交
181
  end
182 183 184 185
end

class PrimaryKeyWithNoConnectionTest < ActiveRecord::TestCase
  self.use_transactional_fixtures = false
186 187 188 189

  def test_set_primary_key_with_no_connection
    return skip("disconnect wipes in-memory db") if in_memory_db?

J
Fix CI  
Jon Leighton 已提交
190
    connection = ActiveRecord::Model.remove_connection
191

192 193
    model = Class.new(ActiveRecord::Base)
    model.primary_key = 'foo'
194 195 196

    assert_equal 'foo', model.primary_key

J
Fix CI  
Jon Leighton 已提交
197
    ActiveRecord::Model.establish_connection(connection)
198 199 200

    assert_equal 'foo', model.primary_key
  end
D
Initial  
David Heinemeier Hansson 已提交
201
end
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
  class PrimaryKeyWithAnsiQuotesTest < ActiveRecord::TestCase
    self.use_transactional_fixtures = false
  
    def test_primaery_key_method_with_ansi_quotes
      con = ActiveRecord::Base.connection
      con.execute("SET SESSION sql_mode='ANSI_QUOTES'")
      assert_equal "id", con.primary_key("topics")
    ensure
      con.reconnect!
    end
  
  end
end