ac-summarization-utils 0.0.47.rc1

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,3 @@
1
+ module ACSummarizationUtils
2
+ VERSION = "0.0.47.rc1"
3
+ end
@@ -0,0 +1,52 @@
1
+ require 'a15-worker'
2
+ require_relative '../lib/ac-summarization-utils/errors_handler'
3
+
4
+ include ACSummarizationUtils
5
+
6
+ describe "Errors Handler#" do
7
+ include A15::Worker
8
+ def mock_producer
9
+ @producer_mock = mock("Producer")
10
+ Producer.stub(:new).with(any_args()).and_return(@producer_mock)
11
+ end
12
+
13
+ def mock_logger
14
+ @log_mock = mock("logger")
15
+ @log_mock.stub(:log)
16
+ end
17
+
18
+ class TestQueriesEror < RuntimeError
19
+ attr_accessor :queries
20
+ end
21
+
22
+ it "should send input errors one by one for 2 generated messages" do
23
+ mock_logger
24
+ mock_producer
25
+ @producer_mock.should_receive(:publish).with(any_args()).exactly(2).times
26
+
27
+ target = ErrorsHandler.new("", "", "", "", "")
28
+ target.send_input_errors(@log_mock, [{"1" => "a"}, {"2" => "b"}])
29
+ end
30
+
31
+ it "should generate and send input errors one by one for 2 messages " do
32
+ mock_logger
33
+ mock_producer
34
+ @producer_mock.should_receive(:publish).with(any_args()).exactly(2).times
35
+
36
+ target = ErrorsHandler.new("", "", "", "","")
37
+ target.gen_and_send_input_errors(@log_mock, ["1", "2"], Exception.new("bla"))
38
+
39
+ end
40
+
41
+
42
+ it "should send db error with 2 messages" do
43
+ mock_logger
44
+ mock_producer
45
+ @producer_mock.should_receive(:publish).with(any_args()).exactly(1).times
46
+
47
+ target = ErrorsHandler.new("", "", "", "","")
48
+ target.gen_and_send_db_errors(@log_mock, ["1", "2"], TestQueriesEror.new("bla"))
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,35 @@
1
+ require 'rspec'
2
+ require_relative '../lib/ac-summarization-utils/general_utils'
3
+
4
+ include ACSummarizationUtils
5
+ include ACSummarizationUtils::GeneralUtils
6
+
7
+ describe "General Utils#" do
8
+ context "convert_country_code#" do
9
+
10
+ it "should return 'oth' for unsupported country code" do
11
+ convert_country_code('shuki').should == 'oth'
12
+ convert_country_code('muki').should == 'oth'
13
+ convert_country_code('neverland').should == 'oth'
14
+ end
15
+
16
+ it "should return specific country for country in supported list" do
17
+ convert_country_code('us').should == 'us'
18
+ convert_country_code('gb').should == 'gb'
19
+ convert_country_code('it').should == 'it'
20
+ convert_country_code('fr').should == 'fr'
21
+ end
22
+
23
+ it "should return 'latin_am' for country in SUPPORTED_LATIN_COUNTRIES" do
24
+ convert_country_code('gt').should == 'latin_am'
25
+ convert_country_code('sv').should == 'latin_am'
26
+ convert_country_code('br').should == 'latin_am'
27
+ end
28
+
29
+ it "should return 'f_east' for country in SUPPORTED_FAR_EAST" do
30
+ convert_country_code('jp').should == 'f_east'
31
+ convert_country_code('ph').should == 'f_east'
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,956 @@
1
+ require 'redis'
2
+ require 'mysql2'
3
+ require_relative '../lib/ac-summarization-utils/lookup_cache'
4
+
5
+ include ACSummarizationUtils
6
+
7
+ describe "Lookup Cache#" do
8
+
9
+ def mock_db
10
+ @db_mock = mock("Mysql2::Client")
11
+ Mysql2::Client.stub(:new).with(any_args()).and_return(@db_mock)
12
+ end
13
+
14
+ def mock_redis
15
+ @redis_client_mock = mock('Redis::Client')
16
+ Redis.stub!(:connect).and_return(@redis_client_mock)
17
+ end
18
+
19
+ it "should raise ResourcesError if db unavailable" do
20
+ expiration = 2
21
+ mock_redis
22
+ @redis_client_mock.stub!(:client).with(no_args()).and_return(nil)
23
+ Mysql2::Client.stub(:new).with(any_args()).and_raise(Mysql2::Error)
24
+ lambda {LookupCache.new("", "", expiration, "", "", "", "db_name")}.should raise_error(LookupCache::ResourcesError)
25
+ end
26
+
27
+ it "should raise ResourcesError if redis is unavailable" do
28
+ expiration = 2
29
+ mock_redis
30
+ Redis.stub!(:client).with(no_args()).and_return(nil)
31
+ Redis.stub!(:connect).and_raise(Exception)
32
+ lambda{LookupCache.new("", "", expiration, "", "", "", "db_name")}.should raise_error(LookupCache::ResourcesError)
33
+ end
34
+
35
+ context "adgroup_id calculations#" do
36
+
37
+ it "should calculate adgroup_id from DB and add to redis if ad_id exist in DB" do
38
+ adgroup_id = 10
39
+ query_result = [{"adgroup_id" => adgroup_id}]
40
+ ad_id = 1
41
+ expiration = 2
42
+ mock_db
43
+ mock_redis
44
+ @redis_client_mock.should_receive(:get).with("k:adgroup:#{ad_id}").and_return(nil)
45
+ @redis_client_mock.should_receive(:setex).with("k:adgroup:#{ad_id}",expiration,adgroup_id)
46
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
47
+
48
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
49
+ target.get_adgroup_id(ad_id).should == adgroup_id
50
+ end
51
+
52
+ it "should not go to DB cache if data exists in redis" do
53
+ adgroup_id = 10
54
+ ad_id = 1
55
+ expiration = 2
56
+ mock_db
57
+ mock_redis
58
+ @redis_client_mock.should_receive(:get).with("k:adgroup:#{ad_id}").and_return(adgroup_id)
59
+ @db_mock.should_not_receive(:query).with(any_args())
60
+
61
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
62
+ target.get_adgroup_id(ad_id).should.should == adgroup_id
63
+ end
64
+
65
+
66
+ it "should raise error if requested to calculate adgroup_id and ad_id doesn't exist in DB" do
67
+ ad_id = 1
68
+ mock_db
69
+ mock_redis
70
+ @redis_client_mock.should_receive(:get).with("k:adgroup:#{ad_id}").and_return(nil)
71
+ @db_mock.should_receive(:query).with(any_args()).and_return([{}])
72
+
73
+ target = LookupCache.new("", "", "", "", "", "", "db_name")
74
+ lambda { target.get_adgroup_id(ad_id)}.should raise_error(LookupCache::InputError)
75
+ end
76
+
77
+
78
+ end
79
+
80
+ context "campaign_id calculations#" do
81
+
82
+ it "should calculate campaign_id from DB and add to redis if ad_id exist in DB" do
83
+ campaign_id = 100
84
+ query_result = [{"campaign_id" => campaign_id}]
85
+ ad_id = 1
86
+ expiration = 2
87
+ mock_db
88
+ mock_redis
89
+ @redis_client_mock.should_receive(:get).with("k:campaign:#{ad_id}").and_return(nil)
90
+ @redis_client_mock.should_receive(:setex).with("k:campaign:#{ad_id}", expiration, campaign_id)
91
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
92
+
93
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
94
+ target.get_campaign_id(ad_id).should == campaign_id
95
+ end
96
+
97
+ it "should not go to DB cache if data exists in redis" do
98
+ campaign_id = 100
99
+ ad_id = 1
100
+ expiration = 2
101
+ mock_db
102
+ mock_redis
103
+ @redis_client_mock.should_receive(:get).with("k:campaign:#{ad_id}").and_return(campaign_id)
104
+ @db_mock.should_not_receive(:query).with(any_args())
105
+
106
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
107
+ target.get_campaign_id(ad_id).should == campaign_id
108
+ end
109
+
110
+
111
+ it "should raise error if requested to calculate campaign_id and ad_id doesn't exist in DB" do
112
+ ad_id = 1
113
+ mock_db
114
+ mock_redis
115
+
116
+ @redis_client_mock.should_receive(:get).with("k:campaign:#{ad_id}").and_return(nil)
117
+ @db_mock.should_receive(:query).with(any_args()).and_return([{}])
118
+
119
+ target = LookupCache.new("", "", "", "", "", "", "db_name")
120
+ lambda {target.get_campaign_id(ad_id)}.should raise_error(LookupCache::InputError)
121
+ end
122
+
123
+ end
124
+
125
+ context "campaign pricing rate" do
126
+
127
+ it "should get pricing_rate from DB and add to redis if campaign_id exists in DB" do
128
+ pricing_rate = 4
129
+ query_result = [{"pricing_rate" => pricing_rate}]
130
+ campaign_id = 1
131
+ expiration = 2
132
+ mock_db
133
+ mock_redis
134
+ @redis_client_mock.should_receive(:get).with("k:campaign_pricing_rate:#{campaign_id}").and_return(nil)
135
+ @redis_client_mock.should_receive(:setex).with("k:campaign_pricing_rate:#{campaign_id}", expiration, pricing_rate)
136
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
137
+
138
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
139
+ target.get_campaign_pricing_rate(campaign_id).should == pricing_rate
140
+ end
141
+
142
+ it "should not go to DB cache if data exists in redis" do
143
+ pricing_rate = 4
144
+ campaign_id = 1
145
+ expiration = 2
146
+ mock_db
147
+ mock_redis
148
+ @redis_client_mock.should_receive(:get).with("k:campaign_pricing_rate:#{campaign_id}").and_return(pricing_rate)
149
+ @db_mock.should_not_receive(:query).with(any_args())
150
+
151
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
152
+ target.get_campaign_pricing_rate(campaign_id).should == pricing_rate
153
+ end
154
+
155
+
156
+ it "should raise error if requested to get pricing_rate for campaign_id doesn't exist in DB" do
157
+ campaign_id = 1
158
+ mock_db
159
+ mock_redis
160
+
161
+ @redis_client_mock.should_receive(:get).with("k:campaign_pricing_rate:#{campaign_id}").and_return(nil)
162
+ @db_mock.should_receive(:query).with(any_args()).and_return([{}])
163
+
164
+ target = LookupCache.new("", "", "", "", "", "", "db_name")
165
+ lambda {target.get_campaign_pricing_rate(campaign_id)}.should raise_error(LookupCache::InputError)
166
+ end
167
+ end
168
+
169
+
170
+ context "engagement type get" do
171
+
172
+ it "should get ad_type_id from DB and add to redis if ad_id exists in DB" do
173
+ ad_type_id = 4
174
+ query_result = [{"ad_type_id" => ad_type_id}]
175
+ ad_id = 1
176
+ expiration = 2
177
+ mock_db
178
+ mock_redis
179
+ @redis_client_mock.should_receive(:get).with("k:engagement_ad_type:#{ad_id}").and_return(nil)
180
+ @redis_client_mock.should_receive(:setex).with("k:engagement_ad_type:#{ad_id}", expiration, ad_type_id)
181
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
182
+
183
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
184
+ target.get_engagement_type(ad_id).should == ad_type_id
185
+ end
186
+
187
+ it "should not go to DB cache if data exists in redis" do
188
+ ad_type_id = 4
189
+ ad_id = 1
190
+ expiration = 2
191
+ mock_db
192
+ mock_redis
193
+ @redis_client_mock.should_receive(:get).with("k:engagement_ad_type:#{ad_id}").and_return(ad_type_id)
194
+ @db_mock.should_not_receive(:query).with(any_args())
195
+
196
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
197
+ target.get_engagement_type(ad_id).should == ad_type_id
198
+ end
199
+
200
+
201
+ it "should raise error if requested to get ad_type_id for ad_id doesn't exist in DB" do
202
+ ad_id = 1
203
+ mock_db
204
+ mock_redis
205
+
206
+ @redis_client_mock.should_receive(:get).with("k:engagement_ad_type:#{ad_id}").and_return(nil)
207
+ @db_mock.should_receive(:query).with(any_args()).and_return([{}])
208
+
209
+ target = LookupCache.new("", "", "", "", "", "", "db_name")
210
+ lambda {target.get_engagement_type(ad_id)}.should raise_error(LookupCache::InputError)
211
+ end
212
+
213
+ end
214
+
215
+ context "engagement video length get" do
216
+
217
+ it "should get ad video length from DB and add to redis if ad_id exist in DB" do
218
+ video_len = 3500
219
+ query_result = [{"rm_time_millis" => video_len}]
220
+ ad_id = 1
221
+ expiration = 2
222
+ mock_db
223
+ mock_redis
224
+ @redis_client_mock.should_receive(:get).with("k:engagement_ad_video:#{ad_id}").and_return(nil)
225
+ @redis_client_mock.should_receive(:setex).with("k:engagement_ad_video:#{ad_id}", expiration, video_len)
226
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
227
+
228
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
229
+ target.get_engagement_video_length(ad_id).should == video_len
230
+ end
231
+
232
+ it "should not go to DB cache if data exists in redis" do
233
+ video_len = 3500
234
+ ad_id = 1
235
+ expiration = 2
236
+ mock_db
237
+ mock_redis
238
+ @redis_client_mock.should_receive(:get).with("k:engagement_ad_video:#{ad_id}").and_return(video_len)
239
+ @db_mock.should_not_receive(:query).with(any_args())
240
+
241
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
242
+ target.get_engagement_video_length(ad_id).should == video_len
243
+ end
244
+
245
+
246
+ it "should raise error if requested to get video length for ad_id doesn't exist in DB" do
247
+ ad_id = 1
248
+ mock_db
249
+ mock_redis
250
+
251
+ @redis_client_mock.should_receive(:get).with("k:engagement_ad_video:#{ad_id}").and_return(nil)
252
+ @db_mock.should_receive(:query).with(any_args()).and_return([{}])
253
+
254
+ target = LookupCache.new("", "", "", "", "", "", "db_name")
255
+ lambda {target.get_engagement_video_length(ad_id)}.should raise_error(LookupCache::InputError)
256
+ end
257
+
258
+ end
259
+
260
+ context "is engagement by precentage" do
261
+ it "get engagment as 1/0" do
262
+ ad_id = 123
263
+ expiration = 20
264
+ [0,1].each do |should_engaged_by_per|
265
+ query_result = [{"engagment_by_percentage" => should_engaged_by_per}]
266
+ mock_db
267
+ mock_redis
268
+
269
+ @redis_client_mock.should_receive(:get).and_return(nil)
270
+ @redis_client_mock.should_receive(:setex).with("k:engagment_by_percentage:#{ad_id}", expiration, should_engaged_by_per)
271
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
272
+
273
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
274
+ target.get_is_engagement_by_percentage(ad_id).should == should_engaged_by_per
275
+ end
276
+ end
277
+
278
+ it "should not go to DB cache if data exists in redis" do
279
+ ad_id = 1
280
+ expiration = 2
281
+ mock_db
282
+ mock_redis
283
+ should_engaged_by_per = 0
284
+ @redis_client_mock.should_receive(:get).with("k:engagment_by_percentage:#{ad_id}").and_return(should_engaged_by_per)
285
+ @db_mock.should_not_receive(:query).with(any_args())
286
+
287
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
288
+ target.get_is_engagement_by_percentage(ad_id).should == should_engaged_by_per
289
+ end
290
+
291
+ it "should raise error if requested to get engagement percentage for ad_id doesn't exist in DB" do
292
+ ad_id = 1
293
+ mock_db
294
+ mock_redis
295
+
296
+ @redis_client_mock.should_receive(:get).with("k:engagment_by_percentage:#{ad_id}").and_return(nil)
297
+ @db_mock.should_receive(:query).with(any_args()).and_return([{}])
298
+
299
+ target = LookupCache.new("", "", "", "", "", "", "db_name")
300
+ lambda {target.get_is_engagement_by_percentage(ad_id)}.should raise_error(LookupCache::InputError)
301
+ end
302
+ end
303
+
304
+ context "account_id calculations#" do
305
+
306
+ it "should calculate account_id from DB and add to redis if ad_id exist in DB" do
307
+ account_id = 1000
308
+ query_result = [{"account_id" => account_id}]
309
+ ad_id = 1
310
+ expiration = 2
311
+ mock_db
312
+ mock_redis
313
+ @redis_client_mock.should_receive(:get).with("k:account:#{ad_id}").and_return(nil)
314
+ @redis_client_mock.should_receive(:setex).with("k:account:#{ad_id}", expiration, account_id)
315
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
316
+
317
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
318
+ target.get_account_id(ad_id).should == account_id
319
+ end
320
+
321
+ it "should not go to DB cache if data exists in redis" do
322
+ account_id = 1000
323
+ ad_id = 1
324
+ expiration = 2
325
+ mock_db
326
+ mock_redis
327
+ @redis_client_mock.should_receive(:get).with("k:account:#{ad_id}").and_return(account_id)
328
+ @db_mock.should_not_receive(:query).with(any_args())
329
+
330
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
331
+ target.get_account_id(ad_id).should == account_id
332
+ end
333
+
334
+
335
+ it "should raise error if requested to calculate account_id and ad_id doesn't exist in DB" do
336
+ ad_id = 1
337
+ mock_db
338
+ mock_redis
339
+ @redis_client_mock.should_receive(:get).with("k:account:#{ad_id}").and_return(nil)
340
+ @db_mock.should_receive(:query).with(any_args()).and_return([{}])
341
+
342
+ target = LookupCache.new("", "", "", "", "", "", "db_name")
343
+ lambda {target.get_account_id(ad_id)}.should raise_error(LookupCache::InputError)
344
+ end
345
+
346
+ end
347
+
348
+ context "date calculations#" do
349
+
350
+ it "should calculate date from record's date" do
351
+ mock_db
352
+ mock_redis
353
+
354
+ target = LookupCache.new("", "", "", "", "", "", "db_name")
355
+ target.get_date("2011-09-25 17:27:02.614").should == "2011-09-25"
356
+ end
357
+
358
+ it "should calculate date from record's date as fixnum" do
359
+ mock_db
360
+ mock_redis
361
+
362
+ target = LookupCache.new("", "", "", "", "", "", "db_name")
363
+ target.get_date(1317911311).should == "2011-10-06"
364
+ end
365
+
366
+ it "should return nil if date cannot be parsed" do
367
+ mock_db
368
+ mock_redis
369
+
370
+ target = LookupCache.new("", "", "", "", "", "", "db_name")
371
+ lambda{ target.get_date("2011-09 17:27:02.614")}.should raise_error(LookupCache::InputError)
372
+ end
373
+
374
+ it "should return nil if date is nil" do
375
+
376
+ mock_db
377
+ mock_redis
378
+
379
+ target = LookupCache.new("", "", "", "", "", "", "db_name")
380
+ target.get_date(nil).should == nil
381
+ end
382
+
383
+ end
384
+
385
+ context "adgroup topics calculations#" do
386
+
387
+ it "should calculate relevant adgroup topics from DB and add to redis if topics exists in DB if page topics is an array" do
388
+ adgroup_id = 1000
389
+ query_result = [{"subtopic" => "1"},{"subtopic" => "2"}]
390
+ page_subtopics = ["1","2"]
391
+ expiration = 2
392
+ mock_db
393
+ mock_redis
394
+ @redis_client_mock.should_receive(:get).with("k:adgroup_topics:#{adgroup_id}").and_return(nil)
395
+ @redis_client_mock.should_receive(:setex).with("k:adgroup_topics:#{adgroup_id}", expiration, page_subtopics.to_json)
396
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
397
+ @redis_client_mock.should_receive(:get).with("k:adgroup_topics:#{adgroup_id}").exactly(3).times.and_return(page_subtopics.to_json)
398
+
399
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
400
+ target.get_relevant_topic_ids([],adgroup_id).should == []
401
+ target.get_relevant_topic_ids(page_subtopics,adgroup_id).should == [1,2]
402
+ target.get_relevant_topic_ids(["1"],adgroup_id).should == [1]
403
+ target.get_relevant_topic_ids(["2"],adgroup_id).should == [2]
404
+ target.get_relevant_topic_ids(["4"],adgroup_id).should == []
405
+ end
406
+
407
+ it "should calculate relevant adgroup topics from DB and add to redis if topics exists in DB if page topics is a string with +" do
408
+ adgroup_id = 1000
409
+ query_result = [{"subtopic" => "1"},{"subtopic" => "2"}]
410
+ page_subtopics= "1+2"
411
+ expiration = 2
412
+ mock_db
413
+ mock_redis
414
+ @redis_client_mock.should_receive(:get).with("k:adgroup_topics:#{adgroup_id}").and_return(nil)
415
+ @redis_client_mock.should_receive(:setex).with("k:adgroup_topics:#{adgroup_id}", expiration, ["1","2"].to_json)
416
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
417
+ @redis_client_mock.should_receive(:get).with("k:adgroup_topics:#{adgroup_id}").exactly(3).times.and_return(["1","2"].to_json)
418
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
419
+ target.get_relevant_topic_ids("",adgroup_id).should == []
420
+ target.get_relevant_topic_ids(page_subtopics,adgroup_id).should == [1,2]
421
+ target.get_relevant_topic_ids("1",adgroup_id).should == [1]
422
+ target.get_relevant_topic_ids("2",adgroup_id).should == [2]
423
+ target.get_relevant_topic_ids("4",adgroup_id).should == []
424
+ end
425
+
426
+ it "should calculate relevant adgroup topics from DB and add to redis if topics exists in DB if page topics is a string with spaces" do
427
+ adgroup_id = 1000
428
+ query_result = [{"subtopic" => "1"},{"subtopic" => "2"}]
429
+ page_subtopics= "1 2"
430
+ expiration = 2
431
+ mock_db
432
+ mock_redis
433
+ @redis_client_mock.should_receive(:get).with("k:adgroup_topics:#{adgroup_id}").and_return(nil)
434
+ @redis_client_mock.should_receive(:setex).with("k:adgroup_topics:#{adgroup_id}", expiration, ["1","2"].to_json)
435
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
436
+ @redis_client_mock.should_receive(:get).with("k:adgroup_topics:#{adgroup_id}").exactly(3).times.and_return(["1","2"].to_json)
437
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
438
+ target.get_relevant_topic_ids("",adgroup_id).should == []
439
+ target.get_relevant_topic_ids(page_subtopics,adgroup_id).should == [1,2]
440
+ target.get_relevant_topic_ids("1",adgroup_id).should == [1]
441
+ target.get_relevant_topic_ids("2",adgroup_id).should == [2]
442
+ target.get_relevant_topic_ids("4",adgroup_id).should == []
443
+ end
444
+
445
+ it "should not go to DB cache if data exists in redis" do
446
+ adgroup_id = 1000
447
+ subtopics_result = [1,2]
448
+ expiration = 2
449
+ mock_db
450
+ mock_redis
451
+ @redis_client_mock.should_receive(:get).with("k:adgroup_topics:#{adgroup_id}").twice.and_return(subtopics_result.to_json)
452
+ @db_mock.should_not_receive(:query).with(any_args())
453
+
454
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
455
+ target.get_relevant_topic_ids(["1","2"], adgroup_id).should == subtopics_result
456
+ target.get_relevant_topic_ids("1+2", adgroup_id).should == subtopics_result
457
+ end
458
+
459
+
460
+ it "should return empty topics if subtopics don't exist in DB" do
461
+ adgroup_id = 1000
462
+ query_result = []
463
+ subtopics_result = []
464
+ expiration = 2
465
+ mock_db
466
+ mock_redis
467
+ @redis_client_mock.should_receive(:get).with("k:adgroup_topics:#{adgroup_id}").and_return(nil)
468
+ @redis_client_mock.should_receive(:setex).with("k:adgroup_topics:#{adgroup_id}", expiration, subtopics_result.to_json)
469
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
470
+ @redis_client_mock.should_receive(:get).with("k:adgroup_topics:#{adgroup_id}").and_return(subtopics_result.to_json)
471
+
472
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
473
+ target.get_relevant_topic_ids("1", adgroup_id).should == subtopics_result
474
+ target.get_relevant_topic_ids(["1"], adgroup_id).should == subtopics_result
475
+ end
476
+
477
+ end
478
+
479
+ context "adgroup publisher pubgroups calculations#" do
480
+
481
+ it "should adgroup publisher pubgroups from DB and add to redis if got matching pubgorups from DB" do
482
+ adgroup_id = 1000
483
+ publisher_id = 131821
484
+ query_result = [{"pubgroup_id" => "1"},{"pubgroup_id" => "2"}]
485
+ pubgroup_ids = ['1','2']
486
+ expiration = 2
487
+ mock_db
488
+ mock_redis
489
+ key = "k:adgroup_publisher_pubgroups:#{adgroup_id}:#{publisher_id}"
490
+ @redis_client_mock.should_receive(:get).with(key).and_return(nil)
491
+ @redis_client_mock.should_receive(:setex).with(key, expiration, pubgroup_ids.to_json)
492
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
493
+ @redis_client_mock.should_receive(:get).with(key).and_return(pubgroup_ids.to_json)
494
+
495
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
496
+ target.get_pubgroup_ids(adgroup_id,nil).should == []
497
+ target.get_pubgroup_ids(nil,publisher_id).should == []
498
+ target.get_pubgroup_ids(adgroup_id,publisher_id).should == pubgroup_ids.map{|pg| pg.to_i}
499
+ target.get_pubgroup_ids(adgroup_id,publisher_id).should == pubgroup_ids.map{|pg| pg.to_i}
500
+ end
501
+
502
+ it "should not go to DB cache if data exists in redis" do
503
+ adgroup_id = 1000
504
+ publisher_id = 131821
505
+ pubgroups_ids = [3,4]
506
+ expiration = 2
507
+ mock_db
508
+ mock_redis
509
+ @redis_client_mock.should_receive(:get).with("k:adgroup_publisher_pubgroups:#{adgroup_id}:#{publisher_id}").and_return(pubgroups_ids.to_json)
510
+ @db_mock.should_not_receive(:query).with(any_args())
511
+
512
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
513
+ target.get_pubgroup_ids(adgroup_id,publisher_id).should == pubgroups_ids
514
+ end
515
+
516
+ it "should return pubgroup -1 if none were found in DB" do
517
+ adgroup_id = 1000
518
+ publisher_id = 131821
519
+ query_result = []
520
+ pubgroups_ids = [-1]
521
+ expiration = 2
522
+ mock_db
523
+ mock_redis
524
+ @redis_client_mock.should_receive(:get).with("k:adgroup_publisher_pubgroups:#{adgroup_id}:#{publisher_id}").and_return(nil)
525
+ @redis_client_mock.should_receive(:setex).with("k:adgroup_publisher_pubgroups:#{adgroup_id}:#{publisher_id}", expiration, pubgroups_ids.to_json)
526
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
527
+ @redis_client_mock.should_receive(:get).with("k:adgroup_publisher_pubgroups:#{adgroup_id}:#{publisher_id}").and_return(pubgroups_ids.to_json)
528
+
529
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
530
+ target.get_pubgroup_ids(adgroup_id,publisher_id).should == pubgroups_ids #check that got default value from DB
531
+ target.get_pubgroup_ids(adgroup_id,publisher_id).should == pubgroups_ids #check that got default value Cache either.
532
+ end
533
+
534
+ end
535
+
536
+ context "duplicate value tests#" do
537
+ it "should return nil if value for the first time and set it" do
538
+ key = "test"
539
+ expiration = 2
540
+ mock_redis
541
+ mock_db
542
+ @redis_client_mock.should_receive(:get).with("k:dup:#{key}").and_return(nil)
543
+ @redis_client_mock.should_receive(:setex).with("k:dup:#{key}", expiration, true)
544
+
545
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
546
+ res = target.get_and_set_duplicate_value(key,expiration)
547
+ res.should == nil
548
+ end
549
+
550
+ it "should return true value for the second time" do
551
+ key = "test"
552
+ expiration = 2
553
+ mock_redis
554
+ mock_db
555
+ @redis_client_mock.should_receive(:get).with("k:dup:#{key}").and_return(nil)
556
+ @redis_client_mock.should_receive(:setex).with("k:dup:#{key}", expiration, true)
557
+ @redis_client_mock.should_receive(:get).with("k:dup:#{key}").and_return(true)
558
+
559
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
560
+ res = target.get_and_set_duplicate_value(key,expiration)
561
+ res = target.get_and_set_duplicate_value(key,expiration)
562
+ res.should == true
563
+ end
564
+
565
+ it "should clear key for redis" do
566
+ key = "test"
567
+ mock_redis
568
+ mock_db
569
+ @redis_client_mock.should_receive(:del).with("k:dup:#{key}")
570
+ target = LookupCache.new("", "", 606060606060606060606060606060606060606060606060606060606060606060606060606060606060606060606060606060606060606060606060, "", "", "", "db_name")
571
+ target.clear_duplicate_value(key)
572
+ end
573
+ end
574
+
575
+ context "advertiser share info#" do
576
+ it "should get advertiser shares from DB and add to redis" do
577
+ advertiser_id = 7753
578
+ query_result = [{"reserveShare" => 0.1, "advertiserShare" => 0.3, "reserve" => 0.0}]
579
+ expected = {"reserve_share" => 0.1, "adv_share" => 0.3, "campaign_reserve" => 0.0}
580
+ expiration = 2
581
+ mock_db
582
+ mock_redis
583
+ key = "k:advertiser_shares:#{advertiser_id}"
584
+ @redis_client_mock.should_receive(:get).with(key).and_return(nil)
585
+ @redis_client_mock.should_receive(:setex).with(key, expiration, expected.to_json)
586
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
587
+ @redis_client_mock.should_receive(:get).with(key).and_return(expected.to_json)
588
+
589
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
590
+ target.get_advertiser_shares(nil,nil).should == {}
591
+ shares = target.get_advertiser_shares(advertiser_id,nil)
592
+ shares.should == expected
593
+ target.get_advertiser_shares(advertiser_id,nil).should == expected
594
+ end
595
+
596
+ it "should not go to DB cache if data exists in redis" do
597
+ advertiser_id = 8
598
+ campaign_id = 55555
599
+ adv_shares = {"reserve_share" => 0.1, "adv_share" => 0.3, "campaign_reserve" => 0.12}
600
+ expiration = 2
601
+ mock_db
602
+ mock_redis
603
+ @redis_client_mock.should_receive(:get).with("k:advertiser_shares:#{advertiser_id}:#{campaign_id}").and_return(adv_shares.to_json)
604
+ @db_mock.should_not_receive(:query).with(any_args())
605
+
606
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
607
+ target.get_advertiser_shares(advertiser_id,campaign_id).should == adv_shares
608
+ end
609
+
610
+ it "should return empty response if advertiser was not found in DB" do
611
+ advertiser_id = 23121
612
+ campaign_id = nil
613
+ query_result = []
614
+ adv_shares = {"reserve_share" => 0.0, "adv_share" => 0.0, "campaign_reserve" => 0.0}
615
+ expiration = 2
616
+ mock_db
617
+ mock_redis
618
+ @redis_client_mock.should_receive(:get).with("k:advertiser_shares:#{advertiser_id}").and_return(nil)
619
+ @redis_client_mock.should_receive(:setex).with("k:advertiser_shares:#{advertiser_id}", expiration, adv_shares.to_json)
620
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
621
+
622
+ @redis_client_mock.should_receive(:get).with("k:advertiser_shares:#{advertiser_id}").and_return(adv_shares.to_json)
623
+
624
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
625
+ target.get_advertiser_shares(advertiser_id,campaign_id).should == adv_shares #check that got default value from DB
626
+ target.get_advertiser_shares(advertiser_id,campaign_id).should == adv_shares #check that got default value Cache either.
627
+ end
628
+ end
629
+
630
+ context "publisher payment info#" do
631
+ it "should get publisher payment info from DB and add to redis" do
632
+ publisher_id = 131821
633
+ query_result = [{"site_payment_type_id" => 2, "cpm" => 232 , "rev_share" => 0.0}]
634
+ expected = {"payment_type" => 2, "cpm" => 232.0 , "rev_share" => 0.0}
635
+ expiration = 2
636
+ mock_db
637
+ mock_redis
638
+ key = "k:publisher_payments_info:#{publisher_id}"
639
+ @redis_client_mock.should_receive(:get).with(key).and_return(nil)
640
+ @redis_client_mock.should_receive(:setex).with(key, expiration, expected.to_json)
641
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
642
+ @redis_client_mock.should_receive(:get).with(key).and_return(expected.to_json)
643
+
644
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
645
+ target.get_publisher_payment_info(nil).should == {}
646
+ shares = target.get_publisher_payment_info(publisher_id)
647
+ shares.should == expected
648
+ target.get_publisher_payment_info(publisher_id).should == expected
649
+ end
650
+
651
+ it "should not go to DB cache if data exists in redis" do
652
+ publisher_id = 4042
653
+ payment_info = {"payment_type" => 1, "cpm" => 0 , "rev_share" => 0.23}
654
+ expiration = 2
655
+ mock_db
656
+ mock_redis
657
+ @redis_client_mock.should_receive(:get).with("k:publisher_payments_info:#{publisher_id}").and_return(payment_info.to_json)
658
+ @db_mock.should_not_receive(:query).with(any_args())
659
+
660
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
661
+ target.get_publisher_payment_info(publisher_id).should == payment_info
662
+ end
663
+
664
+ it "should return empty response if publisher was not found in DB" do
665
+ publisher_id = 22222
666
+ query_result = []
667
+ payment_info = {"payment_type" => 1, "cpm" => 0.0, "rev_share" => 0.0}
668
+ expiration = 2
669
+ mock_db
670
+ mock_redis
671
+ @redis_client_mock.should_receive(:get).with("k:publisher_payments_info:#{publisher_id}").and_return(nil)
672
+ @redis_client_mock.should_receive(:setex).with("k:publisher_payments_info:#{publisher_id}", expiration, payment_info.to_json)
673
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
674
+ @redis_client_mock.should_receive(:get).with("k:publisher_payments_info:#{publisher_id}").and_return(payment_info.to_json)
675
+
676
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
677
+ target.get_publisher_payment_info(publisher_id).should == payment_info #check that got default value from DB
678
+ target.get_publisher_payment_info(publisher_id).should == payment_info #check that got default value Cache either.
679
+ end
680
+ end
681
+
682
+ context "campaign_engagement_qualifier#" do
683
+ it "should get engagement qualifier for campaign" do
684
+ campaign_id = 3
685
+ expected = 4
686
+ json_value = "{\"value\":#{expected}}"
687
+ query_result = [{"engagement_qualifier" => "{\"value\":4}"}]
688
+ mock_db
689
+ mock_redis
690
+ redis_key = "k:engagement_threshold:#{campaign_id}"
691
+ @redis_client_mock.should_receive(:get).with(redis_key).and_return(nil)
692
+ @redis_client_mock.should_receive(:setex).with(redis_key, expiration=5, json_value)
693
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
694
+
695
+ target = LookupCache.new("", "", expiration=5, "", "", "", "db_name")
696
+ target.get_engagement_threshold(campaign_id).should == expected # from db
697
+
698
+ @redis_client_mock.should_receive(:get).with(redis_key).and_return(json_value)
699
+ target.get_engagement_threshold(campaign_id).should == expected # from redis
700
+ end
701
+
702
+ it "should have no engagement qualifier for campaign" do
703
+ campaign_id = 33
704
+ expected = 4
705
+ json_value = "{\"value\":#{expected}}"
706
+ query_result = [{"engagement_qualifier" => "{\"value\":4}"}]
707
+ mock_db
708
+ mock_redis
709
+ redis_key = "k:engagement_threshold:#{campaign_id}"
710
+ @redis_client_mock.should_receive(:get).with(redis_key).and_return(nil)
711
+ @db_mock.should_receive(:query).with(any_args()).and_return(nil)
712
+
713
+ target = LookupCache.new("", "", expiration=5, "", "", "", "db_name")
714
+ target.get_engagement_threshold(campaign_id).should == nil # from db
715
+
716
+ target.get_engagement_threshold(campaign_id+1).should == nil # campaign not set
717
+ end
718
+ end
719
+
720
+ context "campaign cpm data#" do
721
+ it "should get campaign cpm rate from DB and add to redis" do
722
+ campaign_id = 2
723
+ query_result = [{"cpm_rate" => 0.04}]
724
+ expected = 0.04
725
+ expiration = 2
726
+ mock_db
727
+ mock_redis
728
+ key = "k:campaign_cpm:#{campaign_id}"
729
+ @redis_client_mock.should_receive(:get).with(key).and_return(nil)
730
+ @redis_client_mock.should_receive(:setex).with(key, expiration, expected)
731
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
732
+ @redis_client_mock.should_receive(:get).with(key).and_return(expected)
733
+
734
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
735
+ target.get_campaign_cpm(nil).should == 0.0
736
+ cpm = target.get_campaign_cpm(campaign_id)
737
+ cpm.should == expected
738
+ target.get_campaign_cpm(campaign_id).should == expected
739
+ end
740
+
741
+ it "should not go to DB cache if data exists in redis" do
742
+ campaign_id = 2
743
+ expected = 0.04
744
+ expiration = 2
745
+ mock_db
746
+ mock_redis
747
+ @redis_client_mock.should_receive(:get).with("k:campaign_cpm:#{campaign_id}").and_return(expected)
748
+ @db_mock.should_not_receive(:query).with(any_args())
749
+
750
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
751
+ target.get_campaign_cpm(campaign_id).should == expected
752
+ end
753
+
754
+ it "should return raise exception if campaign was not found in DB" do
755
+ campaign_id = 1
756
+ mock_db
757
+ mock_redis
758
+ @redis_client_mock.should_receive(:get).with("k:campaign_cpm:#{campaign_id}").and_return(nil)
759
+ @db_mock.should_receive(:query).with(any_args()).and_return([{}])
760
+
761
+ target = LookupCache.new("", "", "", "", "", "", "db_name")
762
+ lambda {target.get_campaign_cpm(campaign_id)}.should raise_error(LookupCache::InputError)
763
+ end
764
+ end
765
+
766
+ context "campaign pricing data#" do
767
+ it "should get campaign pricing type from DB and add to redis" do
768
+ campaign_id = 2
769
+ query_result = [{"pricing_type_id" => 3}]
770
+ expected = 3
771
+ expiration = 2
772
+ mock_db
773
+ mock_redis
774
+ key = "k:pricing_type_id:#{campaign_id}"
775
+ @redis_client_mock.should_receive(:get).with(key).and_return(nil)
776
+ @redis_client_mock.should_receive(:setex).with(key, expiration, expected)
777
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
778
+ @redis_client_mock.should_receive(:get).with(key).and_return(expected)
779
+
780
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
781
+ #target.get_campaign_pricing_type_id(nil).should == 0.0
782
+ pricing_type_id = target.get_campaign_pricing_type_id(campaign_id)
783
+ pricing_type_id.should == expected
784
+ target.get_campaign_pricing_type_id(campaign_id).should == expected
785
+ end
786
+
787
+ it "should not go to DB cache if data exists in redis" do
788
+ campaign_id = 2
789
+ expected = 3
790
+ expiration = 2
791
+ mock_db
792
+ mock_redis
793
+ @redis_client_mock.should_receive(:get).with("k:pricing_type_id:#{campaign_id}").and_return(expected)
794
+ @db_mock.should_not_receive(:query).with(any_args())
795
+
796
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
797
+ target.get_campaign_pricing_type_id(campaign_id).should == expected
798
+ end
799
+
800
+ it "should return raise exception if campaign was not found in DB" do
801
+ campaign_id = 1
802
+ mock_db
803
+ mock_redis
804
+ @redis_client_mock.should_receive(:get).with("k:pricing_type_id:#{campaign_id}").and_return(nil)
805
+ @db_mock.should_receive(:query).with(any_args()).and_return([{}])
806
+
807
+ target = LookupCache.new("", "", "", "", "", "", "db_name")
808
+ lambda {target.get_campaign_pricing_type_id(campaign_id)}.should raise_error(LookupCache::InputError)
809
+ end
810
+
811
+ end
812
+
813
+ context "Channel publishers#" do
814
+ it "should get all channel publishers from DB and add to redis" do
815
+ query_result = [{"id"=>15},{"id" => 17}]
816
+ expected = [15,17]
817
+ expiration = 2
818
+ mock_db
819
+ mock_redis
820
+ key = "k:channel_publishers"
821
+ @redis_client_mock.should_receive(:get).with(key).and_return(nil)
822
+ @redis_client_mock.should_receive(:setex).with(key, expiration, expected)
823
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
824
+ @redis_client_mock.should_receive(:get).with(key).and_return(expected)
825
+
826
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
827
+ target.get_channel_publishers.should == expected
828
+ target.get_channel_publishers.should == expected
829
+ end
830
+
831
+ it "should not go to DB cache if data exists in redis" do
832
+ expected = [15,17]
833
+ expiration = 2
834
+ mock_db
835
+ mock_redis
836
+ @redis_client_mock.should_receive(:get).with("k:channel_publishers").and_return(expected)
837
+ @db_mock.should_not_receive(:query).with(any_args())
838
+
839
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
840
+ target.get_channel_publishers.should == expected
841
+ end
842
+ end
843
+
844
+ context "campaign cpc data#" do
845
+ it "should get campaign cpc rate from DB and add to redis" do
846
+ campaign_id = 3
847
+ query_result = [{"cpc_rate" => 0.03}]
848
+ expected = 0.03
849
+ expiration = 2
850
+ mock_db
851
+ mock_redis
852
+ key = "k:campaign_cpc:#{campaign_id}"
853
+ @redis_client_mock.should_receive(:get).with(key).and_return(nil)
854
+ @redis_client_mock.should_receive(:setex).with(key, expiration, expected)
855
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
856
+ @redis_client_mock.should_receive(:get).with(key).and_return(expected)
857
+
858
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
859
+ target.get_campaign_cpc(nil).should == 0.0
860
+ cpm = target.get_campaign_cpc(campaign_id)
861
+ cpm.should == expected
862
+ target.get_campaign_cpc(campaign_id).should == expected
863
+ end
864
+ end
865
+
866
+ context "inimage_publisher_id#" do
867
+ it "should get publisher id from DB and add to redis" do
868
+ account_id="1176592e370"
869
+ query_result = [{"publisher_id"=>20}]
870
+ expected = 20
871
+ expiration = 2
872
+ mock_db
873
+ mock_redis
874
+ key = "k:inimage_publisher_id_map:#{account_id}"
875
+ @redis_client_mock.should_receive(:get).with(key).and_return(nil)
876
+ @redis_client_mock.should_receive(:setex).with(key, expiration, expected)
877
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
878
+ @redis_client_mock.should_receive(:get).with(key).and_return(expected)
879
+
880
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
881
+ target.get_publisher_id_by_inimage_id(account_id).should == expected
882
+ target.get_publisher_id_by_inimage_id(account_id).should == expected
883
+ end
884
+
885
+ it "should not go to DB cache if data exists in redis" do
886
+ account_id="1176592e370"
887
+ expected = 20
888
+ expiration = 2
889
+ mock_db
890
+ mock_redis
891
+ @redis_client_mock.should_receive(:get).with("k:inimage_publisher_id_map:#{account_id}").and_return(expected)
892
+ @db_mock.should_not_receive(:query).with(any_args())
893
+
894
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
895
+ target.get_publisher_id_by_inimage_id(account_id).should == expected
896
+ end
897
+ end
898
+
899
+ context "inimage publisher payment info#" do
900
+ it "should get inimage publisher payment info from DB and add to redis" do
901
+ publisher_id = 131821
902
+ #query_result = [{"payment_type" => 0, "cpm" => 232 , "rev_share" => 0.0}]
903
+ query_result = [{"payment_type_id" => 2, "cpm" => 4 , "rev_share" => 0.0}]
904
+ expected = {"payment_type" => 2, "cpm" => 4.0 , "rev_share" => 0.0}
905
+ expiration = 2
906
+ mock_db
907
+ mock_redis
908
+ key = "k:inimage_publisher_payments_info:#{publisher_id}"
909
+ @redis_client_mock.should_receive(:get).with(key).and_return(nil)
910
+ @redis_client_mock.should_receive(:setex).with(key, expiration, expected.to_json)
911
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
912
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
913
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
914
+ @redis_client_mock.should_receive(:get).with(key).and_return(expected.to_json)
915
+
916
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
917
+ target.get_inimage_publisher_payment_info(nil).should == {}
918
+ shares = target.get_inimage_publisher_payment_info(publisher_id)
919
+ shares.should == expected
920
+ target.get_inimage_publisher_payment_info(publisher_id).should == expected
921
+ end
922
+
923
+ it "should not go to DB cache if data exists in redis" do
924
+ publisher_id = 4042
925
+ payment_info = {"payment_type" => 1, "cpm" => 0 , "rev_share" => 0.23}
926
+ expiration = 2
927
+ mock_db
928
+ mock_redis
929
+ @redis_client_mock.should_receive(:get).with("k:inimage_publisher_payments_info:#{publisher_id}").and_return(payment_info.to_json)
930
+ @db_mock.should_not_receive(:query).with(any_args())
931
+
932
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
933
+ target.get_inimage_publisher_payment_info(publisher_id).should == payment_info
934
+ end
935
+
936
+ it "should return empty response if publisher was not found in DB" do
937
+ publisher_id = 22222
938
+ query_result = []
939
+ payment_info = {"payment_type" => 1, "cpm" => 0.0, "rev_share" => 0.0}
940
+ expiration = 2
941
+ mock_db
942
+ mock_redis
943
+ @redis_client_mock.should_receive(:get).with("k:inimage_publisher_payments_info:#{publisher_id}").and_return(nil)
944
+ @redis_client_mock.should_receive(:setex).with("k:inimage_publisher_payments_info:#{publisher_id}", expiration, payment_info.to_json)
945
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
946
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
947
+ @db_mock.should_receive(:query).with(any_args()).and_return(query_result)
948
+ @redis_client_mock.should_receive(:get).with("k:inimage_publisher_payments_info:#{publisher_id}").and_return(payment_info.to_json)
949
+
950
+ target = LookupCache.new("", "", expiration, "", "", "", "db_name")
951
+ target.get_inimage_publisher_payment_info(publisher_id).should == payment_info #check that got default value from DB
952
+ target.get_inimage_publisher_payment_info(publisher_id).should == payment_info #check that got default value Cache either.
953
+ end
954
+ end
955
+
956
+ end