active_remote-cached 1.0.0 → 1.2.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.
@@ -1,130 +1,208 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  class ExistMethodClass
4
6
  include ::ActiveRemote::Cached
5
7
 
6
- def self.find; nil; end
7
- def self.search; nil; end
8
+ def self.find(*)
9
+ :record
10
+ end
11
+
12
+ def self.search(*)
13
+ [:record]
14
+ end
8
15
 
9
16
  cached_finders_for :guid
10
17
  cached_finders_for :guid, :user_guid
11
- cached_finders_for [:user_guid, :client_guid]
12
- cached_finders_for [:derp, :user_guid, :client_guid]
18
+ cached_finders_for %i[user_guid client_guid]
19
+ cached_finders_for %i[derp user_guid client_guid]
13
20
  end
14
21
 
15
22
  describe ExistMethodClass do
16
- describe "API" do
23
+ describe 'API' do
17
24
  it "creates 'cached_exist_find_by_guid'" do
18
- ExistMethodClass.must_respond_to("cached_exist_find_by_guid")
25
+ expect(ExistMethodClass).to respond_to(:cached_exist_find_by_guid)
19
26
  end
20
27
 
21
28
  it "creates 'cached_exist_search_by_guid'" do
22
- ExistMethodClass.must_respond_to("cached_exist_search_by_guid")
29
+ expect(ExistMethodClass).to respond_to(:cached_exist_search_by_guid)
23
30
  end
24
31
 
25
32
  it "creates 'cached_exist_find_by_user_guid'" do
26
- ExistMethodClass.must_respond_to("cached_exist_find_by_user_guid")
33
+ expect(ExistMethodClass).to respond_to(:cached_exist_find_by_user_guid)
27
34
  end
28
35
 
29
36
  it "creates 'cached_exist_search_by_user_guid'" do
30
- ExistMethodClass.must_respond_to("cached_exist_search_by_user_guid")
37
+ expect(ExistMethodClass).to respond_to(:cached_exist_search_by_user_guid)
31
38
  end
32
39
 
33
40
  it "creates 'cached_exist_find_by_user_guid_and_client_guid'" do
34
- ExistMethodClass.must_respond_to("cached_exist_find_by_user_guid_and_client_guid")
41
+ expect(ExistMethodClass).to respond_to(:cached_exist_find_by_user_guid_and_client_guid)
35
42
  end
36
43
 
37
44
  it "creates 'cached_exist_search_by_user_guid_and_client_guid'" do
38
- ExistMethodClass.must_respond_to("cached_exist_search_by_user_guid_and_client_guid")
45
+ expect(ExistMethodClass).to respond_to(:cached_exist_search_by_user_guid_and_client_guid)
39
46
  end
40
47
 
41
48
  it "creates 'cached_exist_find_by_client_guid_and_user_guid'" do
42
- ExistMethodClass.must_respond_to("cached_exist_find_by_client_guid_and_user_guid")
49
+ expect(ExistMethodClass).to respond_to(:cached_exist_find_by_client_guid_and_user_guid)
43
50
  end
44
51
 
45
52
  it "creates 'cached_exist_search_by_client_guid_and_user_guid'" do
46
- ExistMethodClass.must_respond_to("cached_exist_search_by_client_guid_and_user_guid")
53
+ expect(ExistMethodClass).to respond_to(:cached_exist_search_by_client_guid_and_user_guid)
47
54
  end
48
55
 
49
56
  it "creates 'cached_exist_find_by_derp_and_user_guid_and_client_guid'" do
50
- ExistMethodClass.must_respond_to("cached_exist_find_by_derp_and_user_guid_and_client_guid")
57
+ expect(ExistMethodClass).to respond_to(:cached_exist_find_by_derp_and_user_guid_and_client_guid)
51
58
  end
52
59
 
53
60
  it "creates 'cached_exist_search_by_derp_and_user_guid_and_client_guid'" do
54
- ExistMethodClass.must_respond_to("cached_exist_search_by_derp_and_user_guid_and_client_guid")
61
+ expect(ExistMethodClass).to respond_to(:cached_exist_search_by_derp_and_user_guid_and_client_guid)
55
62
  end
56
63
 
57
64
  it "creates 'cached_exist_find_by_client_guid_and_derp_and_user_guid'" do
58
- ExistMethodClass.must_respond_to("cached_exist_find_by_client_guid_and_derp_and_user_guid")
65
+ expect(ExistMethodClass).to respond_to(:cached_exist_find_by_client_guid_and_derp_and_user_guid)
59
66
  end
