aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/pk_test.rb
blob: aefaebde6e72af9e8b4f9521e79fbf85d81f64f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'abstract_unit'
require 'fixtures/topic'
require 'fixtures/subscriber'
require 'fixtures/movie'

class PrimaryKeysTest < Test::Unit::TestCase
  def setup
    @topics      = create_fixtures "topics"
    @subscribers = create_fixtures "subscribers"
    @movies      = create_fixtures "movies"
  end

  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_equal(nil, topic.id)
    assert_nothing_raised{ topic.save }
    id = topic.id

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

  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"
    assert_nothing_raised{ subscriber.save }

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

  def test_find_with_more_than_one_string_key
    assert_equal 2, Subscriber.find(@subscribers["first"]["nick"], @subscribers["second"]["nick"]).length
  end
  
  def test_primary_key_prefix
    ActiveRecord::Base.primary_key_prefix_type = :table_name
    assert_equal "topicid", Topic.primary_key

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

    ActiveRecord::Base.primary_key_prefix_type = nil
    assert_equal "id", Topic.primary_key
  end
end