Skip to content
Randy Russell edited this page Apr 15, 2018 · 1 revision

tests/fixtures/job.yml

a:
  name: a

tests/helpers/job_helpers_test.rb

# frozen_string_literal: true

require 'test_helper'

##
# Test for helper methods for Jobs
#
class JobsHelperTest < ActionView::TestCase
  setup do
    @jobs = Job.all
  end

  test 'should return empty with no entries in the database' do
    jobs = []
    assert_match '', sort_jobs_by_dependancies(jobs, [])
  end

  test 'The result should be a sequence consisting of a single job a' do
    assert_match 'a', sort_jobs_by_dependancies(@jobs, [])
  end

  test 'result should be a sequence containing all three jobs abc in no
        significant order' do
      Job.create(name: 'b')
      Job.create(name: 'c')
      @jobs = Job.all #adding more jobs to the total now
      assert_match 'a, b, c', sort_jobs_by_dependancies(@jobs, [])
  end

  test 'result should be a sequence that positions c before b, containing all
        three jobs abc. c depends on b' do
    Job.create(name: 'c')
    Job.create(name: 'b', dependant: Job.where(name: 'c')[0].id)
    @jobs = Job.all
    jobs_with_dependancies = find_jobs_with_dependancies(@jobs)
    assert_match 'c, b, a', sort_jobs_by_dependancies(@jobs, jobs_with_dependancies)
  end

  test 'result should be a sequence that positions
        f before c, c before b, b before e and a before d, containing all six
        jobs abcdef' do
    Job.create(name: 'a')
    Job.create(name: 'f')
    Job.create(name: 'c', dependant: Job.where(name: 'f')[0].id)
    Job.create(name: 'b', dependant: Job.where(name: 'c')[0].id)
    Job.create(name: 'd', dependant: Job.where(name: 'a')[0].id)
    Job.create(name: 'e', dependant: Job.where(name: 'b')[0].id)
    @jobs = Job.all
    jobs_with_dependancies = find_jobs_with_dependancies(@jobs)
    assert_match 'f, c, a, d, b, e', sort_jobs_by_dependancies(@jobs, jobs_with_dependancies)
  end
end

tests/models/job_test.rb

require 'test_helper'

class JobTest < ActiveSupport::TestCase
  setup do
    @job_1 = Job.create(name: 'a')
    @job_2 = Job.create(name: 'b')
    @job_3 = Job.create(name: 'c')
  end

  # custom validation tests
  test 'should not save jobs cant depend on themselves.' do
    refute @job_1.update(dependant: @job_1.id)
  end

  test 'no circular dependancies.' do
    @job_1.update(dependant: @job_2.id)
    @job_2.update(dependant: @job_3.id)
    refute @job_3.update(dependant: @job_1.id)
  end
end
Clone this wiki locally