Froze rails gems
[depot.git] / vendor / rails / activerecord / test / cases / multiple_db_test.rb
1 require "cases/helper"
2 require 'models/entrant'
3
4 # So we can test whether Course.connection survives a reload.
5 require_dependency 'models/course'
6
7 class MultipleDbTest < ActiveRecord::TestCase
8 self.use_transactional_fixtures = false
9
10 def setup
11 @courses = create_fixtures("courses") { Course.retrieve_connection }
12 @entrants = create_fixtures("entrants")
13 end
14
15 def test_connected
16 assert_not_nil Entrant.connection
17 assert_not_nil Course.connection
18 end
19
20 def test_proper_connection
21 assert_not_equal(Entrant.connection, Course.connection)
22 assert_equal(Entrant.connection, Entrant.retrieve_connection)
23 assert_equal(Course.connection, Course.retrieve_connection)
24 assert_equal(ActiveRecord::Base.connection, Entrant.connection)
25 end
26
27 def test_find
28 c1 = Course.find(1)
29 assert_equal "Ruby Development", c1.name
30 c2 = Course.find(2)
31 assert_equal "Java Development", c2.name
32 e1 = Entrant.find(1)
33 assert_equal "Ruby Developer", e1.name
34 e2 = Entrant.find(2)
35 assert_equal "Ruby Guru", e2.name
36 e3 = Entrant.find(3)
37 assert_equal "Java Lover", e3.name
38 end
39
40 def test_associations
41 c1 = Course.find(1)
42 assert_equal 2, c1.entrants.count
43 e1 = Entrant.find(1)
44 assert_equal e1.course.id, c1.id
45 c2 = Course.find(2)
46 assert_equal 1, c2.entrants.count
47 e3 = Entrant.find(3)
48 assert_equal e3.course.id, c2.id
49 end
50
51 def test_course_connection_should_survive_dependency_reload
52 assert Course.connection
53
54 ActiveSupport::Dependencies.clear
55 Object.send(:remove_const, :Course)
56 require_dependency 'models/course'
57
58 assert Course.connection
59 end
60
61 def test_transactions_across_databases
62 c1 = Course.find(1)
63 e1 = Entrant.find(1)
64
65 begin
66 Course.transaction do
67 Entrant.transaction do
68 c1.name = "Typo"
69 e1.name = "Typo"
70 c1.save
71 e1.save
72 raise "No I messed up."
73 end
74 end
75 rescue
76 # Yup caught it
77 end
78
79 assert_equal "Typo", c1.name
80 assert_equal "Typo", e1.name
81
82 assert_equal "Ruby Development", Course.find(1).name
83 assert_equal "Ruby Developer", Entrant.find(1).name
84 end
85 end