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

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

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

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

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

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

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

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

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

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

61
  def test_customized_primary_key_can_be_get_before_saving
62
    keyboard = Keyboard.new
63 64
    assert_nil keyboard.id
    assert_nothing_raised { assert_nil keyboard.key_number }
65 66 67 68 69 70 71 72 73
  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 已提交
74
  def test_string_key
75 76 77 78
    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 已提交
79 80 81 82 83

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

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

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

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

    ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
102
    Topic.reset_primary_key
D
Initial  
David Heinemeier Hansson 已提交
103 104 105
    assert_equal "topic_id", Topic.primary_key

    ActiveRecord::Base.primary_key_prefix_type = nil
106
    Topic.reset_primary_key
D
Initial  
David Heinemeier Hansson 已提交
107
    assert_equal "id", Topic.primary_key
108 109
  ensure
    ActiveRecord::Base.primary_key_prefix_type = old_primary_key_prefix_type
D
Initial  
David Heinemeier Hansson 已提交
110
  end
J
Jeremy Kemper 已提交
111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  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
127
  def test_instance_destroy_should_quote_pkey
128 129
    assert_nothing_raised { MixedCaseMonkey.find(1).destroy }
  end
130 131 132 133 134 135 136 137

  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
138 139 140 141
    klass = Class.new(ActiveRecord::Base) do
      self.table_name = 'developers'
    end

142
    if ActiveRecord::Base.connection.supports_primary_key?
143
      assert_equal 'id', klass.primary_key
144 145 146 147
    end
  end

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

152
    if ActiveRecord::Base.connection.supports_primary_key?
153
      assert_nil klass.primary_key
154 155
    end
  end
156 157 158 159 160 161 162

  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 已提交
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 170 171 172 173 174 175 176

  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
177 178 179 180
end

class PrimaryKeyWithNoConnectionTest < ActiveRecord::TestCase
  self.use_transactional_fixtures = false
181

182 183 184
  unless in_memory_db?
    def test_set_primary_key_with_no_connection
      connection = ActiveRecord::Base.remove_connection
185

186 187
      model = Class.new(ActiveRecord::Base)
      model.primary_key = 'foo'
188

189
      assert_equal 'foo', model.primary_key
190

191
      ActiveRecord::Base.establish_connection(connection)
192

193 194
      assert_equal 'foo', model.primary_key
    end
195
  end
D
Initial  
David Heinemeier Hansson 已提交
196
end
197

R
Ryuta Kamizono 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
class PrimaryKeyAnyTypeTest < ActiveRecord::TestCase
  self.use_transactional_fixtures = false

  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
    @connection.execute("DROP TABLE IF EXISTS barcodes")
  end

  def test_any_type_primary_key
    assert_equal "code", Barcode.primary_key

    column_type = Barcode.type_for_attribute(Barcode.primary_key)
    assert_equal :string, column_type.type
    assert_equal 42, column_type.limit
  end
end

222
if current_adapter?(:MysqlAdapter, :Mysql2Adapter)
223 224
  class PrimaryKeyWithAnsiQuotesTest < ActiveRecord::TestCase
    self.use_transactional_fixtures = false
225

V
Vipul A M 已提交
226
    def test_primary_key_method_with_ansi_quotes
227 228 229 230 231 232 233 234
      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
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

if current_adapter?(:PostgreSQLAdapter)
  class PrimaryKeyBigSerialTest < ActiveRecord::TestCase
    self.use_transactional_fixtures = false

    class Widget < ActiveRecord::Base
    end

    setup do
      @connection = ActiveRecord::Base.connection
      @connection.create_table(:widgets, id: :bigserial) { |t| }
    end

    teardown do
      @connection.drop_table :widgets
    end

    def test_bigserial_primary_key
      assert_equal "id", Widget.primary_key
      assert_equal :integer, Widget.columns_hash[Widget.primary_key].type

      widget = Widget.create!
      assert_not_nil widget.id
    end
  end
end