require 'debci/db'
require 'debci/job'

module Debci
  class ExperimentTest < ActiveRecord::Base
    belongs_to :experiment
    belongs_to :control_job, class_name: 'Debci::Job'
    belongs_to :test_job, class_name: 'Debci::Job'

    scope :regressions, lambda {
      joins(
        'JOIN jobs control_job ON control_job.run_id = experiment_tests.control_job_id'
      ).joins(
        'JOIN jobs test_job ON test_job.run_id = experiment_tests.test_job_id'
      ).where(
        "control_job.status IN ('pass', 'neutral') AND test_job.status = 'fail'"
      )
    }

    def regression?
      ['pass', 'neutral'].include?(control_job.status) && test_job.status == 'fail'
    end

    def improvement?
      control_job.status == 'fail' && ['pass', 'neutral'].include?(test_job.status)
    end
  end
end
