<commit_before>require 'test_helper'
class C1TaskTest < ActiveSupport::TestCase
  include ::Validators
  include ActiveJob::TestHelper

  def setup
    collection_fixtures('product_tests', 'products', 'bundles',
                        'measures', 'records', 'patient_cache',
                        'health_data_standards_svs_value_sets')
    load_library_functions
    @product_test = ProductTest.find('51703a883054cf84390000d3')
  end

  def test_create
    assert @product_test.tasks.create({}, C1Task)
  end

  def test_should_exclude_c3_validators_when_no_c3
    @product_test.tasks.clear
    task = @product_test.tasks.create({}, C1Task)

    task.validators.each do |v|
      assert !v.is_a?(MeasurePeriodValidator)
    end
  end

  def test_should_be_able_to_test_a_good_archive_of_qrda_files
    task = @product_test.tasks.create({}, C1Task)
    zip = File.new(File.join(Rails.root, 'test/fixtures/product_tests/ep_qrda_test_good.zip'))
    perform_enqueued_jobs do
      te = task.execute(zip)
      te.reload
      assert te.execution_errors.where(:msg_type => :error).empty?, 'should be no errors for good cat I archive'
    end
  end

  def test_should_be_able_to_tell_when_wrong_number_of_documents_are_supplied_in_archive
    task = @product_test.tasks.create({}, C1Task)
    @product_test.product.c1_test = true
    zip = File.new(File.join(Rails.root, 'test/fixtures/product_tests/ep_qrda_test_too_many_files.zip'))
    perform_enqueued_jobs do
      te = task.execute(zip)
      te.reload
      assert_equal 1, te.execution_errors.where(:msg_type => :error).count, 'should be 1 error from cat I archive'
      assert_equal 1, te.execution_errors.where(:message => '4 files expected but was 5', :msg_type => :error).count
    end
  end

  def test_should_be_able_to_tell_when_wrong_names_are_provided_in_documents
    task = @product_test.tasks.create({}, C1Task)
    @product_test.product.c1_test = true
    zip = File.new(File.join(Rails.root, 'test/fixtures/product_tests/ep_qrda_test_wrong_names.zip'))
    perform_enqueued_jobs do
      te = task.execute(zip)
      te.reload
      assert_equal 2, te.execution_errors.where(:msg_type => :error).count, 'should be 2 errors from cat I archive'
      assert_equal 'Patient name \'GP_PEDS CPPP\' declared in file not found in test records', te.execution_errors[0].message
      assert_equal 'Records for patients GP_PEDS C not found in archive as expected', te.execution_errors[1].message
    end
  end

  def test_should_have_c3_execution_as_sibling_test_execution_when_c3_task_exists
    c1_task = @product_test.tasks.create!({}, C1Task)
    c3_task = @product_test.tasks.create!({}, C3Cat1Task)
    @product_test.product.c3_test = true
    zip = File.new(File.join(Rails.root, 'test/fixtures/product_tests/ep_qrda_test_good.zip'))
    perform_enqueued_jobs do
      te = c1_task.execute(zip)
      assert_equal c3_task.test_executions.first.id.to_s, te.sibling_execution_id
    end
  end

  def test_should_return_warning_when_incorrect_templates_in_c1_without_c3
    ptest = ProductTest.find('51703a883054cf84390000d3')
    task = ptest.tasks.create({}, C1Task)
    zip = File.new(File.join(Rails.root, 'test/fixtures/product_tests/ep_qrda_test_too_much_data.zip'))
    perform_enqueued_jobs do
      te = task.execute(zip)
      te.reload<commit_msg>test has to state it is a c1_test<commit_after>require 'test_helper'
class C1TaskTest < ActiveSupport::TestCase
  include ::Validators
  include ActiveJob::TestHelper

  def setup
    collection_fixtures('product_tests', 'products', 'bundles',
                        'measures', 'records', 'patient_cache',
                        'health_data_standards_svs_value_sets')
    load_library_functions
    @product_test = ProductTest.find('51703a883054cf84390000d3')
  end

  def test_create
    assert @product_test.tasks.create({}, C1Task)
  end

  def test_should_exclude_c3_validators_when_no_c3
    @product_test.tasks.clear
    task = @product_test.tasks.create({}, C1Task)

    task.validators.each do |v|
      assert !v.is_a?(MeasurePeriodValidator)
    end
  end

  def test_should_be_able_to_test_a_good_archive_of_qrda_files
    task = @product_test.tasks.create({}, C1Task)
    zip = File.new(File.join(Rails.root, 'test/fixtures/product_tests/ep_qrda_test_good.zip'))
    perform_enqueued_jobs do
      te = task.execute(zip)
      te.reload
      assert te.execution_errors.where(:msg_type => :error).empty?, 'should be no errors for good cat I archive'
    end
  end

  def test_should_be_able_to_tell_when_wrong_number_of_documents_are_supplied_in_archive
    task = @product_test.tasks.create({}, C1Task)
    @product_test.product.c1_test = true
    zip = File.new(File.join(Rails.root, 'test/fixtures/product_tests/ep_qrda_test_too_many_files.zip'))
    perform_enqueued_jobs do
      te = task.execute(zip)
      te.reload
      assert_equal 1, te.execution_errors.where(:msg_type => :error).count, 'should be 1 error from cat I archive'
      assert_equal 1, te.execution_errors.where(:message => '4 files expected but was 5', :msg_type => :error).count
    end
  end

  def test_should_be_able_to_tell_when_wrong_names_are_provided_in_documents
    task = @product_test.tasks.create({}, C1Task)
    @product_test.product.c1_test = true
    zip = File.new(File.join(Rails.root, 'test/fixtures/product_tests/ep_qrda_test_wrong_names.zip'))
    perform_enqueued_jobs do
      te = task.execute(zip)
      te.reload
      assert_equal 2, te.execution_errors.where(:msg_type => :error).count, 'should be 2 errors from cat I archive'
      assert_equal 'Patient name \'GP_PEDS CPPP\' declared in file not found in test records', te.execution_errors[0].message
      assert_equal 'Records for patients GP_PEDS C not found in archive as expected', te.execution_errors[1].message
    end
  end

  def test_should_have_c3_execution_as_sibling_test_execution_when_c3_task_exists
    c1_task = @product_test.tasks.create!({}, C1Task)
    c3_task = @product_test.tasks.create!({}, C3Cat1Task)
    @product_test.product.c3_test = true
    zip = File.new(File.join(Rails.root, 'test/fixtures/product_tests/ep_qrda_test_good.zip'))
    perform_enqueued_jobs do
      te = c1_task.execute(zip)
      assert_equal c3_task.test_executions.first.id.to_s, te.sibling_execution_id
    end
  end

  def test_should_return_warning_when_incorrect_templates_in_c1_without_c3
    task = @product_test.tasks.create({}, C1Task)
    @product_test.product.c1_test = true
    zip = File.new(File.join(Rails.root, 'test/fixtures/product_tests/ep_qrda_test_too_much_data.zip'))
    perform_enqueued_jobs do
      te = task.execute(zip)
      te.reload
{'fraction_non_alphanumeric': 0.09624624624624625, 'fraction_numerical': 0.01891891891891892, 'mean_word_length': 4.127790608160123, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7630970', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2292, 'n_words': 408}