<commit_before>require 'spec_helper'

describe ImportSourceDataTask do
  let(:job) { jobs(:default) }
  let(:source_dataset) { datasets(:table) }

  describe '#truncate' do
    let(:planned_job_task) do
      {
        :action => 'import_source_data',
        :is_new_table => false,
        :index => 1000,
        :source_id => source_dataset.id,
        :destination_id => '2',
        :row_limit => '500',
        :truncate => "false"
      }
    end

    it "is coerced to a boolean" do
      task = JobTask.assemble!(planned_job_task, job)
      task.reload.payload.truncate.should == false
    end
  end

  describe '.assemble!' do
    context 'with a destination dataset' do
      let(:import_data) do
        {
          :source_id => source_dataset.id,
          :destination_id => '2',
          :row_limit => '500',
          :truncate => false
        }
      end
      let(:planned_job_task) do
        {
          :action => 'import_source_data',
          :is_new_table => false,
        }.merge!(import_data)
      end

      it "should make an associated ImportSourceDataTask" do
        expect {
          expect {
            JobTask.assemble!(planned_job_task, job)
          }.to change(ImportSourceDataTask, :count).by(1)
        }.to change(job.job_tasks.reload, :count).by(1)
      end

      it "should create an ImportTemplate as its payload" do
        task = JobTask.assemble!(planned_job_task, job)
        task.payload.source.should == source_dataset
        task.payload.destination_id.should == import_data[:destination_id].to_i
        task.payload.truncate.should == import_data[:truncate]
        task.payload.row_limit.should == import_data[:row_limit].to_i
      end

      it 'should set the display name' do
        task = JobTask.assemble!(planned_job_task, job)
        task.name.should == "Import from " + source_dataset.name
      end
    end

    context 'with a new destination table' do
      let(:import_data) do
        {
          :source_id => source_dataset.id,
          :destination_name => 'FOO',
          :row_limit => '500',
          :truncate => false
        }
      end
      let(:planned_job_task) do
        {
          :action => 'import_source_data',
          :is_new_table => false,
        }.merge!(import_data)
      end

      it "should make an ImportSourceDataTask" do
        task = JobTask.assemble!(planned_job_task, job)
        task.payload.source_id.should == import_data[:source_id]
        task.payload.destination_name.should == import_data[:destination_name]
        task.payload.truncate.should == import_data[:truncate]
        task.payload.row_limit.should == import_data[:row_limit].to_i
        task.id.should_not be_nil
      end

      it 'should set the display name' do
        task = JobTask.assemble!(planned_job_task, job)
        task.name.should == "Import from " + source_dataset.name
      end

      it 'cannot have the same destination_name as any existing dataset in the sandbox' do
        import_data[:destination_name] = job.workspace.sandbox.datasets.first.name
        expect {
          JobTask.assemble!(planned_job_task, job)
        }.to raise_error(ActiveRecord::RecordInvalid)
      end
    end
  end

  describe '#execute' do
    let(:isdt) { job_tasks(:isdt) }

    before do<commit_msg>Remove reference to ImportSourceDataTask #name<commit_after>require 'spec_helper'

describe ImportSourceDataTask do
  let(:job) { jobs(:default) }
  let(:source_dataset) { datasets(:table) }

  describe '#truncate' do
    let(:planned_job_task) do
      {
        :action => 'import_source_data',
        :is_new_table => false,
        :index => 1000,
        :source_id => source_dataset.id,
        :destination_id => '2',
        :row_limit => '500',
        :truncate => "false"
      }
    end

    it "is coerced to a boolean" do
      task = JobTask.assemble!(planned_job_task, job)
      task.reload.payload.truncate.should == false
    end
  end

  describe '.assemble!' do
    context 'with a destination dataset' do
      let(:import_data) do
        {
          :source_id => source_dataset.id,
          :destination_id => '2',
          :row_limit => '500',
          :truncate => false
        }
      end
      let(:planned_job_task) do
        {
          :action => 'import_source_data',
          :is_new_table => false,
        }.merge!(import_data)
      end

      it "should make an associated ImportSourceDataTask" do
        expect {
          expect {
            JobTask.assemble!(planned_job_task, job)
          }.to change(ImportSourceDataTask, :count).by(1)
        }.to change(job.job_tasks.reload, :count).by(1)
      end

      it "should create an ImportTemplate as its payload" do
        task = JobTask.assemble!(planned_job_task, job)
        task.payload.source.should == source_dataset
        task.payload.destination_id.should == import_data[:destination_id].to_i
        task.payload.truncate.should == import_data[:truncate]
        task.payload.row_limit.should == import_data[:row_limit].to_i
      end
    end

    context 'with a new destination table' do
      let(:import_data) do
        {
          :source_id => source_dataset.id,
          :destination_name => 'FOO',
          :row_limit => '500',
          :truncate => false
        }
      end
      let(:planned_job_task) do
        {
          :action => 'import_source_data',
          :is_new_table => false,
        }.merge!(import_data)
      end

      it "should make an ImportSourceDataTask" do
        task = JobTask.assemble!(planned_job_task, job)
        task.payload.source_id.should == import_data[:source_id]
        task.payload.destination_name.should == import_data[:destination_name]
        task.payload.truncate.should == import_data[:truncate]
        task.payload.row_limit.should == import_data[:row_limit].to_i
        task.id.should_not be_nil
      end

      it 'cannot have the same destination_name as any existing dataset in the sandbox' do
        import_data[:destination_name] = job.workspace.sandbox.datasets.first.name
        expect {
          JobTask.assemble!(planned_job_task, job)
        }.to raise_error(ActiveRecord::RecordInvalid)
      end
    end
  end

  describe '#execute' do
    let(:isdt) { job_tasks(:isdt) }

    before do
{'fraction_non_alphanumeric': 0.1070122435999364, 'fraction_numerical': 0.005406264906980442, 'mean_word_length': 2.3404142326075412, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, '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': '2262910', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1870, 'n_words': 411}