active_remote-cached 0.2.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,63 +1,96 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  class SearchMethodClass
4
6
  include ::ActiveRemote::Cached
5
7
 
6
- def self.derp; nil; end
7
- def self.find; nil; end
8
- def self.search; nil; end
8
+ def self.derp
9
+ nil
10
+ end
11
+
12
+ def self.find
13
+ nil
14
+ end
15
+
16
+ def self.search
17
+ nil
18
+ end
9
19
 
10
20
  cached_finders_for :foo, :expires_in => 500
11
21
  cached_finders_for :guid
12
22
  cached_finders_for :guid, :user_guid
13
- cached_finders_for [:user_guid, :client_guid]
14
- cached_finders_for [:derp, :user_guid, :client_guid]
23
+ cached_finders_for %i[user_guid client_guid]
24
+ cached_finders_for %i[derp user_guid client_guid]
25
+ cached_finders_for %i[zippy123 alpha]
15
26
  end
16
27
 
17
28
  describe SearchMethodClass do
18
- describe "API" do
29
+ let(:versioned_prefix) do
30
+ "#{RUBY_ENGINE_VERSION}:#{ActiveSupport::VERSION::STRING}"
31
+ end
32
+
33
+ describe 'API' do
19
34
  it "creates 'cached_search_by_foo'" do
20
- SearchMethodClass.must_respond_to("cached_search_by_foo")
35
+ expect(SearchMethodClass).to respond_to(:cached_search_by_foo)
21
36
  end
22
37
 
23
38
  it "creates 'cached_search_by_foo!'" do
24
- SearchMethodClass.must_respond_to("cached_search_by_foo!")
39
+ expect(SearchMethodClass).to respond_to(:cached_search_by_foo!)
25
40
  end
26
41
 
27
42
  it "creates 'cached_search_by_guid'" do
28
- SearchMethodClass.must_respond_to("cached_search_by_guid")
43
+ expect(SearchMethodClass).to respond_to(:cached_search_by_guid)
29
44
  end
30
45
 
31
46
  it "creates 'cached_search_by_user_guid'" do
32
- SearchMethodClass.must_respond_to("cached_search_by_user_guid")
47
+ expect(SearchMethodClass).to respond_to(:cached_search_by_user_guid)
33
48
  end
34
49
 
35
50
  it "creates 'cached_search_by_user_guid_and_client_guid'" do
36
- SearchMethodClass.must_respond_to("cached_search_by_user_guid_and_client_guid")
51
+ expect(SearchMethodClass).to respond_to(:cached_search_by_user_guid_and_client_guid)
37
52
  end
38
53
 
39
54
  it "creates 'cached_search_by_client_guid_and_user_guid'" do
40
- SearchMethodClass.must_respond_to("cached_search_by_client_guid_and_user_guid")
55
+ expect(SearchMethodClass).to respond_to(:cached_search_by_client_guid_and_user_guid)
41
56
  end
42
57
 
43
58
  it "creates 'cached_search_by_derp_and_user_guid_and_client_guid'" do
44
- SearchMethodClass.must_respond_to("cached_search_by_derp_and_user_guid_and_client_guid")
59
+ expect(SearchMethodClass).to respond_to(:cached_search_by_derp_and_user_guid_and_client_guid)
45
60
  end
46
61
 
47
62
  it "creates 'cached_search_by_client_guid_and_derp_and_user_guid'" do
48
- SearchMethodClass.must_respond_to("cached_search_by_client_guid_and_derp_and_user_guid")
63
+ expect(SearchMethodClass).to respond_to(:cached_search_by_client_guid_and_derp_and_user_guid)
49
64
  end
50
65
 
51
66
  it "creates 'cached_search_by_client_guid_and_user_guid_and_derp'" do
52
- SearchMethodClass.must_respond_to("cached_search_by_client_guid_and_user_guid_and_derp")
67
+ expect(SearchMethodClass).to respond_to(:cached_search_by_client_guid_and_user_guid_and_derp)
53
68
  end
54
69
 
55
70
  it "creates 'cached_search_by_client_guid_and_user_guid_and_derp!'" do