60
67
 
61
68
  it "creates 'cached_exist_search_by_client_guid_and_derp_and_user_guid'" do
62
- ExistMethodClass.must_respond_to("cached_exist_search_by_client_guid_and_derp_and_user_guid")
69
+ expect(ExistMethodClass).to respond_to(:cached_exist_search_by_client_guid_and_derp_and_user_guid)
63
70
  end
64
71
 
65
72
  it "creates 'cached_exist_find_by_client_guid_and_user_guid_and_derp'" do
66
- ExistMethodClass.must_respond_to("cached_exist_find_by_client_guid_and_user_guid_and_derp")
73
+ expect(ExistMethodClass).to respond_to(:cached_exist_find_by_client_guid_and_user_guid_and_derp)
67
74
  end
68
75
 
69
76
  it "creates 'cached_exist_search_by_client_guid_and_user_guid_and_derp'" do
70
- ExistMethodClass.must_respond_to("cached_exist_search_by_client_guid_and_user_guid_and_derp")
77
+ expect(ExistMethodClass).to respond_to(:cached_exist_search_by_client_guid_and_user_guid_and_derp)
71
78
  end
72
79
 
73
80
  # ? based methods
74
81
  it "creates 'cached_exist_find_by_guid?'" do
75
- ExistMethodClass.must_respond_to("cached_exist_find_by_guid?")
82
+ expect(ExistMethodClass).to respond_to(:cached_exist_find_by_guid?)
76
83
  end
77
84
 
78
85
  it "creates 'cached_exist_search_by_guid?'" do
79
- ExistMethodClass.must_respond_to("cached_exist_search_by_guid?")
86
+ expect(ExistMethodClass).to respond_to(:cached_exist_search_by_guid?)
80
87
  end
81
88
 
82
89
  it "creates 'cached_exist_find_by_user_guid?'" do
83
- ExistMethodClass.must_respond_to("cached_exist_find_by_user_guid?")
90
+ expect(ExistMethodClass).to respond_to(:cached_exist_find_by_user_guid?)
84
91
  end
85
92
 
86
93
  it "creates 'cached_exist_search_by_user_guid?'" do
87
- ExistMethodClass.must_respond_to("cached_exist_search_by_user_guid?")
94
+ expect(ExistMethodClass).to respond_to(:cached_exist_search_by_user_guid?)
88
95
  end
89
96
 
90
97
  it "creates 'cached_exist_find_by_user_guid_and_client_guid?'" do
91
- ExistMethodClass.must_respond_to("cached_exist_find_by_user_guid_and_client_guid?")
98
+ expect(ExistMethodClass).to respond_to(:cached_exist_find_by_user_guid_and_client_guid?)
92
99
  end
93
100
 
94
101
  it "creates 'cached_exist_search_by_user_guid_and_client_guid?'" do
95
- ExistMethodClass.must_respond_to("cached_exist_search_by_user_guid_and_client_guid?")
102
+ expect(ExistMethodClass).to respond_to(:cached_exist_search_by_user_guid_and_client_guid?)
96
103
  end
97
104
 
98
105
  it "creates 'cached_exist_find_by_client_guid_and_user_guid?'" do
99
- ExistMethodClass.must_respond_to("cached_exist_find_by_client_guid_and_user_guid?")
106
+ expect(ExistMethodClass).to respond_to(:cached_exist_find_by_client_guid_and_user_guid?)
100
107
  end
101
108
 
102
109
  it "creates 'cached_exist_search_by_client_guid_and_user_guid?'" do
103
- ExistMethodClass.must_respond_to("cached_exist_search_by_client_guid_and_user_guid?")
110
+ expect(ExistMethodClass).to respond_to(:cached_exist_search_by_client_guid_and_user_guid?)
104
111
  end
105
112
 
106
113
  it "creates 'cached_exist_find_by_derp_and_user_guid_and_client_guid?'" do
107
- ExistMethodClass.must_respond_to("cached_exist_find_by_derp_and_user_guid_and_client_guid?")
114
+ expect(ExistMethodClass).to respond_to(:cached_exist_find_by_derp_and_user_guid_and_client_guid?)
108
115
  end
109
116
 
110
117
  it "creates 'cached_exist_search_by_derp_and_user_guid_and_client_guid?'" do
111
- ExistMethodClass.must_respond_to("cached_exist_search_by_derp_and_user_guid_and_client_guid?")
118
+ expect(ExistMethodClass).to respond_to(:cached_exist_search_by_derp_and_user_guid_and_client_guid?)
112
119
  end
