]> git.openstreetmap.org Git - rails.git/blob - test/jobs/trace_importer_job_test.rb
Basic tests for the trace jobs
[rails.git] / test / jobs / trace_importer_job_test.rb
1 require "test_helper"
2 require "minitest/mock"
3
4 class TraceImporterJobTest < ActiveJob::TestCase
5   def test_success_notification
6     # Check that the user gets a success notification when the trace has valid points
7     trace = create(:trace)
8
9     gpx = Minitest::Mock.new
10     def gpx.actual_points
11       5
12     end
13
14     trace.stub(:import, gpx) do
15       perform_enqueued_jobs do
16         TraceImporterJob.perform_now(trace)
17       end
18     end
19
20     assert_performed_jobs 1
21
22     email = ActionMailer::Base.deliveries.last
23     assert_equal trace.user.email, email.to[0]
24     assert_match(/success/, email.subject)
25   end
26
27   def test_failure_notification
28     # Check that the user gets a failure notification when the trace has no valid points
29     trace = create(:trace)
30
31     gpx = Minitest::Mock.new
32     def gpx.actual_points
33       0
34     end
35
36     trace.stub(:import, gpx) do
37       perform_enqueued_jobs do
38         TraceImporterJob.perform_now(trace)
39       end
40     end
41
42     assert_performed_jobs 1
43
44     email = ActionMailer::Base.deliveries.last
45     assert_equal trace.user.email, email.to[0]
46     assert_match(/failure/, email.subject)
47   end
48
49   def test_error_notification
50     # Check that the user gets a failure notification when something goes badly wrong
51     trace = create(:trace)
52     trace.stub(:import, -> { raise }) do
53       perform_enqueued_jobs do
54         TraceImporterJob.perform_now(trace)
55       end
56     end
57
58     assert_performed_jobs 1
59
60     email = ActionMailer::Base.deliveries.last
61     assert_equal trace.user.email, email.to[0]
62     assert_match(/failure/, email.subject)
63   end
64 end