2025-09-05 13:39:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
|
|
RSpec.describe Cache::PreheatingJob do
|
2025-09-13 09:37:09 -04:00
|
|
|
before { Rails.cache.clear }
|
2025-09-05 13:39:50 -04:00
|
|
|
|
|
|
|
|
describe '#perform' do
|
|
|
|
|
let!(:user1) { create(:user) }
|
|
|
|
|
let!(:user2) { create(:user) }
|
|
|
|
|
let!(:import1) { create(:import, user: user1) }
|
|
|
|
|
let!(:import2) { create(:import, user: user2) }
|
2025-09-13 09:58:36 -04:00
|
|
|
let(:user_1_years_tracked_key) { "dawarich/user_#{user1.id}_years_tracked" }
|
|
|
|
|
let(:user_2_years_tracked_key) { "dawarich/user_#{user2.id}_years_tracked" }
|
|
|
|
|
let(:user_1_points_geocoded_stats_key) { "dawarich/user_#{user1.id}_points_geocoded_stats" }
|
|
|
|
|
let(:user_2_points_geocoded_stats_key) { "dawarich/user_#{user2.id}_points_geocoded_stats" }
|
2025-09-13 10:05:52 -04:00
|
|
|
let(:user_1_countries_visited_key) { "dawarich/user_#{user1.id}_countries_visited" }
|
|
|
|
|
let(:user_2_countries_visited_key) { "dawarich/user_#{user2.id}_countries_visited" }
|
|
|
|
|
let(:user_1_cities_visited_key) { "dawarich/user_#{user1.id}_cities_visited" }
|
|
|
|
|
let(:user_2_cities_visited_key) { "dawarich/user_#{user2.id}_cities_visited" }
|
2025-09-05 13:39:50 -04:00
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
create_list(:point, 3, user: user1, import: import1, reverse_geocoded_at: Time.current)
|
|
|
|
|
create_list(:point, 2, user: user2, import: import2, reverse_geocoded_at: Time.current)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'preheats years_tracked cache for all users' do
|
2025-09-13 09:58:36 -04:00
|
|
|
# Clear cache before test to ensure clean state
|
|
|
|
|
Rails.cache.clear
|
2025-09-05 13:39:50 -04:00
|
|
|
|
|
|
|
|
described_class.new.perform
|
2025-09-13 09:58:36 -04:00
|
|
|
|
|
|
|
|
# Verify that cache keys exist after job runs
|
|
|
|
|
expect(Rails.cache.exist?(user_1_years_tracked_key)).to be true
|
|
|
|
|
expect(Rails.cache.exist?(user_2_years_tracked_key)).to be true
|
|
|
|
|
|
|
|
|
|
# Verify the cached data is reasonable
|
|
|
|
|
user1_years = Rails.cache.read(user_1_years_tracked_key)
|
|
|
|
|
user2_years = Rails.cache.read(user_2_years_tracked_key)
|
|
|
|
|
|
|
|
|
|
expect(user1_years).to be_an(Array)
|
|
|
|
|
expect(user2_years).to be_an(Array)
|
2025-09-05 13:39:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'preheats points_geocoded_stats cache for all users' do
|
2025-09-13 09:58:36 -04:00
|
|
|
# Clear cache before test to ensure clean state
|
|
|
|
|
Rails.cache.clear
|
2025-09-05 13:39:50 -04:00
|
|
|
|
|
|
|
|
described_class.new.perform
|
2025-09-13 09:58:36 -04:00
|
|
|
|
|
|
|
|
# Verify that cache keys exist after job runs
|
|
|
|
|
expect(Rails.cache.exist?(user_1_points_geocoded_stats_key)).to be true
|
|
|
|
|
expect(Rails.cache.exist?(user_2_points_geocoded_stats_key)).to be true
|
|
|
|
|
|
|
|
|
|
# Verify the cached data has the expected structure
|
|
|
|
|
user1_stats = Rails.cache.read(user_1_points_geocoded_stats_key)
|
|
|
|
|
user2_stats = Rails.cache.read(user_2_points_geocoded_stats_key)
|
|
|
|
|
|
|
|
|
|
expect(user1_stats).to be_a(Hash)
|
|
|
|
|
expect(user1_stats).to have_key(:geocoded)
|
|
|
|
|
expect(user1_stats).to have_key(:without_data)
|
|
|
|
|
expect(user1_stats[:geocoded]).to eq(3)
|
|
|
|
|
|
|
|
|
|
expect(user2_stats).to be_a(Hash)
|
|
|
|
|
expect(user2_stats).to have_key(:geocoded)
|
|
|
|
|
expect(user2_stats).to have_key(:without_data)
|
|
|
|
|
expect(user2_stats[:geocoded]).to eq(2)
|
2025-09-05 13:39:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'actually writes to cache' do
|
|
|
|
|
described_class.new.perform
|
|
|
|
|
|
2025-09-13 09:58:36 -04:00
|
|
|
expect(Rails.cache.exist?(user_1_years_tracked_key)).to be true
|
|
|
|
|
expect(Rails.cache.exist?(user_1_points_geocoded_stats_key)).to be true
|
|
|
|
|
expect(Rails.cache.exist?(user_1_countries_visited_key)).to be true
|
|
|
|
|
expect(Rails.cache.exist?(user_1_cities_visited_key)).to be true
|
|
|
|
|
expect(Rails.cache.exist?(user_2_years_tracked_key)).to be true
|
|
|
|
|
expect(Rails.cache.exist?(user_2_points_geocoded_stats_key)).to be true
|
|
|
|
|
expect(Rails.cache.exist?(user_2_countries_visited_key)).to be true
|
|
|
|
|
expect(Rails.cache.exist?(user_2_cities_visited_key)).to be true
|
2025-09-05 13:39:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'handles users with no points gracefully' do
|
|
|
|
|
user_no_points = create(:user)
|
2025-09-13 09:37:09 -04:00
|
|
|
|
2025-09-05 13:39:50 -04:00
|
|
|
expect { described_class.new.perform }.not_to raise_error
|
2025-09-13 09:37:09 -04:00
|
|
|
|
2025-09-05 13:39:50 -04:00
|
|
|
cached_stats = Rails.cache.read("dawarich/user_#{user_no_points.id}_points_geocoded_stats")
|
|
|
|
|
expect(cached_stats).to eq({ geocoded: 0, without_data: 0 })
|
|
|
|
|
end
|
|
|
|
|
end
|
2025-09-13 09:37:09 -04:00
|
|
|
end
|