Fix randomly failing specs due to SearchLog cache.

This commit is contained in:
Guo Xiang Tan 2018-04-23 10:00:37 +08:00
parent ded84a4b58
commit 9eabf7c02c
6 changed files with 35 additions and 21 deletions

View File

@ -47,22 +47,25 @@ class SearchLog < ActiveRecord::Base
if existing = $redis.get(key) if existing = $redis.get(key)
id, old_term = existing.split(",", 2) id, old_term = existing.split(",", 2)
if term.start_with?(old_term) if term.start_with?(old_term)
where(id: id.to_i).update_all( where(id: id.to_i).update_all(
created_at: Time.zone.now, created_at: Time.zone.now,
term: term term: term
) )
result = [:updated, id.to_i] result = [:updated, id.to_i]
end end
end end
if !result if !result
log = create( log = self.create!(
term: term, term: term,
search_type: search_type, search_type: search_type,
ip_address: ip_address, ip_address: ip_address,
user_id: user_id user_id: user_id
) )
result = [:created, log.id] result = [:created, log.id]
end end

View File

@ -161,7 +161,7 @@ describe SearchController do
end end
context "#click" do context "#click" do
before do after do
SearchLog.clear_debounce_cache! SearchLog.clear_debounce_cache!
end end

View File

@ -296,16 +296,18 @@ describe Report do
SearchLog.log(term: 'php', search_type: :header, ip_address: '127.0.0.1') SearchLog.log(term: 'php', search_type: :header, ip_address: '127.0.0.1')
end end
after do
SearchLog.clear_debounce_cache!
end
it "returns a report with data" do it "returns a report with data" do
expect(report.data).to be_present expect(report.data[0][0]).to eq("ruby")
expect(report.data[0][1]).to eq(3)
expect(report.data[0][2]).to eq(2)
expect(report.data[0][0]).to eq "ruby" expect(report.data[1][0]).to eq("php")
expect(report.data[0][1]).to eq 3 expect(report.data[1][1]).to eq(1)
expect(report.data[0][2]).to eq 2 expect(report.data[1][2]).to eq(1)
expect(report.data[1][0]).to eq "php"
expect(report.data[1][1]).to eq 1
expect(report.data[1][2]).to eq 1
end end
end end
end end

View File

@ -2,7 +2,7 @@ require 'rails_helper'
RSpec.describe SearchLog, type: :model do RSpec.describe SearchLog, type: :model do
before do after do
SearchLog.clear_debounce_cache! SearchLog.clear_debounce_cache!
end end

View File

@ -5,10 +5,13 @@ RSpec.describe Admin::SearchLogsController do
let(:user) { Fabricate(:user) } let(:user) { Fabricate(:user) }
before do before do
SearchLog.clear_debounce_cache!
SearchLog.log(term: 'ruby', search_type: :header, ip_address: '127.0.0.1') SearchLog.log(term: 'ruby', search_type: :header, ip_address: '127.0.0.1')
end end
after do
SearchLog.clear_debounce_cache!
end
context "#index" do context "#index" do
it "raises an error if you aren't logged in" do it "raises an error if you aren't logged in" do
get '/admin/logs/search_logs.json' get '/admin/logs/search_logs.json'

View File

@ -503,17 +503,23 @@ describe UserMerger do
expect(post3.reload.rejected_by).to eq(target_user) expect(post3.reload.rejected_by).to eq(target_user)
end end
it "updates search log entries" do describe 'search logs' do
SearchLog.log(term: 'hello', search_type: :full_page, ip_address: '192.168.0.1', user_id: source_user.id) after do
SearchLog.log(term: 'world', search_type: :full_page, ip_address: '192.168.0.1', user_id: source_user.id) SearchLog.clear_debounce_cache!
SearchLog.log(term: 'star trek', search_type: :full_page, ip_address: '192.168.0.2', user_id: target_user.id) end
SearchLog.log(term: 'bad', search_type: :full_page, ip_address: '192.168.0.3', user_id: walter.id)
merge_users! it "updates search log entries" do
SearchLog.log(term: 'hello', search_type: :full_page, ip_address: '192.168.0.1', user_id: source_user.id)
SearchLog.log(term: 'world', search_type: :full_page, ip_address: '192.168.0.1', user_id: source_user.id)
SearchLog.log(term: 'star trek', search_type: :full_page, ip_address: '192.168.0.2', user_id: target_user.id)
SearchLog.log(term: 'bad', search_type: :full_page, ip_address: '192.168.0.3', user_id: walter.id)
expect(SearchLog.where(user_id: target_user.id).count).to eq(3) merge_users!
expect(SearchLog.where(user_id: source_user.id).count).to eq(0)
expect(SearchLog.where(user_id: walter.id).count).to eq(1) expect(SearchLog.where(user_id: target_user.id).count).to eq(3)
expect(SearchLog.where(user_id: source_user.id).count).to eq(0)
expect(SearchLog.where(user_id: walter.id).count).to eq(1)
end
end end
it "merges tag notification settings" do it "merges tag notification settings" do