<commit_before>require 'spec_helper'

describe QuotasCopyWorker do
  let(:taxonomy) {
    create(:taxonomy,
           :name => "CITES_EU")
  }
  let(:geo_entity) {
    create(:geo_entity,
           :name_en => "Portugal")
  }
  let(:taxon_concept) {
    create(:taxon_concept,
          :taxonomy_id => taxonomy.id)
  }
  let!(:quota) {
    create(:quota,
           :start_date => 1.year.ago,
           :end_date => 1.month.ago,
           :geo_entity_id => geo_entity.id,
           :taxon_concept_id => taxon_concept.id,
           :is_current => true,
           :notes => "Le Caviar Quota Forever"
          )
  }

  describe "Copy single quota, for a given year" do
    before(:each) do
      QuotasCopyWorker.new.perform({
        "from_year" => quota.start_date.year,
        "start_date" => Time.now.strftime("%d/%m/%Y"),
        "end_date" => 1.day.from_now.strftime("%d/%m/%Y"),
        "publication_date" => Time.now.strftime("%d/%m/%Y"),
        "excluded_taxon_concepts_ids" => nil,
        "included_taxon_concepts_ids" => nil,
        "excluded_geo_entities_ids" => nil,
        "included_geo_entities_ids" => nil,
        "from_text" => '',
        "to_text" => ''
      })
    end
    specify { Quota.count(true).should == 2}
    specify { Quota.where(:is_current => true).count(true).should == 1}
    specify { Quota.where(:is_current => false).count(true).should == 1}
    specify { Quota.where(:is_current => false).first.id.should == quota.id}
  end

  describe "Try to copy quota from wrong year" do
    before(:each) do
      QuotasCopyWorker.new.perform({
        "from_year" => quota.start_date.year+1,
        "start_date" => Time.now.strftime("%d/%m/%Y"),
        "end_date" => 1.day.from_now.strftime("%d/%m/%Y"),
        "publication_date" => Time.now.strftime("%d/%m/%Y"),
        "excluded_taxon_concepts_ids" => nil,
        "included_taxon_concepts_ids" => nil,
        "excluded_geo_entities_ids" => nil,
        "included_geo_entities_ids" => nil,
        "from_text" => '',
        "to_text" => ''
      })
    end
    specify { Quota.count(true).should == 1}
    specify { Quota.where(:is_current => true).count(true).should == 1}
    specify { Quota.where(:is_current => false).count(true).should == 0}
  end

  describe "Copy quota when there are no current quotas" do
    before(:each) do
      quota.is_current = false
      quota.save
      QuotasCopyWorker.new.perform({
        "from_year" => quota.start_date.year,
        "start_date" => Time.now.strftime("%d/%m/%Y"),
        "end_date" => 1.day.from_now.strftime("%d/%m/%Y"),
        "publication_date" => Time.now.strftime("%d/%m/%Y"),
        "excluded_taxon_concepts_ids" => nil,
        "included_taxon_concepts_ids" => nil,
        "excluded_geo_entities_ids" => nil,
        "included_geo_entities_ids" => nil,
        "from_text" => '',
        "to_text" => ''
      })
    end
    specify { Quota.count(true).should == 1}
    specify { Quota.where(:is_current => true).count(true).should == 0}
    specify { Quota.where(:is_current => false).count(true).should == 1}
  end

  describe "When multiple quotas copy quota for given country" do
    before(:each) do
      geo_entity2 = create(:geo_entity)
      tc2 = create(:taxon_concept,
                   :taxonomy_id => taxonomy.id)
      @quota2 = create(:quota,
             :start_date => quota.start_date,
             :end_date => quota.end_date,
             :is_current => true,
             :geo_entity_id => geo_entity2.id,
             :taxon_concept_id => tc2.id)
      QuotasCopyWorker.new.perform({
        "from_year" => quota.start_date.year,
        "start_date" => Time.now.strftime("%d/%m/%Y"),
        "end_date" => 1.day.from_now.strftime("%d/%m/%Y"),
        "publication_date" => Time.now.strftime("%d/%m/%Y"),
        "excluded_taxon_concepts_ids" => nil,
        "included_taxon_concepts_ids" => nil,
        "excluded_geo_entities_ids" => nil,
        "included_geo_entities_ids" => [geo_entity.id],
        "from_text" => '',
        "to_text" => ''
      })
    end
    specify { Quota.count(true).should == 3}
    specify { Quota.where(:is_current => true).count(true).should == 2}
    specify { Quota.where(:is_current => true).map(&:id).include?(@quota2.id) }
    specify { Quota.where(:is_current => false).count(true).should == 1}
    specify { Quota.where(:is_current => false).first.id.should == quota.id }
  end

  describe "When multiple quotas copy quota for both countries" do
    before(:each) do
      geo_entity2 = create(:geo_entity)
      tc2 = create(:taxon_concept,
                   :taxonomy_id => taxonomy.id)
      @quota2 = create(:quota,
             :start_date => quota.start_date,
             :end_date => quota.end_date,
             :is_current => true,
             :geo_entity_id => geo_entity2.id,
             :taxon_concept_id => tc2.id)
      QuotasCopyWorker.new.perform({
        "from_year" => quota.start_date.year,
        "start_date" => Time.now.strftime("%d/%m/%Y"),
        "end_date" => 1.day.from_now.strftime("%d/%m/%Y"),
        "publication_date" => Time.now.strftime("%d/%m/%Y"),
        "excluded_taxon_concepts_ids" => nil,
        "included_taxon_concepts_ids" => nil,
        "excluded_geo_entities_ids" => nil,
        "included_geo_entities_ids" => [geo_entity.id.to_s, geo_entity2.id.to_s],
        "from_text" => '',
        "to_text" => ''
      })
    end
    specify { Quota.count(true).should == 4}
    specify { Quota.where(:is_current => true).count(true).should == 2}
    specify { Quota.where(:is_current => false).count(true).should == 2}
    specify { Quota.where(:is_current => false).first.id.should == quota.id }
    specify { Quota.where(:is_current => false).map(&:id).include?(@quota2.id) }
  end

  describe "When multiple quotas don't copy quota for given country" do
    before(:each) do
      geo_entity2 = create(:geo_entity)
      tc2 = create(:taxon_concept,
                   :taxonomy_id => taxonomy.id)
      @quota2 = create(:quota,
             :start_date => quota.start_date,
             :end_date => quota.end_date,
             :is_current => true,
             :geo_entity_id => geo_entity2.id,
             :taxon_concept_id => tc2.id)
      QuotasCopyWorker.new.perform({
        "from_year" => quota.start_date.year,
        "start_date" => Time.now.strftime("%d/%m/%Y"),
        "end_date" => 1.day.from_now.strftime("%d/%m/%Y"),
        "publication_date" => Time.now.strftime("%d/%m/%Y"),
        "excluded_taxon_concepts_ids" => nil,
        "included_taxon_concepts_ids" => nil,
        "excluded_geo_entities_ids" => [geo_entity2.id],
        "included_geo_entities_ids" => nil,
        "from_text" => '',
        "to_text" => ''
      })
    end
    specify { Quota.count(true).should == 3}<commit_msg>Geo entities passed should be an array of strings<commit_after>require 'spec_helper'

