require 'rails_helper'

describe SourceFile::MySource do
  subject { create('source_file/my_source', original_file: fixture_file("source_files/my_source_1.csv") ) }
  let(:model) { SourceRecord::MySource }

  describe '#execute_loading_process after_activate_hook' do
    it 'calls hooks' do
      expect(WorkflowInstance).to receive(:create_or_update_from_my_source)
      subject.execute_loading_process
    end
  end

  describe "#execute_loading_process" do

    it "loads" do
      subject.execute_loading_process
      subject.should_not have_log_entries /ERROR/
      subject.should have_log_entries 'Inladen gereed'

      model.count.should eq 2

      record1 = model.first
      record1.my_date.should eq "2015-05-09".to_date
      record1.my_var.should eq "abc D"
      record1.my_count.should eq 19
      record1.my_num.should eq 4.8
      record1.my_truth.should be true

      record2 = model.last
      record2.my_date.should eq "2015-11-09".to_date
      record2.my_var.should eq "def G"
      record2.my_count.should eq 19
      record2.my_num.should eq 7.8
      record2.my_truth.should be false
    end

    it "handles loading errors" do
      expect( subject ).to receive(:after_activate_hook) { raise "Oops, did not see that one coming :(" }
      subject.execute_loading_process
      subject.should have_log_entries /ERROR/
    end
  end
end
