arid_cache 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,217 @@
1
+ require 'spec_helper'
2
+
3
+ describe AridCache::CacheProxy::ResultProcessor do
4
+
5
+ def new_result(value, opts={})
6
+ AridCache::CacheProxy::ResultProcessor.new(value, opts)
7
+ end
8
+
9
+ describe "empty array" do
10
+ before :each do
11
+ @result = new_result([])
12
+ end
13
+
14
+ it "should recognize an empty array" do
15
+ @result.is_enumerable?.should be_true
16
+ @result.is_empty?.should be_true
17
+ @result.is_activerecord?.should be_false
18
+ @result.is_hashes?.should be_false
19
+ @result.is_cached_result?.should be_false
20
+ @result.order_in_database?.should be_false
21
+ @result.to_cache.should be_a(AridCache::CacheProxy::CachedResult)
22
+ end
23
+ end
24
+
25
+ describe "basic array" do
26
+ before :each do
27
+ @result = new_result([1,2,3])
28
+ end
29
+
30
+ it "should recognize an empty array" do
31
+ @result.is_enumerable?.should be_true
32
+ @result.is_empty?.should be_false
33
+ @result.is_activerecord?.should be_false
34
+ @result.is_hashes?.should be_false
35
+ @result.is_activerecord_reflection?.should be_false
36
+ @result.order_in_database?.should be_false
37
+ @result.to_cache.should == [1,2,3]
38
+ end
39
+ end
40
+
41
+ describe "array of activerecords" do
42
+ before :each do
43
+ @company = Company.make
44
+ @result = new_result([@company])
45
+ end
46
+
47
+ it "should recognize activerecords" do
48
+ @result.is_enumerable?.should be_true
49
+ @result.is_empty?.should be_false
50
+ @result.is_activerecord?.should be_true
51
+ @result.is_hashes?.should be_false
52
+ @result.order_in_database?.should be_false
53
+ @result.is_activerecord_reflection?.should be_false
54
+ end
55
+
56
+ it "should convert to a CachedResult" do
57
+ @cached = @result.to_cache
58
+ @cached.should be_a(AridCache::CacheProxy::CachedResult)
59
+ @cached.ids.should == [@company.id]
60
+ @cached.klass.should == @company.class
61
+ @cached.count.should == 1
62
+ end
63
+ end
64
+
65
+ describe "array of hashes" do
66
+ before :each do
67
+ @result = new_result([{}, {}])
68
+ end
69
+
70
+ it "should be recognized" do
71
+ @result.is_enumerable?.should be_true
72
+ @result.is_empty?.should be_false
73
+ @result.is_activerecord?.should be_false
74
+ @result.is_hashes?.should be_true
75
+ @result.order_in_database?.should be_false
76
+ @cached = @result.to_cache
77
+ @cached.should == [{}, {}]
78
+ end
79
+ end
80
+
81
+ describe "proxy reflections" do
82
+ before :each do
83
+ @user = User.make
84
+ end
85
+
86
+ it "should be recognized" do
87
+ @result = new_result(@user.companies)
88
+ @result.is_activerecord_reflection?.should be_true
89
+ end
90
+
91
+ it "should recognize named scope" do
92
+ @result = new_result(User.companies)
93
+ @result.is_activerecord_reflection?.should be_true
94
+ end
95
+ end
96
+
97
+ describe "cached result" do
98
+ before :each do
99
+ @result = new_result(AridCache::CacheProxy::CachedResult.new)
100
+ end
101
+
102
+ it "should be recognized" do
103
+ @result.is_cached_result?.should be_true
104
+ end
105
+ end
106
+
107
+ describe "order in database" do
108
+ before :each do
109
+ @company = Company.make
110
+ @cached = AridCache::CacheProxy::CachedResult.new
111
+ @cached.klass = Company
112
+ @cached.ids = [@company.id]
113
+ end
114
+
115
+ it "cached results should use the database for ordering" do
116
+ @result = new_result(@cached, :order => 'column DESC')
117
+ @result.is_cached_result?.should be_true
118
+ @result.order_in_database?.should be_true
119
+ end
120
+
121
+ it "active records should use the database only if an order is specified" do
122
+ @result = new_result([@company], :order => 'column DESC')
123
+ @result.order_in_database?.should be_true
124
+ @result = new_result([@company], :order => :symbol)
125
+ @result.order_in_database?.should be_true
126
+ @result = new_result([@company], :order => Proc.new {})
127
+ @result.order_in_database?.should be_false
128
+ end
129
+ end
130
+
131
+
132
+ describe "non-activerecord enumerables" do
133
+ before :each do
134
+ @value = (1..10).to_a
135
+ end
136
+
137
+ it "should return it unmodified" do
138
+ new_result(@value).to_result.should == @value
139
+ end
140
+
141
+ it "should apply limit" do
142
+ @limit = 3
143
+ new_result(@value, :limit => @limit).to_result.should == @value[0,@limit]
144
+ new_result(@value, :limit => @limit).to_result.size.should == @limit
145
+ new_result(@value, :limit => @value.size).to_result.should == @value
146
+ new_result(@value, :limit => 0).to_result.should == []
147
+ end
148
+
149
+ it "should apply offset" do
150
+ @offset = 3
151
+ new_result(@value, :offset => @offset).to_result.should == @value[@offset,@value.size]
152
+ new_result(@value, :offset => @offset).to_result.size.should == @value.size - @offset
153
+ new_result(@value, :offset => @value.size).to_result.should == []
154
+ end
155
+
156
+ it "should apply offset and limit" do
157
+ @offset = 2
158
+ @limit = 3
159
+ new_result(@value, :offset => @offset, :limit => @limit).to_result.should == @value[@offset,@limit]
160
+ new_result(@value, :offset => @offset, :limit => @limit).to_result.size.should == @limit
161
+ end
162
+
163
+ describe "order by" do
164
+ before :each do
165
+ @low = [1, 2, 3, 4]
166
+ @high = [5, 6, 7, 8]
167
+ @hashes = [{ 'low' => 4, :high => 6 }, { 'low' => 3, :high => 5 }, { 'low' => 1, :high => 8 }, { 'low' => 2, :high => 7 }]
168
+ @value = (1..10).to_a
169
+ end
170
+
171
+ it "should order by proc" do
172
+ new_result(@value, :order => Proc.new { |a, b| b <=> a }).to_result.should == @value.reverse
173
+ end
174
+
175
+ it "should order hashes by string key" do
176
+ new_result(@hashes, :order => 'low').to_result.collect { |h| h['low'] }.should == @low
177
+ end
178
+
179
+ it "should order hashes by symbol key" do
180
+ new_result(@hashes, :order => :high).to_result.collect { |h| h[:high] }.should == @high
181
+ end
182
+ end
183
+
184
+ describe "paginating arrays" do
185
+ before :each do
186
+ @value = (1..10).to_a
187
+ end
188
+
189
+ it "should paginate" do
190
+ @result = new_result(@value, :page => 1).to_result
191
+ @result.should be_a(WillPaginate::Collection)
192
+ @result.total_entries.should == @value.size
193
+ @result.current_page.should == 1
194
+ end
195
+
196
+ it "should handle per_page option" do
197
+ @result = new_result(@value, :page => 3, :per_page => 3).to_result
198
+ @result.should be_a(WillPaginate::Collection)
199
+ @result.total_entries.should == @value.size
200
+ @result.current_page.should == 3
201
+ @result.per_page.should == 3
202
+ end
203
+ end
204
+
205
+ it "should order limit and then paginate all at once" do
206
+ # It will reverse it, offset 2, limit 15, then paginate
207
+ @options = {
208
+ :limit => 15,
209
+ :offset => 2,
210
+ :order => Proc.new { |a, b| b <=> a },
211
+ :page => 2,
212
+ :per_page => 5
213
+ }
214
+ @result = new_result((1..20).to_a, @options).to_result.should == [13, 12, 11, 10, 9]
215
+ end
216
+ end
217
+ end
@@ -1,95 +1,124 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe AridCache::CacheProxy do
4
- describe 'preserve_order' do
4
+ # describe 'preserve_order' do
5
+ # before :each do
6
+ # @proxy = AridCache::CacheProxy.new(Company, 'dummy-key', {})
7
+ # end
8
+ #
9
+ # it "id column should be prefixed by the table name" do
10
+ # ::ActiveRecord::Base.stubs(:is_mysql_adapter?).returns(true)
11
+ # @proxy.send(:preserve_order, [1,2,3]).should =~ %r[#{Company.table_name}]
12
+ # end
13
+ #
14
+ # it "id column should be prefixed by the table name" do
15
+ # ::ActiveRecord::Base.stubs(:is_mysql_adapter?).returns(false)
16
+ # @proxy.send(:preserve_order, [1,2,3]).should =~ %r[#{Company.table_name}]
17
+ # end
18
+ # end
19
+ #
20
+ # describe "with raw => true" do
21
+ # before :each do
22
+ # @user = User.make
23
+ # @user.companies << Company.make
24
+ # @user.companies << Company.make
25
+ # @user.clear_instance_caches
26
+ # end
27
+ #
28
+ # it "should return a CacheProxy::CachedResult" do
29
+ # @user.cached_companies(:raw => true).should be_a(AridCache::CacheProxy::CachedResult)
30
+ # end
31
+ #
32
+ # it "result should have the same ids as the normal result" do
33
+ # @user.cached_companies(:raw => true).ids.should == @user.cached_companies.collect(&:id)
34
+ # end
35
+ #
36
+ # it "should ignore :raw => false" do
37
+ # @user.cached_companies(:raw => false).should == @user.cached_companies
38
+ # end
39
+ #
40
+ # it "should only query once to seed the cache, ignoring all other options" do
41
+ # lambda { @user.cached_companies(:raw => true, :limit => 0, :order => 'nonexistent_column desc') }.should query(1)
42
+ # end
43
+ #
44
+ # it "should ignore all other options if the cache has already been seeded" do
45
+ # lambda {
46
+ # companies = @user.cached_companies
47
+ # @user.cached_companies(:raw => true, :limit => 0, :order => 'nonexistent_column').ids.should == companies.collect(&:id)
48
+ # }.should query(1)
49
+ # end
50
+ #
51
+ # it "should not use the raw option when reading from the cache" do
52
+ # Rails.cache.expects(:read).with(@user.arid_cache_key(:companies), {})
53
+ # @user.cached_companies(:raw => true)
54
+ # end
55
+ #
56
+ # it "should work for calls to a method that ends with _count" do
57
+ # @user.cached_bogus_count do
58
+ # 10
59
+ # end
60
+ # @user.cached_bogus_count(:raw => true).should == 10
61
+ # end
62
+ #
63
+ # it "should work for calls to a method that ends with _count" do
64
+ # @user.cached_companies_count(:raw => true).should == @user.cached_companies_count
65
+ # end
66
+ # end
67
+ #
68
+ # describe "with clear => true" do
69
+ # before :each do
70
+ # @user = User.make
71
+ # @user.companies << Company.make
72
+ # @user.companies << Company.make
73
+ # @user.clear_instance_caches rescue Rails.cache.clear
74
+ # end
75
+ #
76
+ # it "should not fail if there is no cached value" do
77
+ # lambda { @user.cached_companies(:clear => true) }.should_not raise_exception
78
+ # end
79
+ #
80
+ # it "should clear the cached entry" do
81
+ # key = @user.arid_cache_key(:companies)
82
+ # @user.cached_companies
83
+ # Rails.cache.read(key).should_not be_nil
84
+ # @user.cached_companies(:clear => true)
85
+ # Rails.cache.read(key).should be_nil
86
+ # end
87
+ #
88
+ # it "should not read from the cache or database" do
89
+ # Rails.cache.expects(:read).never
90
+ # lambda {
91
+ # @user.cached_companies(:clear => true)
92
+ # }.should query(0)
93
+ # end
94
+ # end
95
+
96
+ describe "arrays" do
5
97
  before :each do
