Skip to content

Commit

Permalink
CleanupJob fixes and specs coverage (#12)
Browse files Browse the repository at this point in the history
* CleanupJob fixes and specs coverage, closes #4

* Rubocop fixes

* Add missing frozen string literal comment
  • Loading branch information
mbajur authored Nov 26, 2024
1 parent 98e7a97 commit e8100e3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/jobs/inner_performance/cleanup_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
module InnerPerformance
class CleanupJob < ApplicationJob
def perform
InnerPerformance::Event.where('created_at >= ?', InnerPerformance.configuration.events_retention)
InnerPerformance::Event
.where('created_at < ?', InnerPerformance.configuration.events_retention.ago)
.destroy_all
end
end
end
1 change: 1 addition & 0 deletions inner_performance.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ Gem::Specification.new do |spec|
spec.add_dependency 'rails', '>= 7.1.5'
spec.add_dependency 'ransack', '>= 4.2.1'

spec.add_development_dependency 'factory_bot'
spec.add_development_dependency 'rspec-rails'
end
7 changes: 7 additions & 0 deletions spec/factories/events.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

FactoryBot.define do
factory :event, class: 'InnerPerformance::Event' do
name { 'x' }
end
end
14 changes: 14 additions & 0 deletions spec/jobs/inner_performance/cleanup_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require 'rails_helper'

describe InnerPerformance::CleanupJob do
subject { described_class.perform_now }

let!(:old_event) { create(:event, created_at: 7.days.ago) }
let!(:fresh_event) { create(:event, created_at: 6.days.ago) }

it 'removes jobs older than InnerPerformance.configuration.events_retention' do
expect { subject }.to(change(InnerPerformance::Event.all, :count).by(-1))
end
end
5 changes: 5 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
# that will avoid rails generators crashing because migrations haven't been run yet
# return unless Rails.env.test?
require 'rspec/rails'
require 'factory_bot'
# Add additional requires below this line. Rails is not loaded until this point!

FactoryBot.find_definitions

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
Expand All @@ -40,6 +43,8 @@
abort e.to_s.strip
end
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods

# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_paths = [
Rails.root.join('spec/fixtures')
Expand Down

0 comments on commit e8100e3

Please sign in to comment.