Skip to content

Commit

Permalink
Fix finished_at filters and add a couple of tests
Browse files Browse the repository at this point in the history
Follow up to rails#111
  • Loading branch information
rosa committed Dec 2, 2024
1 parent de40350 commit 8336d0c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
13 changes: 7 additions & 6 deletions app/controllers/concerns/mission_control/jobs/job_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ module MissionControl::Jobs::JobFilters

private
def set_filters
@job_filters = { job_class_name: params.dig(:filter, :job_class_name).presence, queue_name: params.dig(:filter, :queue_name).presence,
finished_at: finished_at_range_params }.compact
@job_filters = {
job_class_name: params.dig(:filter, :job_class_name).presence,
queue_name: params.dig(:filter, :queue_name).presence,
finished_at: finished_at_range_params
}.compact
end

def active_filters?
Expand All @@ -24,9 +27,7 @@ def finished_at_range_params
end
end

def date_with_time_zone(date)
if date.present?
DateTime.parse(date).in_time_zone
end
def parse_with_time_zone(date)
DateTime.parse(date).in_time_zone if date.present?
end
end
10 changes: 10 additions & 0 deletions test/active_job/jobs_relation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ class ActiveJob::JobsRelationTest < ActiveSupport::TestCase
assert_equal "MyJob", jobs.job_class_name
end

test "set finished_at range" do
jobs = @jobs.where(finished_at: (1.day.ago..))
assert 1.hour.ago.in? jobs.finished_at

# Supports concatenation without overriding exising properties
jobs = jobs.where(queue_name: "my_queue")
assert_equal "my_queue", jobs.queue_name
assert 1.hour.ago.in? jobs.finished_at
end

test "caches the fetched set of jobs" do
ActiveJob::Base.queue_adapter.expects(:fetch_jobs).twice.returns([ :job_1, :job_2 ], [])
ActiveJob::Base.queue_adapter.expects(:supports_job_filter?).at_least_once.returns(true)
Expand Down
21 changes: 21 additions & 0 deletions test/controllers/jobs_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ class MissionControl::Jobs::JobsControllerTest < ActionDispatch::IntegrationTest
assert_select "div.is-danger", "failed"
end

test "get finished jobs filtered by finished_at date" do
job = DummyJob.perform_later(42)
perform_enqueued_jobs_async

get mission_control_jobs.application_jobs_url(@application, :finished)
assert_response :ok
assert_select "tr.job", 1

get mission_control_jobs.application_jobs_url(@application, :finished, filter: { finished_at_start: 1.hour.from_now.to_s })
assert_response :ok
assert_select "tr.job", 0

get mission_control_jobs.application_jobs_url(@application, :finished, filter: { finished_at_start: 1.hour.ago.to_s, finished_at_end: 1.hour.from_now })
assert_response :ok
assert_select "tr.job", 1

get mission_control_jobs.application_jobs_url(@application, :finished, filter: { finished_at_end: 1.hour.from_now })
assert_response :ok
assert_select "tr.job", 1
end

test "redirect to queue when job doesn't exist" do
job = DummyJob.perform_later(42)

Expand Down

0 comments on commit 8336d0c

Please sign in to comment.