56
- SearchMethodClass.must_respond_to("cached_search_by_client_guid_and_user_guid_and_derp!")
71
+ expect(SearchMethodClass).to respond_to(:cached_search_by_client_guid_and_user_guid_and_derp!)
72
+ end
73
+
74
+ # Ensure it works with numbers in method name
75
+ it "creates 'cached_search_by_alpha_and_zippy123'" do
76
+ expect(SearchMethodClass).to respond_to(:cached_search_by_alpha_and_zippy123)
77
+ end
78
+
79
+ # Ensure it responds to methods where params are not sorted
80
+ # i.e. the method missing override is used.
81
+ it "responds to 'cached_search_by_zippy123_and_alpha'" do
82
+ expect(SearchMethodClass).to respond_to(:cached_search_by_zippy123_and_alpha)
83
+ end
84
+
85
+ # Ensure the right method actually gets called when
86
+ # method_missing override is used.
87
+ it "can call 'cached_search_by_zippy123_and_alpha' when not defined" do
88
+ expect(SearchMethodClass).to receive(:search)
89
+ SearchMethodClass.cached_search_by_zippy123_and_alpha('abc', '123')
57
90
  end
58
91
  end
59
92
 
60
- describe "#cached_search_by_guid" do
93
+ describe '#cached_search_by_guid' do
61
94
  before do
62
95
  ::ActiveRemote::Cached.cache(HashCache.new)
63
96
  ::ActiveRemote::Cached.default_options(:expires_in => 100)
@@ -67,108 +100,105 @@ describe SearchMethodClass do
67
100
  ::ActiveRemote::Cached.default_options({})
68
101
  end
69
102
 
70
- it "executes the search block when a block is passed" do
71
- SearchMethodClass.stub(:derp, :derp) do
72
- SearchMethodClass.cached_search(:guid => :guid) do
73
- SearchMethodClass.derp
74
- end.must_equal(:derp)
75
- end
103
+ it 'executes the search block when a block is passed' do
104
+ expect(SearchMethodClass).to receive(:derp).and_return(:derp)
105
+ expect(SearchMethodClass.cached_search(:guid => :guid) do
106
+ SearchMethodClass.derp
107
+ end).to eq(:derp)
76
108
  end
77
109
 
78
- it "does not persist empty values by default" do
79
- SearchMethodClass.stub(:derp, []) do
80
- SearchMethodClass.cached_search(:guid => :guid) do
81
- SearchMethodClass.derp
82
- end
110
+ it 'does not persist empty values by default' do
111
+ expect(SearchMethodClass).to receive(:derp).and_return([])
83
112
 
84
- SearchMethodClass.cached_exist_search_by_guid?(:guid).must_equal(false)
113
+ SearchMethodClass.cached_search(:guid => :guid) do
114
+ SearchMethodClass.derp
85
115
  end
86
- end
87
116
 
88
- it "persists empty values when allow_empty sent" do
89
- SearchMethodClass.stub(:derp, []) do
90
- SearchMethodClass.cached_search({:guid => :guid}, :allow_empty => true) do
91
- SearchMethodClass.derp
92
- end
117
+ expect(SearchMethodClass.cached_exist_search_by_guid?(:guid)).to eq(false)
118
+ end
93
119
 
94
- SearchMethodClass.cached_exist_search_by_guid?(:guid).must_equal(true)
120
+ it 'persists empty values when allow_empty sent' do
121
+ expect(SearchMethodClass).to receive(:derp).and_return([])
122
+ SearchMethodClass.cached_search({ :guid => :guid }, :allow_empty => true) do
123
+ SearchMethodClass.derp
95
124
  end
96
- end
97
125
 
98
- it "does not persist nil values by default" do
99
- SearchMethodClass.stub(:derp, nil) do
100
- SearchMethodClass.cached_search(:guid => :guid) do
101
- SearchMethodClass.derp
102
- end
126
+ expect(SearchMethodClass.cached_exist_search_by_guid?(:guid)).to eq(true)
127
+ end
103
128
 
104
- SearchMethodClass.cached_exist_search_by_guid?(:guid).must_equal(false)
129
+ it 'does not persist nil values by default' do
130
+ expect(SearchMethodClass).to receive(:derp).and_return(nil)
131
+ SearchMethodClass.cached_search(:guid => :guid) do
132
+ SearchMethodClass.derp
105
133
  end
106
- end
107
134
 
108
- it "persists nil values when allow_nil sent" do
109
- SearchMethodClass.stub(:derp, nil) do
110
- SearchMethodClass.cached_search({:guid => :guid}, :allow_nil => true) do
111
- SearchMethodClass.derp
112
- end
135
+ expect(SearchMethodClass.cached_exist_search_by_guid?(:guid)).to eq(false)
136
+ end
113
137
 
