2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-25 01:19:12 -05:00
|
|
|
describe RandomTopicSelector do
|
|
|
|
|
|
|
|
it 'can correctly use cache' do
|
|
|
|
key = RandomTopicSelector.cache_key
|
|
|
|
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.del key
|
2015-02-25 01:19:12 -05:00
|
|
|
|
|
|
|
4.times do |t|
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.rpush key, t
|
2015-02-25 01:19:12 -05:00
|
|
|
end
|
|
|
|
|
2016-03-02 19:26:45 -05:00
|
|
|
expect(RandomTopicSelector.next(0)).to eq([])
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(RandomTopicSelector.next(2)).to eq([0, 1])
|
2016-03-02 19:26:45 -05:00
|
|
|
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.expects(:multi).returns(Discourse.received_redis_readonly!)
|
2016-03-02 19:26:45 -05:00
|
|
|
expect(RandomTopicSelector.next(2)).to eq([2, 3])
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.unstub(:multi)
|
2016-03-02 19:26:45 -05:00
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(RandomTopicSelector.next(2)).to eq([2, 3])
|
2016-03-02 19:26:45 -05:00
|
|
|
expect(RandomTopicSelector.next(2)).to eq([])
|
2015-02-25 01:19:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can correctly backfill' do
|
2016-12-27 03:08:54 -05:00
|
|
|
category = Fabricate(:category, sort_order: 'op_likes')
|
2015-02-25 01:19:12 -05:00
|
|
|
t1 = Fabricate(:topic, category_id: category.id)
|
|
|
|
_t2 = Fabricate(:topic, category_id: category.id, visible: false)
|
|
|
|
_t3 = Fabricate(:topic, category_id: category.id, deleted_at: 1.minute.ago)
|
|
|
|
t4 = Fabricate(:topic, category_id: category.id)
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(RandomTopicSelector.next(5, category).sort).to eq([t1.id, t4.id].sort)
|
2015-02-25 01:19:12 -05:00
|
|
|
end
|
|
|
|
end
|