Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure there are UserStat records before running #2426

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/jobs/user_stat_collection_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ class UserStatCollectionJob < ApplicationJob
queue_as :default

def perform(*_args)
# Do something later
return unless UserStat.exists?

importer = Hyrax::UserStatImporter.new(verbose: true, logging: true)
importer.import
UserStatCollectionJob.set(wait_until: Date.tomorrow.midnight).perform_later
Expand Down
10 changes: 10 additions & 0 deletions spec/factories/user_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true
FactoryBot.define do
factory :user_stat do
user_id { create(:user).id }
date { Time.zone.today }
file_views { rand(1..100) }
file_downloads { rand(1..100) }
work_views { rand(1..100) }
end
end
25 changes: 21 additions & 4 deletions spec/jobs/user_stat_collection_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,27 @@

let(:account) { create(:account_with_public_schema) }

describe '#reenqueue' do
it 'Enques an TenantMaintenanceJob after perform' do
switch!(account)
expect { UserStatCollectionJob.perform_now }.to have_enqueued_job(UserStatCollectionJob)
describe '#perform' do
context 'when UserStat records exist' do
before do
FactoryBot.create(:user_stat) # Ensure a UserStat record exists
end
after do
# Ensure we reset to the default tenant after each test
Apartment::Tenant.switch!(Apartment.default_tenant)
end

ShanaLMoore marked this conversation as resolved.
Show resolved Hide resolved
it 'enqueues UserStatCollectionJob after perform' do
switch!(account)
ShanaLMoore marked this conversation as resolved.
Show resolved Hide resolved
expect { UserStatCollectionJob.perform_now }.to have_enqueued_job(UserStatCollectionJob)
end
end

context 'when no UserStat records exist' do
it 'does not enqueue UserStatCollectionJob' do
switch!(account)
expect { UserStatCollectionJob.perform_now }.not_to have_enqueued_job(UserStatCollectionJob)
end
end
end
end
Loading