114
- SearchMethodClass.cached_exist_search_by_guid?(:guid).must_equal(true)
138
+ it 'persists nil values when allow_nil sent' do
139
+ expect(SearchMethodClass).to receive(:derp).and_return(nil)
140
+ SearchMethodClass.cached_search({ :guid => :guid }, :allow_nil => true) do
141
+ SearchMethodClass.derp
115
142
  end
116
- end
117
143
 
118
- it "does persist non nil values" do
119
- SearchMethodClass.stub(:derp, :derp) do
120
- SearchMethodClass.cached_search(:guid => :guid) do
121
- SearchMethodClass.derp
122
- end
144
+ expect(SearchMethodClass.cached_exist_search_by_guid?(:guid)).to eq(true)
145
+ end
123
146
 
124
- SearchMethodClass.cached_exist_search_by_guid?(:guid).must_equal(true)
147
+ it 'does persist non nil values' do
148
+ expect(SearchMethodClass).to receive(:derp).and_return(:derp)
149
+ SearchMethodClass.cached_search(:guid => :guid) do
150
+ SearchMethodClass.derp
125
151
  end
152
+
153
+ expect(SearchMethodClass.cached_exist_search_by_guid?(:guid)).to eq(true)
126
154
  end
127
155
 
128
- it "executes search_by_guid when cached_search with guid called" do
129
- FindMethodClass.stub(:search, :hello) do
130
- FindMethodClass.cached_search(:guid => :guid).must_equal(:hello)
131
- end
156
+ it 'executes search_by_guid when cached_search with guid called' do
157
+ expect(SearchMethodClass).to receive(:search).and_return(:hello)
158
+
159
+ expect(SearchMethodClass.cached_search(:guid => :guid)).to eq(:hello)
132
160
  end
133
161
 
134
- it "executes the fetch block if not present in cache" do
135
- SearchMethodClass.stub(:search, :hello) do
136
- SearchMethodClass.cached_search_by_guid(:guid).must_equal(:hello)
137
- end
162
+ it 'executes the fetch block if not present in cache' do
163
+ expect(SearchMethodClass).to receive(:search).and_return(:hello)
164
+ expect(SearchMethodClass.cached_search_by_guid(:guid)).to eq(:hello)
138
165
  end
139
166
 
140
- it "merges the default options in for the fetch call" do
141
- ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", "guid"], :expires_in => 100).returns(:hello)
167
+ it 'merges the default options in for the fetch call' do
168
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
169
+ [versioned_prefix, SearchMethodClass.name, '#search', 'guid'], { :expires_in => 100 }
170
+ ).and_return(:hello)
142
171
 
143
- SearchMethodClass.stub(:search, :hello) do
144
- SearchMethodClass.cached_search_by_guid(:guid).must_equal(:hello)
145
- end
172
+ expect(SearchMethodClass).not_to receive(:search)
173
+ expect(SearchMethodClass.cached_search_by_guid(:guid)).to eq(:hello)
146
174
  end
147
175
 
148
- it "overrides the default options with local options for the fetch call" do
149
- ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", "guid"], :expires_in => 200).returns(:hello)
176
+ it 'overrides the default options with local options for the fetch call' do
177
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
178
+ [versioned_prefix, SearchMethodClass.name, '#search', 'guid'], { :expires_in => 200 }
179
+ ).and_return(:hello)
150
180
 
151
- SearchMethodClass.stub(:search, :hello) do
152
- SearchMethodClass.cached_search_by_guid(:guid, :expires_in => 200).must_equal(:hello)
153
- end
181
+ expect(SearchMethodClass).not_to receive(:search)
182
+ expect(SearchMethodClass.cached_search_by_guid(:guid, { :expires_in => 200 })).to eq(:hello)
154
183
  end
155
184
 
156
- describe "namespaced cache" do
185
+ describe 'namespaced cache' do
157
186
  before do
158
- ::ActiveRemote::Cached.default_options(:expires_in => 100, :namespace => "MyApp")
187
+ ::ActiveRemote::Cached.default_options(:expires_in => 100, :namespace => 'MyApp')
159
188
  end
160
189
 