describe QuotasCopyWorker do
  let(:taxonomy) {
    create(:taxonomy,
           :name => "CITES_EU")
  }
  let(:geo_entity) {
    create(:geo_entity,
           :name_en => "Portugal")
  }
  let(:taxon_concept) {
    create(:taxon_concept,
          :taxonomy_id => taxonomy.id)
  }
  let!(:quota) {
    create(:quota,
           :start_date => 1.year.ago,
           :end_date => 1.month.ago,
           :geo_entity_id => geo_entity.id,
           :taxon_concept_id => taxon_concept.id,
           :is_current => true,
           :notes => "Le Caviar Quota Forever"
          )
  }

  describe "Copy single quota, for a given year" do
    before(:each) do
      QuotasCopyWorker.new.perform({
        "from_year" => quota.start_date.year,
        "start_date" => Time.now.strftime("%d/%m/%Y"),
        "end_date" => 1.day.from_now.strftime("%d/%m/%Y"),
        "publication_date" => Time.now.strftime("%d/%m/%Y"),
        "excluded_taxon_concepts_ids" => nil,
        "included_taxon_concepts_ids" => nil,
        "excluded_geo_entities_ids" => nil,
        "included_geo_entities_ids" => nil,
        "from_text" => '',
        "to_text" => ''
      })
    end
    specify { Quota.count(true).should == 2}
    specify { Quota.where(:is_current => true).count(true).should == 1}
    specify { Quota.where(:is_current => false).count(true).should == 1}
    specify { Quota.where(:is_current => false).first.id.should == quota.id}
  end

  describe "Try to copy quota from wrong year" do
    before(:each) do
      QuotasCopyWorker.new.perform({
        "from_year" => quota.start_date.year+1,
        "start_date" => Time.now.strftime("%d/%m/%Y"),
        "end_date" => 1.day.from_now.strftime("%d/%m/%Y"),
        "publication_date" => Time.now.strftime("%d/%m/%Y"),
        "excluded_taxon_concepts_ids" => nil,
        "included_taxon_concepts_ids" => nil,
        "excluded_geo_entities_ids" => nil,
        "included_geo_entities_ids" => nil,
        "from_text" => '',
        "to_text" => ''
      })
    end
    specify { Quota.count(true).should == 1}
    specify { Quota.where(:is_current => true).count(true).should == 1}
    specify { Quota.where(:is_current => false).count(true).should == 0}
  end

  describe "Copy quota when there are no current quotas" do
    before(:each) do
      quota.is_current = false
      quota.save
      QuotasCopyWorker.new.perform({
        "from_year" => quota.start_date.year,
        "start_date" => Time.now.strftime("%d/%m/%Y"),
        "end_date" => 1.day.from_now.strftime("%d/%m/%Y"),
        "publication_date" => Time.now.strftime("%d/%m/%Y"),
        "excluded_taxon_concepts_ids" => nil,
        "included_taxon_concepts_ids" => nil,
        "excluded_geo_entities_ids" => nil,
        "included_geo_entities_ids" => nil,
        "from_text" => '',
        "to_text" => ''
      })
    end
    specify { Quota.count(true).should == 1}
    specify { Quota.where(:is_current => true).count(true).should == 0}
    specify { Quota.where(:is_current => false).count(true).should == 1}
  end

  describe "When multiple quotas copy quota for given country" do
    before(:each) do
      geo_entity2 = create(:geo_entity)
      tc2 = create(:taxon_concept,
                   :taxonomy_id => taxonomy.id)
      @quota2 = create(:quota,
             :start_date => quota.start_date,
             :end_date => quota.end_date,
             :is_current => true,
             :geo_entity_id => geo_entity2.id,
             :taxon_concept_id => tc2.id)
      QuotasCopyWorker.new.perform({
        "from_year" => quota.start_date.year,
        "start_date" => Time.now.strftime("%d/%m/%Y"),
        "end_date" => 1.day.from_now.strftime("%d/%m/%Y"),
        "publication_date" => Time.now.strftime("%d/%m/%Y"),
        "excluded_taxon_concepts_ids" => nil,
        "included_taxon_concepts_ids" => nil,
        "excluded_geo_entities_ids" => nil,
        "included_geo_entities_ids" => [geo_entity.id],
        "from_text" => '',
        "to_text" => ''
      })
    end
    specify { Quota.count(true).should == 3}
    specify { Quota.where(:is_current => true).count(true).should == 2}
    specify { Quota.where(:is_current => true).map(&:id).include?(@quota2.id) }
    specify { Quota.where(:is_current => false).count(true).should == 1}
    specify { Quota.where(:is_current => false).first.id.should == quota.id }
  end

  describe "When multiple quotas copy quota for both countries" do
    before(:each) do
      geo_entity2 = create(:geo_entity)
      tc2 = create(:taxon_concept,
                   :taxonomy_id => taxonomy.id)
      @quota2 = create(:quota,
             :start_date => quota.start_date,
             :end_date => quota.end_date,
             :is_current => true,
             :geo_entity_id => geo_entity2.id,
             :taxon_concept_id => tc2.id)
      QuotasCopyWorker.new.perform({
        "from_year" => quota.start_date.year,
        "start_date" => Time.now.strftime("%d/%m/%Y"),
        "end_date" => 1.day.from_now.strftime("%d/%m/%Y"),
        "publication_date" => Time.now.strftime("%d/%m/%Y"),
        "excluded_taxon_concepts_ids" => nil,
        "included_taxon_concepts_ids" => nil,
        "excluded_geo_entities_ids" => nil,
        "included_geo_entities_ids" => [geo_entity.id.to_s, geo_entity2.id.to_s],
        "from_text" => '',
        "to_text" => ''
      })
    end
    specify { Quota.count(true).should == 4}
    specify { Quota.where(:is_current => true).count(true).should == 2}
    specify { Quota.where(:is_current => false).count(true).should == 2}
    specify { Quota.where(:is_current => false).first.id.should == quota.id }
    specify { Quota.where(:is_current => false).map(&:id).include?(@quota2.id) }
  end

  describe "When multiple quotas don't copy quota for given country" do
    before(:each) do
      geo_entity2 = create(:geo_entity)
      tc2 = create(:taxon_concept,
                   :taxonomy_id => taxonomy.id)
      @quota2 = create(:quota,
             :start_date => quota.start_date,
             :end_date => quota.end_date,
             :is_current => true,
             :geo_entity_id => geo_entity2.id,
             :taxon_concept_id => tc2.id)
      QuotasCopyWorker.new.perform({
        "from_year" => quota.start_date.year,
        "start_date" => Time.now.strftime("%d/%m/%Y"),
        "end_date" => 1.day.from_now.strftime("%d/%m/%Y"),
        "publication_date" => Time.now.strftime("%d/%m/%Y"),
        "excluded_taxon_concepts_ids" => nil,
        "included_taxon_concepts_ids" => nil,
        "excluded_geo_entities_ids" => [geo_entity2.id.to_s],
        "included_geo_entities_ids" => nil,
        "from_text" => '',
        "to_text" => ''
      })
    end
    specify { Quota.count(true).should == 3}
{'fraction_non_alphanumeric': 0.16147017827011959, 'fraction_numerical': 0.006455872643239674, 'mean_word_length': 2.5518499218342887, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 207, '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': '6632548', 'n_tokens_mistral': 4874, 'n_tokens_neox': 4706, 'n_words': 735}