active_remote-cached 1.0.0 → 1.1.1
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +55 -0
- data/Gemfile +2 -0
- data/Rakefile +6 -4
- data/active_remote-cached.gemspec +23 -17
- data/lib/active_remote/cached/argument_keys.rb +49 -46
- data/lib/active_remote/cached/cache.rb +50 -49
- data/lib/active_remote/cached/railtie.rb +5 -5
- data/lib/active_remote/cached/version.rb +3 -1
- data/lib/active_remote/cached.rb +57 -61
- data/lib/active_remote-cached.rb +3 -1
- data/spec/active_remote/cached/argument_keys_spec.rb +12 -10
- data/spec/active_remote/cached/cache_spec.rb +13 -16
- data/spec/active_remote/cached_delete_methods_spec.rb +19 -12
- data/spec/active_remote/cached_exist_methods_spec.rb +40 -33
- data/spec/active_remote/cached_find_methods_spec.rb +64 -51
- data/spec/active_remote/cached_search_methods_spec.rb +153 -102
- data/spec/spec_helper.rb +5 -10
- metadata +36 -7
@@ -1,54 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
class FindMethodClass
|
4
6
|
include ::ActiveRemote::Cached
|
5
7
|
|
6
|
-
def self.find
|
7
|
-
|
8
|
+
def self.find
|
9
|
+
nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.search
|
13
|
+
nil
|
14
|
+
end
|
8
15
|
|
9
16
|
cached_finders_for :foo, :expires_in => 500
|
10
17
|
cached_finders_for :guid
|
11
18
|
cached_finders_for :guid, :user_guid
|
12
|
-
cached_finders_for [
|
13
|
-
cached_finders_for [
|
19
|
+
cached_finders_for %i[user_guid client_guid]
|
20
|
+
cached_finders_for %i[derp user_guid client_guid]
|
14
21
|
end
|
15
22
|
|
16
23
|
describe FindMethodClass do
|
17
|
-
|
24
|
+
let(:versioned_prefix) do
|
25
|
+
"#{RUBY_ENGINE_VERSION}:#{ActiveSupport::VERSION::STRING}"
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'API' do
|
18
29
|
it "creates 'cached_find_by_foo'" do
|
19
|
-
FindMethodClass.
|
30
|
+
expect(FindMethodClass).to respond_to(:cached_find_by_foo)
|
20
31
|
end
|
21
32
|
|
22
33
|
it "creates 'cached_find_by_guid'" do
|
23
|
-
FindMethodClass.
|
34
|
+
expect(FindMethodClass).to respond_to(:cached_find_by_guid)
|
24
35
|
end
|
25
36
|
|
26
37
|
it "creates 'cached_find_by_user_guid'" do
|
27
|
-
FindMethodClass.
|
38
|
+
expect(FindMethodClass).to respond_to(:cached_find_by_user_guid)
|
28
39
|
end
|
29
40
|
|
30
41
|
it "creates 'cached_find_by_user_guid_and_client_guid'" do
|
31
|
-
FindMethodClass.
|
42
|
+
expect(FindMethodClass).to respond_to(:cached_find_by_user_guid_and_client_guid)
|
32
43
|
end
|
33
44
|
|
34
45
|
it "creates 'cached_find_by_client_guid_and_user_guid'" do
|
35
|
-
FindMethodClass.
|
46
|
+
expect(FindMethodClass).to respond_to(:cached_find_by_client_guid_and_user_guid)
|
36
47
|
end
|
37
48
|
|
38
49
|
it "creates 'cached_find_by_derp_and_user_guid_and_client_guid'" do
|
39
|
-
FindMethodClass.
|
50
|
+
expect(FindMethodClass).to respond_to(:cached_find_by_derp_and_user_guid_and_client_guid)
|
40
51
|
end
|
41
52
|
|
42
53
|
it "creates 'cached_find_by_client_guid_and_derp_and_user_guid'" do
|
43
|
-
FindMethodClass.
|
54
|
+
expect(FindMethodClass).to respond_to(:cached_find_by_client_guid_and_derp_and_user_guid)
|
44
55
|
end
|
45
56
|
|
46
57
|
it "creates 'cached_find_by_client_guid_and_user_guid_and_derp'" do
|
47
|
-
FindMethodClass.
|
58
|
+
expect(FindMethodClass).to respond_to(:cached_find_by_client_guid_and_user_guid_and_derp)
|
48
59
|
end
|
49
60
|
end
|
50
61
|
|
51
|
-
describe
|
62
|
+
describe '#cached_find_by_guid' do
|
52
63
|
before do
|
53
64
|
::ActiveRemote::Cached.cache(HashCache.new)
|
54
65
|
::ActiveRemote::Cached.default_options(:expires_in => 100)
|
@@ -58,50 +69,50 @@ describe FindMethodClass do
|
|
58
69
|
::ActiveRemote::Cached.default_options({})
|
59
70
|
end
|
60
71
|
|
61
|
-
it
|
62
|
-
FindMethodClass.
|
63
|
-
|
64
|
-
end
|
72
|
+
it 'executes find_by_guid when cached_find with guid called' do
|
73
|
+
expect(FindMethodClass).to receive(:find).and_return(:hello)
|
74
|
+
expect(FindMethodClass.cached_find(:guid => :guid)).to eq(:hello)
|
65
75
|
end
|
66
76
|
|
67
|
-
it
|
68
|
-
FindMethodClass.
|
69
|
-
|
70
|
-
end
|
77
|
+
it 'executes the fetch block if not present in cache' do
|
78
|
+
expect(FindMethodClass).to receive(:find).and_return(:hello)
|
79
|
+
expect(FindMethodClass.cached_find_by_guid(:guid)).to eq(:hello)
|
71
80
|
end
|
72
81
|
|
73
|
-
it
|
74
|
-
::ActiveRemote::Cached.cache.
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
82
|
+
it 'merges the default options in for the fetch call' do
|
83
|
+
expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
|
84
|
+
[versioned_prefix, FindMethodClass.name, '#find', 'guid'], { :expires_in => 100 }
|
85
|
+
).and_return(:hello)
|
86
|
+
expect(FindMethodClass).not_to receive(:find)
|
87
|
+
expect(FindMethodClass.cached_find_by_guid(:guid)).to eq(:hello)
|
79
88
|
end
|
80
89
|
|
81
|
-
it
|
82
|
-
::ActiveRemote::Cached.cache.
|
90
|
+
it 'overrides the default options with local options for the fetch call' do
|
91
|
+
expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
|
92
|
+
[versioned_prefix, FindMethodClass.name, '#find', 'guid'], { :expires_in => 200 }
|
93
|
+
).and_return(:hello)
|
94
|
+
expect(FindMethodClass).not_to receive(:find)
|
83
95
|
|
84
|
-
FindMethodClass.
|
85
|
-
FindMethodClass.cached_find_by_guid(:guid, :expires_in => 200).must_equal(:hello)
|
86
|
-
end
|
96
|
+
expect(FindMethodClass.cached_find_by_guid(:guid, :expires_in => 200)).to eq(:hello)
|
87
97
|
end
|
88
98
|
|
89
|
-
describe
|
99
|
+
describe 'namespaced cache' do
|
90
100
|
before do
|
91
|
-
::ActiveRemote::Cached.default_options(:expires_in => 100, :namespace =>
|
101
|
+
::ActiveRemote::Cached.default_options(:expires_in => 100, :namespace => 'MyApp')
|
92
102
|
end
|
93
103
|
|
94
|
-
it
|
95
|
-
::ActiveRemote::Cached.cache.
|
104
|
+
it 'uses the namespace as a prefix to the cache key' do
|
105
|
+
expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
|
106
|
+
[versioned_prefix, 'MyApp', FindMethodClass.name, '#find', 'guid'], { :expires_in => 100 }
|
107
|
+
).and_return(:hello)
|
96
108
|
|
97
|
-
FindMethodClass.
|
98
|
-
|
99
|
-
end
|
109
|
+
expect(FindMethodClass).not_to receive(:find)
|
110
|
+
FindMethodClass.cached_find_by_guid(:guid)
|
100
111
|
end
|
101
112
|
end
|
102
113
|
end
|
103
114
|
|
104
|
-
describe
|
115
|
+
describe '#cached_find_by_foo' do
|
105
116
|
before do
|
106
117
|
::ActiveRemote::Cached.cache(HashCache.new)
|
107
118
|
::ActiveRemote::Cached.default_options(:expires_in => 100)
|
@@ -111,20 +122,22 @@ describe FindMethodClass do
|
|
111
122
|
::ActiveRemote::Cached.default_options({})
|
112
123
|
end
|
113
124
|
|
114
|
-
it
|
115
|
-
::ActiveRemote::Cached.cache.
|
125
|
+
it 'overrides the default options with cached_finder options for the fetch call' do
|
126
|
+
expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
|
127
|
+
[versioned_prefix, FindMethodClass.name, '#find', 'foo'], { :expires_in => 500 }
|
128
|
+
).and_return(:hello)
|
116
129
|
|
117
|
-
FindMethodClass.
|
118
|
-
|
119
|
-
end
|
130
|
+
expect(FindMethodClass).not_to receive(:find)
|
131
|
+
expect(FindMethodClass.cached_find_by_foo(:foo)).to eq(:hello)
|
120
132
|
end
|
121
133
|
|
122
|
-
it
|
123
|
-
::ActiveRemote::Cached.cache.
|
134
|
+
it 'overrides the cached_finder options with local options for the fetch call' do
|
135
|
+
expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
|
136
|
+
[versioned_prefix, FindMethodClass.name, '#find', 'foo'], { :expires_in => 200 }
|
137
|
+
).and_return(:hello)
|
124
138
|
|
125
|
-
FindMethodClass.
|
126
|
-
|
127
|
-
end
|
139
|
+
expect(FindMethodClass).not_to receive(:find)
|
140
|
+
expect(FindMethodClass.cached_find_by_foo(:foo, :expires_in => 200)).to eq(:hello)
|
128
141
|
end
|
129
142
|
end
|
130
143
|
end
|
@@ -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
|
7
|
-
|
8
|
-
|
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 [
|
14
|
-
cached_finders_for [
|
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
|
-
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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
|
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
|
71
|
-
SearchMethodClass.
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
79
|
-
SearchMethodClass.
|
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
|
-
|
113
|
+
SearchMethodClass.cached_search(:guid => :guid) do
|
114
|
+
SearchMethodClass.derp
|
85
115
|
end
|
86
|
-
end
|
87
116
|
|
88
|
-
|
89
|
-
|
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
|
-
|
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
|
-
|
99
|
-
|
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
|
-
|
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
|
-
|
109
|
-
|
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
|
-
|
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
|
-
|
119
|
-
|
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
|
-
|
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
|
129
|
-
|
130
|
-
|
131
|
-
|
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
|
135
|
-
SearchMethodClass.
|
136
|
-
|
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
|
141
|
-
::ActiveRemote::Cached.cache.
|
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.
|
144
|
-
|
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
|
149
|
-
::ActiveRemote::Cached.cache.
|
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.
|
152
|
-
|
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
|
185
|
+
describe 'namespaced cache' do
|
157
186
|
before do
|
158
|
-
::ActiveRemote::Cached.default_options(:expires_in => 100, :namespace =>
|
187
|
+
::ActiveRemote::Cached.default_options(:expires_in => 100, :namespace => 'MyApp')
|
159
188
|
end
|
160
189
|
|
161
|
-
it
|
162
|
-
::ActiveRemote::Cached.cache.
|
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.
|
165
|
-
|
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
|
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
|
182
|
-
::ActiveRemote::Cached.cache.
|
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.
|
185
|
-
|
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
|
190
|
-
::ActiveRemote::Cached.cache.
|
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.
|
193
|
-
|
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
|
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
|
209
|
-
SearchMethodClass.
|
210
|
-
|
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
|
215
|
-
SearchMethodClass.
|
216
|
-
|
217
|
-
|
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
|
-
|
9
|
+
key?(key)
|
13
10
|
end
|
14
11
|
|
15
|
-
def fetch(key,
|
16
|
-
if
|
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
|