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 "#first_line" do
    it "returns the first line" do
      subject.first_line(subject.original_file.path).should eq "my_date,my_var,My count,my_num,my_truth"
    end
  end

  describe ".last_current_day" do
    it "returns the latest non nil non failed source file current_at_day stamp" do
      create 'source_file/my_source'
      create 'source_file/my_source', state: 'active',    current_at_day: '2016-08-26'
      create 'source_file/my_source', state: 'scheduled', current_at_day: '2016-08-28'
      create 'source_file/my_source', state: 'inactive',  current_at_day: '2016-08-24'
      create 'source_file/my_source', state: 'failed',    current_at_day: '2016-08-29'
      create 'source_file/my_source'
      described_class.last_current_day.iso8601.should eq '2016-08-28'
    end

    it "works without source files" do
      described_class.last_current_day.should be nil
    end
  end

  describe "#source" do
    it "can be a user" do
      user = create :user
      source_file = create 'source_file/my_source', original_file: fixture_file("source_files/my_source_1.csv"), creator: user
      user.source_files.should eq [source_file]
      SourceFile.find(source_file.id).creator.should eq user
    end
  end

  describe '#default_file' do
    it "returns the original_file" do
      subject.default_file.path.should end_with 'my_source_1.csv'
    end
  end
end
