public
Description: simple identity map for active record. eager loading associations FTL
Clone URL: git://github.com/technoweenie/active_record_context.git
Click here to lend your support to: active_record_context and make a donation at www.pledgie.com !
add tests for belongs_to/polymorphic associations

git-svn-id: 
http://svn.techno-weenie.net/projects/plugins/active_record_context@2513 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Sat Nov 25 23:33:38 -0800 2006
commit  239e3da70a266de8079c54a29020bc8e1092026f
tree    e6f44212097e71aed9e4c00b4a747e45b71d15c2
parent  2c267883690e1dfa3df5a5545537b5c86ebf1ac8
...
16
17
18
19
20
 
 
 
 
 
 
 
 
 
 
21
22
...
16
17
18
 
 
19
20
21
22
23
24
25
26
27
28
29
30
0
@@ -16,6 +16,14 @@ load(File.dirname(__FILE__) + "/schema.rb")
0
 
0
 ActiveRecord::Base.send :extend, Technoweenie::ActiveRecordContext
0
 
0
-class Foo < ActiveRecord::Base
0
- set_table_name 'foo'
0
+class Topic < ActiveRecord::Base
0
+end
0
+
0
+class Post < ActiveRecord::Base; end
0
+class NormalPost < Post
0
+ belongs_to :topic
0
+end
0
+
0
+class PolymorphPost < Post
0
+ belongs_to :topic, :polymorphic => true
0
 end
0
\ No newline at end of file
...
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
...
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
60
61
62
0
@@ -2,39 +2,61 @@ require File.join(File.dirname(__FILE__), 'abstract_unit')
0
 
0
 class ActiveRecordContextTest < Test::Unit::TestCase
0
   def setup
0
- Foo.destroy_all
0
- @records = {}
0
- 2.times { |i| f = Foo.create!(:bar => "test#{i}"); @records[f.id] = f }
0
- assert_nil Foo.context_cache
0
+ Post.destroy_all
0
+ @posts = []
0
+ @topic = Topic.create! :title => 'test'
0
+ @posts << NormalPost.create!(:body => 'normal body', :topic => @topic)
0
+ @posts << PolymorphPost.create!(:body => 'polymorph body', :topic => @topic)
0
+ assert_nil Post.context_cache
0
   end
0
 
0
   def test_should_initialize_context_cache_hash
0
- Foo.with_context do
0
- assert_kind_of Hash, Foo.context_cache
0
- assert_equal 0, Foo.context_cache.size
0
+ Post.with_context do
0
+ assert_kind_of Hash, Post.context_cache
0
+ assert_equal 0, Post.context_cache.size
0
     end
0
- assert_nil Foo.context_cache
0
+ assert_nil Post.context_cache
0
   end
0
 
0
   def test_should_store_records_in_cache
0
- Foo.with_context do
0
- records = Foo.find(:all)
0
- assert_equal records.size, Foo.context_cache[Foo].size
0
- assert_equal @records[1], Foo.find_in_context(1)
0
- assert_equal @records[2], Foo.find_in_context(2)
0
+ Post.with_context do
0
+ records = Post.find(:all)
0
+ assert_equal records.size, Post.context_cache[Post].size
0
+ assert_equal @posts[0], Post.find_in_context(1)
0
+ assert_equal @posts[1], Post.find_in_context(2)
0
     end
0
   end
0
   
0
   def test_should_find_records_in_context
0
- Foo.with_context do
0
- records = Foo.find(:all)
0
- Foo.destroy_all
0
- assert_equal @records[1], Foo.find(1)
0
- assert_equal @records[2], Foo.find(2)
0
+ Post.with_context do
0
+ records = Post.find(:all)
0
+ Post.destroy_all
0
+ assert_equal @posts[0], Post.find(1)
0
+ assert_equal @posts[1], Post.find(2)
0
     end
0
     
0
     assert_raise ActiveRecord::RecordNotFound do
0
- Foo.find 1
0
+ Post.find 1
0
     end
0
   end
0
+
0
+ def test_should_find_belongs_to_record
0
+ Post.with_context do
0
+ Topic.find :all ; Topic.delete_all
0
+ assert_equal @topic, @posts[0].topic(true)
0
+ end
0
+
0
+ assert_equal @topic, @posts[0].topic
0
+ assert_nil @posts[0].topic(true)
0
+ end
0
+
0
+ def test_should_find_belongs_to_polymorphic_record
0
+ Post.with_context do
0
+ Topic.find :all ; Topic.delete_all
0
+ assert_equal @topic, @posts[1].topic(true)
0
+ end
0
+
0
+ assert_equal @topic, @posts[1].topic
0
+ assert_nil @posts[1].topic(true)
0
+ end
0
 end
...
1
2
3
 
 
 
 
 
 
 
 
 
4
5
6
...
1
 
 
2
3
4
5
6
7
8
9
10
11
12
13
0
@@ -1,5 +1,12 @@
0
 ActiveRecord::Schema.define(:version => 0) do
0
- create_table :foo, :force => true do |t|
0
- t.column :bar, :string
0
+ create_table :topics, :force => true do |t|
0
+ t.column :title, :string
0
+ end
0
+
0
+ create_table :posts, :force => true do |t|
0
+ t.column :topic_id, :integer
0
+ t.column :topic_type, :string
0
+ t.column :type, :string
0
+ t.column :body, :string
0
   end
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.