161
- it "uses the namespace as a prefix to the cache key" do
162
- ::ActiveRemote::Cached.cache.expects(:fetch).with(["MyApp", SearchMethodClass.name, "#search", "guid"], :expires_in => 100).returns(:hello)
190
+ it 'uses the namespace as a prefix to the cache key' do
191
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
192
+ [versioned_prefix, 'MyApp', SearchMethodClass.name, '#search', 'guid'], { :expires_in => 100 }
193
+ ).and_return(:hello)
163
194
 
164
- SearchMethodClass.stub(:search, :hello) do
165
- SearchMethodClass.cached_search_by_guid(:guid)
166
- end
195
+ expect(SearchMethodClass).not_to receive(:search)
196
+ SearchMethodClass.cached_search_by_guid(:guid)
167
197
  end
168
198
  end
169
199
  end
170
200
 
171
- describe "#cached_search_by_foo" do
201
+ describe '#cached_search_by_foo' do
172
202
  before do
173
203
  ::ActiveRemote::Cached.cache(HashCache.new)
174
204
  ::ActiveRemote::Cached.default_options(:expires_in => 100)
@@ -178,24 +208,26 @@ describe SearchMethodClass do
178
208
  ::ActiveRemote::Cached.default_options({})
179
209
  end
180
210
 
181
- it "overrides the default options with cached_finder options for the fetch call" do
182
- ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", "foo"], :expires_in => 500).returns(:hello)
211
+ it 'overrides the default options with cached_finder options for the fetch call' do
212
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
213
+ [versioned_prefix, SearchMethodClass.name, '#search', 'foo'], { :expires_in => 500 }
214
+ ).and_return(:hello)
183
215
 
184
- SearchMethodClass.stub(:find, :hello) do
185
- SearchMethodClass.cached_search_by_foo(:foo).must_equal(:hello)
186
- end
216
+ expect(SearchMethodClass).not_to receive(:find)
217
+ expect(SearchMethodClass.cached_search_by_foo(:foo)).to eq(:hello)
187
218
  end
188
219
 
189
- it "overrides the cached_finder options with local options for the fetch call" do
190
- ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", "foo"], :expires_in => 200).returns(:hello)
220
+ it 'overrides the cached_finder options with local options for the fetch call' do
221
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
222
+ [versioned_prefix, SearchMethodClass.name, '#search', 'foo'], { :expires_in => 200 }
223
+ ).and_return(:hello)
191
224
 
192
- SearchMethodClass.stub(:find, :hello) do
193
- SearchMethodClass.cached_search_by_foo(:foo, :expires_in => 200).must_equal(:hello)
194
- end
225
+ expect(SearchMethodClass).not_to receive(:find)
226
+ expect(SearchMethodClass.cached_search_by_foo(:foo, :expires_in => 200)).to eq(:hello)
195
227
  end
196
228
  end
197
229
 
198
- describe "#cached_search_by_foo!" do
230
+ describe '#cached_search_by_foo!' do
199
231
  before do
200
232
  ::ActiveRemote::Cached.cache(HashCache.new)
201
233
  ::ActiveRemote::Cached.default_options(:expires_in => 100)
@@ -205,16 +237,35 @@ describe SearchMethodClass do
205
237
  ::ActiveRemote::Cached.default_options({})
206
238
  end
207
239
 
208
- it "returns results when present" do
209
- SearchMethodClass.stub(:search, [:hello]) do
210
- SearchMethodClass.cached_search_by_foo!(:foo, :expires_in => 200).must_equal([:hello])
211
- end
240
+ it 'and_return results when present' do
241
+ expect(SearchMethodClass).to receive(:search).and_return([:hello])
242
+ expect(SearchMethodClass.cached_search_by_foo!(:foo, :expires_in => 200)).to eq([:hello])
212
243
  end
213
244
 