6
- @proxy = AridCache::CacheProxy.new(Company, 'dummy-key', {})
7
- end
8
-
9
- it "id column should be prefixed by the table name" do
10
- ::ActiveRecord::Base.stubs(:is_mysql_adapter?).returns(true)
11
- @proxy.send(:preserve_order, [1,2,3]).should =~ %r[#{Company.table_name}]
12
- end
13
-
14
- it "id column should be prefixed by the table name" do
15
- ::ActiveRecord::Base.stubs(:is_mysql_adapter?).returns(false)
16
- @proxy.send(:preserve_order, [1,2,3]).should =~ %r[#{Company.table_name}]
17
- end
18
- end
19
-
20
- describe "with raw => true" do
21
- before :each do
22
- @user = User.make
23
- @user.companies << Company.make
24
- @user.companies << Company.make
25
- @user.clear_instance_caches
26
- end
27
-
28
- it "should return a CacheProxy::Result" do
29
- @user.cached_companies(:raw => true).should be_a(AridCache::CacheProxy::Result)
30
- end
31
-
32
- it "result should have the same ids as the normal result" do
33
- @user.cached_companies(:raw => true).ids.should == @user.cached_companies.collect(&:id)
34
- end
35
-
36
- it "should ignore :raw => false" do
37
- @user.cached_companies(:raw => false).should == @user.cached_companies
38
- end
39
-
40
- it "should only query once to seed the cache, ignoring all other options" do
41
- lambda { @user.cached_companies(:raw => true, :limit => 0, :order => 'nonexistent_column desc') }.should query(1)
42
- end
43
-
44
- it "should ignore all other options if the cache has already been seeded" do
45
- lambda {
46
- companies = @user.cached_companies
47
- @user.cached_companies(:raw => true, :limit => 0, :order => 'nonexistent_column').ids.should == companies.collect(&:id)
48
- }.should query(1)
49
- end
50
-
51
- it "should not use the raw option when reading from the cache" do
52
- Rails.cache.expects(:read).with(@user.arid_cache_key(:companies), {})
53
- @user.cached_companies(:raw => true)
54
- end
55
-
56
- it "should work for calls to a method that ends with _count" do
57
- @user.cached_bogus_count do
58
- 10
59
- end
60
- @user.cached_bogus_count(:raw => true).should == 10
61
- end
62
-
63
- it "should work for calls to a method that ends with _count" do
64
- @user.cached_companies_count(:raw => true).should == @user.cached_companies_count
65
- end
66
- end
67
-
68
- describe "with clear => true" do
69
- before :each do
70
- @user = User.make
71
- @user.companies << Company.make
72
- @user.companies << Company.make
73
- @user.clear_instance_caches rescue Rails.cache.clear
74
- end
75
-
76
- it "should not fail if there is no cached value" do
77
- lambda { @user.cached_companies(:clear => true) }.should_not raise_exception
78
- end
79
-
80
- it "should clear the cached entry" do
81
- key = @user.arid_cache_key(:companies)
82
- @user.cached_companies
83
- Rails.cache.read(key).should_not be_nil
84
- @user.cached_companies(:clear => true)
85
- Rails.cache.read(key).should be_nil
86
- end
87
-
88
- it "should not read from the cache or database" do
89
- Rails.cache.expects(:read).never
90
- lambda {
91
- @user.cached_companies(:clear => true)
92
- }.should query(0)
93
- end
98
+ @o = Class.new do
99
+ include AridCache
100
+ def result
101
+ @result ||= (1..5).to_a
102
+ end
103
+ end.new
104
+ end
105
+
106
+ # it "should cache the result" do
107
+ # @o.cached_result { result }.should == @o.result
108
+ # end
109
+ #
110
+ # it "should respect the :limit option" do
111
+ # @o.cached_result(:limit => 4) { result }.should == [1,2,3,4]
112
+ # end
113
+
114
+ it "should respect the :offset option" do
115
+ @o.cached_result(:offset => 2) { result }.should == [3,4,5]
116
+ end
117
+
118
+ # it "should apply pagination" do
119
+ # result = @o.cached_result(:page => 2, :per_page => 2) { result }
120
+ # result.should == @o.paginate(:page => 2, :per_page => 2)
121
+ # result.total_entries.should == @o.result.size
122
+ # end
94
123
  end
95
124
  end
data/test/console CHANGED
@@ -1,3 +1,5 @@
1
+ #!/usr/bin/ruby
2
+
1
3
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
2
4
 
3
5
  require 'rubygems'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arid_cache
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 1.2.0
10
+ version: 1.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Karl Varga
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-28 00:00:00 -07:00
18
+ date: 2011-04-05 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -98,6 +98,9 @@ files:
98
98
  - lib/arid_cache.rb
99
99
  - lib/arid_cache/active_record.rb
100
100
  - lib/arid_cache/cache_proxy.rb
101
+ - lib/arid_cache/cache_proxy/options.rb
102
+ - lib/arid_cache/cache_proxy/result_processor.rb
103
+ - lib/arid_cache/cache_proxy/utilities.rb
101
104
  - lib/arid_cache/helpers.rb
102
105
  - lib/arid_cache/inflector.rb
103
106
  - lib/arid_cache/inflector/inflections.rb
@@ -106,7 +109,9 @@ files:
106
109
  - rails/init.rb
107
110
  - spec/arid_cache/active_record_spec.rb
108
111
  - spec/arid_cache/arid_cache_spec.rb
109
- - spec/arid_cache/cache_proxy_result_spec.rb
112
+ - spec/arid_cache/cache_proxy/cached_result_spec.rb
113
+ - spec/arid_cache/cache_proxy/options_spec.rb
114
+ - spec/arid_cache/cache_proxy/result_processor_spec.rb
110
115
  - spec/arid_cache/cache_proxy_spec.rb
111
116
  - spec/spec.opts
112
117
  - spec/spec_helper.rb
@@ -162,7 +167,9 @@ summary: Automates efficient caching of your ActiveRecord collections, gives you
162
167
  test_files:
163
168
  - spec/arid_cache/active_record_spec.rb
164
169
  - spec/arid_cache/arid_cache_spec.rb
165
- - spec/arid_cache/cache_proxy_result_spec.rb
170
+ - spec/arid_cache/cache_proxy/cached_result_spec.rb
171
+ - spec/arid_cache/cache_proxy/options_spec.rb
172
+ - spec/arid_cache/cache_proxy/result_processor_spec.rb
166
173
  - spec/arid_cache/cache_proxy_spec.rb
167
174
  - spec/spec_helper.rb
168
175
  - spec/support/ar_query.rb