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

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

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

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

26 27 28
  def test_to_key_with_primary_key_after_destroy
    topic = Topic.find(1)
    topic.destroy
29
    assert_equal [1], topic.to_key
30 31
  end

D
Initial  
David Heinemeier Hansson 已提交
32 33
  def test_integer_key
    topic = Topic.find(1)
34
    assert_equal(topics(:first).author_name, topic.author_name)
D
Initial  
David Heinemeier Hansson 已提交
35
    topic = Topic.find(2)
36
    assert_equal(topics(:second).author_name, topic.author_name)
D
Initial  
David Heinemeier Hansson 已提交
37 38 39

    topic = Topic.new
    topic.title = "New Topic"
40
    assert_nil topic.id
41
    assert_nothing_raised { topic.save! }
D
Initial  
David Heinemeier Hansson 已提交
42 43 44 45 46 47
    id = topic.id

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

48
  def test_customized_primary_key_auto_assigns_on_save
49
    Keyboard.delete_all
50
    keyboard = Keyboard.new(:name => 'HHKB')
51
    assert_nothing_raised { keyboard.save! }
52 53 54
    assert_equal keyboard.id, Keyboard.find_by_name('HHKB').id
  end

55
  def test_customized_primary_key_can_be_get_before_saving
56
    keyboard = Keyboard.new
57 58
    assert_nil keyboard.id
    assert_nothing_raised { assert_nil keyboard.key_number }
59 60 61 62 63 64 65 66 67
  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 已提交
68
  def test_string_key
69 70 71 72
    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 已提交
73 74 75 76 77

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

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

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

D
Initial  
David Heinemeier Hansson 已提交
89 90
  def test_primary_key_prefix
    ActiveRecord::Base.primary_key_prefix_type = :table_name
91
    Topic.reset_primary_key
D
Initial  
David Heinemeier Hansson 已提交
92 93 94
    assert_equal "topicid", Topic.primary_key

    ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
95
    Topic.reset_primary_key
D
Initial  
David Heinemeier Hansson 已提交
96 97 98
    assert_equal "topic_id", Topic.primary_key

    ActiveRecord::Base.primary_key_prefix_type = nil
99
    Topic.reset_primary_key
D
Initial  
David Heinemeier Hansson 已提交
100 101
    assert_equal "id", Topic.primary_key
  end
J
Jeremy Kemper 已提交
102

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

  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
139 140 141 142 143 144 145 146 147

  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
    k.set_primary_key "bar"
    assert_equal k.connection.quote_column_name("bar"), k.quoted_primary_key
  end
D
Initial  
David Heinemeier Hansson 已提交
148
end