PageRenderTime 73ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/rails/activerecord/test/pk_test.rb

http://github.com/benburkert/cruisecontrolrb
Ruby | 104 lines | 89 code | 15 blank | 0 comment | 0 complexity | 59bcfcb8f9798319cf3cefef4d464fb8 MD5 | raw file
Possible License(s): Apache-2.0
  1. require "#{File.dirname(__FILE__)}/abstract_unit"
  2. require 'fixtures/topic'
  3. require 'fixtures/reply'
  4. require 'fixtures/subscriber'
  5. require 'fixtures/movie'
  6. require 'fixtures/keyboard'
  7. require 'fixtures/mixed_case_monkey'
  8. class PrimaryKeysTest < Test::Unit::TestCase
  9. fixtures :topics, :subscribers, :movies, :mixed_case_monkeys
  10. def test_integer_key
  11. topic = Topic.find(1)
  12. assert_equal(topics(:first).author_name, topic.author_name)
  13. topic = Topic.find(2)
  14. assert_equal(topics(:second).author_name, topic.author_name)
  15. topic = Topic.new
  16. topic.title = "New Topic"
  17. assert_equal(nil, topic.id)
  18. assert_nothing_raised { topic.save! }
  19. id = topic.id
  20. topicReloaded = Topic.find(id)
  21. assert_equal("New Topic", topicReloaded.title)
  22. end
  23. def test_customized_primary_key_auto_assigns_on_save
  24. Keyboard.delete_all
  25. keyboard = Keyboard.new(:name => 'HHKB')
  26. assert_nothing_raised { keyboard.save! }
  27. assert_equal keyboard.id, Keyboard.find_by_name('HHKB').id
  28. end
  29. def test_customized_primary_key_can_be_get_before_saving
  30. keyboard = Keyboard.new
  31. assert_nil keyboard.id
  32. assert_nothing_raised { assert_nil keyboard.key_number }
  33. end
  34. def test_customized_string_primary_key_settable_before_save
  35. subscriber = Subscriber.new
  36. assert_nothing_raised { subscriber.id = 'webster123' }
  37. assert_equal 'webster123', subscriber.id
  38. assert_equal 'webster123', subscriber.nick
  39. end
  40. def test_string_key
  41. subscriber = Subscriber.find(subscribers(:first).nick)
  42. assert_equal(subscribers(:first).name, subscriber.name)
  43. subscriber = Subscriber.find(subscribers(:second).nick)
  44. assert_equal(subscribers(:second).name, subscriber.name)
  45. subscriber = Subscriber.new
  46. subscriber.id = "jdoe"
  47. assert_equal("jdoe", subscriber.id)
  48. subscriber.name = "John Doe"
  49. assert_nothing_raised { subscriber.save! }
  50. assert_equal("jdoe", subscriber.id)
  51. subscriberReloaded = Subscriber.find("jdoe")
  52. assert_equal("John Doe", subscriberReloaded.name)
  53. end
  54. def test_find_with_more_than_one_string_key
  55. assert_equal 2, Subscriber.find(subscribers(:first).nick, subscribers(:second).nick).length
  56. end
  57. def test_primary_key_prefix
  58. ActiveRecord::Base.primary_key_prefix_type = :table_name
  59. Topic.reset_primary_key
  60. assert_equal "topicid", Topic.primary_key
  61. ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
  62. Topic.reset_primary_key
  63. assert_equal "topic_id", Topic.primary_key
  64. ActiveRecord::Base.primary_key_prefix_type = nil
  65. Topic.reset_primary_key
  66. assert_equal "id", Topic.primary_key
  67. end
  68. def test_delete_should_quote_pkey
  69. assert_nothing_raised { MixedCaseMonkey.delete(1) }
  70. end
  71. def test_increment_counter_should_quote_pkey_and_quote_counter_columns
  72. assert_nothing_raised { MixedCaseMonkey.increment_counter(:fleaCount, 1) }
  73. end
  74. def test_decrement_counter_should_quote_pkey_and_quote_counter_columns
  75. assert_nothing_raised { MixedCaseMonkey.decrement_counter(:fleaCount, 1) }
  76. end
  77. def test_find_with_one_id_should_quote_pkey
  78. assert_nothing_raised { MixedCaseMonkey.find(1) }
  79. end
  80. def test_find_with_multiple_ids_should_quote_pkey
  81. assert_nothing_raised { MixedCaseMonkey.find([1,2]) }
  82. end
  83. def test_instance_update_should_quote_pkey
  84. assert_nothing_raised { MixedCaseMonkey.find(1).save }
  85. end
  86. def test_instance_destry_should_quote_pkey
  87. assert_nothing_raised { MixedCaseMonkey.find(1).destroy }
  88. end
  89. end