113
120
 
114
121
  it "creates 'cached_exist_find_by_client_guid_and_derp_and_user_guid?'" do
115
- ExistMethodClass.must_respond_to("cached_exist_find_by_client_guid_and_derp_and_user_guid?")
122
+ expect(ExistMethodClass).to respond_to(:cached_exist_find_by_client_guid_and_derp_and_user_guid?)
116
123
  end
117
124
 
118
125
  it "creates 'cached_exist_search_by_client_guid_and_derp_and_user_guid?'" do
119
- ExistMethodClass.must_respond_to("cached_exist_search_by_client_guid_and_derp_and_user_guid?")
126
+ expect(ExistMethodClass).to respond_to(:cached_exist_search_by_client_guid_and_derp_and_user_guid?)
120
127
  end
121
128
 
122
129
  it "creates 'cached_exist_find_by_client_guid_and_user_guid_and_derp?'" do
123
- ExistMethodClass.must_respond_to("cached_exist_find_by_client_guid_and_user_guid_and_derp?")
130
+ expect(ExistMethodClass).to respond_to(:cached_exist_find_by_client_guid_and_user_guid_and_derp?)
124
131
  end
125
132
 
126
133
  it "creates 'cached_exist_search_by_client_guid_and_user_guid_and_derp?'" do
127
- ExistMethodClass.must_respond_to("cached_exist_search_by_client_guid_and_user_guid_and_derp?")
134
+ expect(ExistMethodClass).to respond_to(:cached_exist_search_by_client_guid_and_user_guid_and_derp?)
135
+ end
136
+ end
137
+
138
+ describe '#cached_exist_find_by_guid?' do
139
+ before do
140
+ ::ActiveRemote::Cached.cache(HashCache.new)
141
+ end
142
+
143
+ after do
144
+ ::ActiveRemote::Cached.default_options({})
145
+ end
146
+
147
+ it 'returns false before the find cache key is written' do
148
+ expect(ExistMethodClass.cached_exist_find_by_guid?(:guid)).to eq(false)
149
+ end
150
+
151
+ it 'returns true after the find cache key is written' do
152
+ ExistMethodClass.cached_find_by_guid(:guid)
153
+
154
+ expect(ExistMethodClass.cached_exist_find_by_guid?(:guid)).to eq(true)
155
+ end
156
+
157
+ it 'returns the same result as the method without the question mark' do
158
+ ExistMethodClass.cached_find_by_guid(:guid)
159
+
160
+ expect(ExistMethodClass.cached_exist_find_by_guid(:guid)).to eq(
161
+ ExistMethodClass.cached_exist_find_by_guid?(:guid)
162
+ )
163
+ end
164
+
165
+ it 'ignores a search cache key for the same arguments' do
166
+ ExistMethodClass.cached_search_by_guid(:guid)
167
+
168
+ expect(ExistMethodClass.cached_exist_find_by_guid?(:guid)).to eq(false)
169
+ end
170
+
171
+ describe 'namespaced cache' do
172
+ it 'uses the namespace as a prefix to the cache key' do
173
+ expect(::ActiveRemote::Cached.cache).to receive(:exist?).with(
174
+ [::ActiveRemote::Cached::RUBY_AND_ACTIVE_SUPPORT_VERSION, 'MyApp', ExistMethodClass.name, '#find',
175
+ 'guid.guid']
176
+ ).and_return(true)
177
+
178
+ expect(ExistMethodClass.cached_exist_find_by_guid?(:guid, :namespace => 'MyApp')).to eq(true)
179
+ end
180
+ end
181
+ end
182
+
183
+ describe '#cached_exist_search_by_guid?' do
184
+ before do
185
+ ::ActiveRemote::Cached.cache(HashCache.new)
186
+ end
187
+
188
+ after do
189
+ ::ActiveRemote::Cached.default_options({})
190
+ end
191
+
192
+ it 'returns false before the search cache key is written' do
193
+ expect(ExistMethodClass.cached_exist_search_by_guid?(:guid)).to eq(false)
194
+ end
195
+
196
+ it 'returns true after the search cache key is written' do
197
+ ExistMethodClass.cached_search_by_guid(:guid)
198
+
199
+ expect(ExistMethodClass.cached_exist_search_by_guid?(:guid)).to eq(true)
200
+ end
201
+
202
+ it 'ignores a find cache key for the same arguments' do
203
+ ExistMethodClass.cached_find_by_guid(:guid)
204
+
205
+ expect(ExistMethodClass.cached_exist_search_by_guid?(:guid)).to eq(false)
128
206
  end
