9888: Add find_reusable unit test.
authorTom Clegg <tom@curoverse.com>
Thu, 8 Sep 2016 20:56:42 +0000 (16:56 -0400)
committerTom Clegg <tom@curoverse.com>
Thu, 8 Sep 2016 20:56:42 +0000 (16:56 -0400)
services/api/test/fixtures/jobs.yml
services/api/test/unit/job_test.rb

index 584cc23f391e3613bba1a6b6d009cb20e86561eb..1a06d573d914462bdcabd8a1a0bcb25008bc36ff 100644 (file)
@@ -465,9 +465,9 @@ job_with_latest_version:
   supplied_script_version: master
   script_parameters:
     input: 1f4b0bc7583c2a7f9102c395f4ffc5e3+45
-  created_at: <%= 4.minute.ago.to_s(:db) %>
-  started_at: <%= 3.minute.ago.to_s(:db) %>
-  finished_at: <%= 2.minute.ago.to_s(:db) %>
+  created_at: <%= 3.minute.ago.to_s(:db) %>
+  started_at: <%= 2.minute.ago.to_s(:db) %>
+  finished_at: <%= 1.minute.ago.to_s(:db) %>
   running: false
   success: true
   output: fa7aeb5140e2848d39b416daeef4ffc5+45
index 1d3abac60a8070b9c70726e5f3ef10ba43202d6e..3da2c836ed61579fe1d5058e4c367db2c9dd3eab 100644 (file)
@@ -462,4 +462,26 @@ class JobTest < ActiveSupport::TestCase
     b = {'z' => [[{'b' => 'b', 'a' => 'a'}]]}
     assert_equal Job.deep_sort_hash(a).to_json, Job.deep_sort_hash(b).to_json
   end
+
+  test 'find_reusable' do
+    foobar = jobs(:foobar)
+    example_attrs = {
+      script_version: foobar.script_version,
+      script: foobar.script,
+      script_parameters: foobar.script_parameters,
+      repository: foobar.repository,
+    }
+
+    # Two matching jobs exist with identical outputs. The older one
+    # should be reused.
+    j = Job.find_reusable(example_attrs, {}, [], [users(:active)])
+    assert j
+    assert_equal foobar.uuid, j.uuid
+
+    # Two matching jobs exist with different outputs. Neither should
+    # be reused.
+    Job.where(uuid: jobs(:job_with_latest_version).uuid).
+      update_all(output: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+1')
+    assert_nil Job.find_reusable(example_attrs, {}, [], [users(:active)])
+  end
 end