214
- it "raises ActiveRemote::RemoteRecordNotFound when not found" do
215
- SearchMethodClass.stub(:search, []) do
216
- -> { SearchMethodClass.cached_search_by_foo!(:foo, :expires_in => 200) }.must_raise ::ActiveRemote::RemoteRecordNotFound
217
- end
245
+ it 'raises ActiveRemote::RemoteRecordNotFound when not found' do
246
+ expect(SearchMethodClass).to receive(:search).and_return([])
247
+ expect do
248
+ SearchMethodClass.cached_search_by_foo!(:foo, :expires_in => 200)
249
+ end.to raise_error ::ActiveRemote::RemoteRecordNotFound
250
+ end
251
+ end
252
+
253
+ # This tests the method_missing override
254
+ describe '#cached_search_by_zippy123_and_alpha' do
255
+ before do
256
+ ::ActiveRemote::Cached.cache(HashCache.new)
257
+ ::ActiveRemote::Cached.default_options(:expires_in => 100)
258
+ end
259
+
260
+ after do
261
+ ::ActiveRemote::Cached.default_options({})
262
+ end
263
+
264
+ it 'calls the underlying method with params in correct order' do
265
+ expect(SearchMethodClass).to receive(:cached_search_by_alpha_and_zippy123).with('123', 'abc').and_return(:hello)
266
+
267
+ # Calls method that does not exist (different param order)
268
+ expect(SearchMethodClass.cached_search_by_zippy123_and_alpha('abc', '123')).to eq(:hello)
218
269
  end
219
270
  end
220
271
  end
data/spec/spec_helper.rb CHANGED
@@ -1,21 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rubygems'
2
4
  require 'bundler'
3
5
  Bundler.require(:default, :development, :test)
4
6
 
5
- require 'minitest/mock'
6
- require 'minitest/spec'
7
- require 'minitest/autorun'
8
- require 'minitest/pride'
9
-
10
7
  class HashCache < Hash
11
8
  def exist?(key)
12
- self.has_key?(key)
9
+ key?(key)
13
10
  end
14
11
 
15
- def fetch(key, options = {}, &blk)
16
- if self.has_key?(key)
17
- return self[key]
18
- end
12
+ def fetch(key, _options = {})
13
+ return self[key] if key?(key)
19
14
 
20
15
  self[key] = yield
21
16
  end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_remote-cached
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Dewitt
8
- autorequire:
8
+ - MXDevExperience
9
+ autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2020-03-19 00:00:00.000000000 Z
12
+ date: 2024-06-10 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: active_remote
@@ -16,14 +17,14 @@ dependencies:
16
17
  requirements:
17
18
  - - ">="
18
19
  - !ruby/object:Gem::Version
19
- version: '0'
20
+ version: '6.1'
20
21
  type: :runtime
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
25
  - - ">="
25
26
  - !ruby/object:Gem::Version
26
- version: '0'
27
+ version: '6.1'
27
28
  - !ruby/object:Gem::Dependency
28
29
  name: activesupport
29
30
  requirement: !ruby/object:Gem::Requirement
@@ -94,15 +95,45 @@ dependencies:
94
95
  - - ">="
95
96
  - !ruby/object:Gem::Version
96
97
  version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rspec
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '3.0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '3.0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: rubocop
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
97
126
  description: ' Provides "cached" finders and a DSL to enumerate which finders should
98
127
  have cached versions '
99
128
  email:
100
129
  - brandonsdewitt@gmail.com
130
+ - devexperience@mx.com
101
131
  executables: []
102
132
  extensions: []
103
133
  extra_rdoc_files: []
104
134
  files:
105
135
  - ".gitignore"
136
+ - ".rubocop.yml"
106
137
  - Gemfile
107
138
  - LICENSE.txt
108
139
  - README.md
@@ -124,7 +155,7 @@ files:
124
155
  homepage: ''
125
156
  licenses: []
126
157
  metadata: {}
127
- post_install_message:
158
+ post_install_message:
128
159
  rdoc_options: []
129
160
  require_paths:
130
161
  - lib
@@ -132,18 +163,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
163
  requirements:
133
164
  - - ">="
134
165
  - !ruby/object:Gem::Version
135
- version: '0'
166
+ version: '2.6'
136
167
  required_rubygems_version: !ruby/object:Gem::Requirement
137
168
  requirements:
138
169
  - - ">="
139
170
  - !ruby/object:Gem::Version
140
171
  version: '0'
141
172
  requirements: []
142
- rubygems_version: 3.0.1
143
- signing_key:
173
+ rubygems_version: 3.1.6
174
+ signing_key:
144
175
  specification_version: 4
145
176
  summary: Provides a configuration for caching mechanisms and finders on ActiveRemote
146
- models that are cached/cacheable
177
+ models
147
178
  test_files:
148
179
  - spec/active_remote/cached/argument_keys_spec.rb
149
180
  - spec/active_remote/cached/cache_spec.rb