129
207
  end
130
208
  end
@@ -1,54 +1,63 @@
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; nil; end
7
- def self.search; nil; end
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 [:user_guid, :client_guid]
13
- cached_finders_for [:derp, :user_guid, :client_guid]
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
- describe "API" do
24
+ let(:versioned_prefix) { ::ActiveRemote::Cached::RUBY_AND_ACTIVE_SUPPORT_VERSION }
25
+
26
+ describe 'API' do
18
27
  it "creates 'cached_find_by_foo'" do
19
- FindMethodClass.must_respond_to("cached_find_by_foo")
28
+ expect(FindMethodClass).to respond_to(:cached_find_by_foo)
20
29
  end
21
30
 
22
31
  it "creates 'cached_find_by_guid'" do
23
- FindMethodClass.must_respond_to("cached_find_by_guid")
32
+ expect(FindMethodClass).to respond_to(:cached_find_by_guid)
24
33
  end
25
34
 
26
35
  it "creates 'cached_find_by_user_guid'" do
27
- FindMethodClass.must_respond_to("cached_find_by_user_guid")
36
+ expect(FindMethodClass).to respond_to(:cached_find_by_user_guid)
28
37
  end
29
38
 
30
39
  it "creates 'cached_find_by_user_guid_and_client_guid'" do
31
- FindMethodClass.must_respond_to("cached_find_by_user_guid_and_client_guid")
40
+ expect(FindMethodClass).to respond_to(:cached_find_by_user_guid_and_client_guid)
32
41
  end
33
42
 
34
43
  it "creates 'cached_find_by_client_guid_and_user_guid'" do
35
- FindMethodClass.must_respond_to("cached_find_by_client_guid_and_user_guid")
44
+ expect(FindMethodClass).to respond_to(:cached_find_by_client_guid_and_user_guid)
36
45
  end
37
46
 
38
47
  it "creates 'cached_find_by_derp_and_user_guid_and_client_guid'" do
39
- FindMethodClass.must_respond_to("cached_find_by_derp_and_user_guid_and_client_guid")
48
+ expect(FindMethodClass).to respond_to(:cached_find_by_derp_and_user_guid_and_client_guid)
40
49
  end
41
50
 
42
51
  it "creates 'cached_find_by_client_guid_and_derp_and_user_guid'" do
43
- FindMethodClass.must_respond_to("cached_find_by_client_guid_and_derp_and_user_guid")
52
+ expect(FindMethodClass).to respond_to(:cached_find_by_client_guid_and_derp_and_user_guid)
44
53
  end
45
54
 
46
55
  it "creates 'cached_find_by_client_guid_and_user_guid_and_derp'" do
47
- FindMethodClass.must_respond_to("cached_find_by_client_guid_and_user_guid_and_derp")
56
+ expect(FindMethodClass).to respond_to(:cached_find_by_client_guid_and_user_guid_and_derp)
48
57
  end
49
58
  end
50
59
 
51
- describe "#cached_find_by_guid" do
60
+ describe '#cached_find_by_guid' do
52
61
  before do
53
62
  ::ActiveRemote::Cached.cache(HashCache.new)
54
63
  ::ActiveRemote::Cached.default_options(:expires_in => 100)
@@ -58,50 +67,50 @@ describe FindMethodClass do
58
67
  ::ActiveRemote::Cached.default_options({})
59
68
  end
60
69
 
61
- it "executes find_by_guid when cached_find with guid called" do
62
- FindMethodClass.stub(:find, :hello) do
63
- FindMethodClass.cached_find(:guid => :guid).must_equal(:hello)
64
- end
70
+ it 'executes find_by_guid when cached_find with guid called' do
71
+ expect(FindMethodClass).to receive(:find).and_return(:hello)
72
+ expect(FindMethodClass.cached_find(:guid => :guid)).to eq(:hello)
65
73
  end
66
74
 
67
- it "executes the fetch block if not present in cache" do
68
- FindMethodClass.stub(:find, :hello) do
69
- FindMethodClass.cached_find_by_guid(:guid).must_equal(:hello)
70
- end
75
+ it 'executes the fetch block if not present in cache' do
76
+ expect(FindMethodClass).to receive(:find).and_return(:hello)
77
+ expect(FindMethodClass.cached_find_by_guid(:guid)).to eq(:hello)
71
78
  end
72
79
 
73
- it "merges the default options in for the fetch call" do
74
- ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", "guid"], :expires_in => 100).returns(:hello)
75
-
76
- FindMethodClass.stub(:find, :hello) do
77
- FindMethodClass.cached_find_by_guid(:guid).must_equal(:hello)
78
- end
80
+ it 'merges the default options in for the fetch call' do
81
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
82
+ [versioned_prefix, FindMethodClass.name, '#find', 'guid.guid'], { :expires_in => 100 }
83
+ ).and_return(:hello)
84
+ expect(FindMethodClass).not_to receive(:find)
85
+ expect(FindMethodClass.cached_find_by_guid(:guid)).to eq(:hello)
79
86
  end
80
87
 
81
- it "overrides the default options with local options for the fetch call" do
82
- ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", "guid"], :expires_in => 200).returns(:hello)
88
+ it 'overrides the default options with local options for the fetch call' do
89
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
90
+ [versioned_prefix, FindMethodClass.name, '#find', 'guid.guid'], { :expires_in => 200 }
91
+ ).and_return(:hello)
92
+ expect(FindMethodClass).not_to receive(:find)
83
93
 
84
- FindMethodClass.stub(:find, :hello) do
85
- FindMethodClass.cached_find_by_guid(:guid, :expires_in => 200).must_equal(:hello)
86
- end
94
+ expect(FindMethodClass.cached_find_by_guid(:guid, :expires_in => 200)).to eq(:hello)
87
95
  end
88
96
 
89
- describe "namespaced cache" do
97
+ describe 'namespaced cache' do
90
98
  before do
91
- ::ActiveRemote::Cached.default_options(:expires_in => 100, :namespace => "MyApp")
99
+ ::ActiveRemote::Cached.default_options(:expires_in => 100, :namespace => 'MyApp')
92
100
  end
93
101
 
94
- it "uses the namespace as a prefix to the cache key" do
95
- ::ActiveRemote::Cached.cache.expects(:fetch).with(["MyApp", FindMethodClass.name, "#find", "guid"], :expires_in => 100).returns(:hello)
102
+ it 'uses the namespace as a prefix to the cache key' do
103
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
104
+ [versioned_prefix, 'MyApp', FindMethodClass.name, '#find', 'guid.guid'], { :expires_in => 100 }
105
+ ).and_return(:hello)
96
106
 
97
- FindMethodClass.stub(:find, :hello) do
98
- FindMethodClass.cached_find_by_guid(:guid)
99
- end
107
+ expect(FindMethodClass).not_to receive(:find)
108
+ FindMethodClass.cached_find_by_guid(:guid)
100
109
  end
101
110
  end
102
111
  end
103
112
 
104
- describe "#cached_find_by_foo" do
113
+ describe '#cached_find_by_foo' do
105
114
  before do
106
115
  ::ActiveRemote::Cached.cache(HashCache.new)
107
116
  ::ActiveRemote::Cached.default_options(:expires_in => 100)
@@ -111,20 +120,22 @@ describe FindMethodClass do
111
120
  ::ActiveRemote::Cached.default_options({})
112
121
  end
113
122
 
114
- it "overrides the default options with cached_finder options for the fetch call" do
115
- ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", "foo"], :expires_in => 500).returns(:hello)
123
+ it 'overrides the default options with cached_finder options for the fetch call' do
124
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
125
+ [versioned_prefix, FindMethodClass.name, '#find', 'foo.foo'], { :expires_in => 500 }
126
+ ).and_return(:hello)
116
127
 
117
- FindMethodClass.stub(:find, :hello) do
118
- FindMethodClass.cached_find_by_foo(:foo).must_equal(:hello)
119
- end
128
+ expect(FindMethodClass).not_to receive(:find)
129
+ expect(FindMethodClass.cached_find_by_foo(:foo)).to eq(:hello)
120
130
  end
121
131
 
122
- it "overrides the cached_finder options with local options for the fetch call" do
123
- ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", "foo"], :expires_in => 200).returns(:hello)
132
+ it 'overrides the cached_finder options with local options for the fetch call' do
133
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
134
+ [versioned_prefix, FindMethodClass.name, '#find', 'foo.foo'], { :expires_in => 200 }
135
+ ).and_return(:hello)
124
136
 
125
- FindMethodClass.stub(:find, :hello) do
126
- FindMethodClass.cached_find_by_foo(:foo, :expires_in => 200).must_equal(:hello)
127
- end
137
+ expect(FindMethodClass).not_to receive(:find)
138
+ expect(FindMethodClass.cached_find_by_foo(:foo, :expires_in => 200)).to eq(:hello)
128
139
  end
129
140
  end
130
141
  end