# Copyright© 2017 Merck Sharp & Dohme Corp. a subsidiary of Merck & Co., Inc., Kenilworth, NJ, USA.  Licensed under the Apache License, Version 2.0 (the "License");    you may not use this file except in compliance with the License.    You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0     Unless required by applicable law or agreed to in writing, software    distributed under the License is distributed on an "AS IS" BASIS,    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.    See the License for the specific language governing permissions and    limitations under the License. 
require 'spec_helper'

describe DatasetScheduling, type: :model do
  describe 'validations' do
    context 'when it "cron" type' do
      it 'validates job syntax' do
        instance = described_class.new
        instance.type = 'cron'
        instance.job = 'blah'

        expect(instance.valid?).to be false
        expect(instance.errors.full_messages).to eq(['Job Invalid cron syntax'])

        instance.job = '* * * * * *'
        expect(instance.valid?).to be true
      end
    end

    context 'when the type if undefined' do
      it 'raises an exception' do
        instance = described_class.new
        expect { instance.valid?  }.to raise_error('Unknow scheduling type')
      end
    end
  end

  describe '#start_date(*)' do
    it 'can be set by simple writer' do
      date = Date.new
      instance = described_class.new(start_date: date)
      expect(instance.start_date).to eq(date)
    end

    it 'can be set by param generated by date_select' do
      instance = described_class.new(
        'start_date(1i)' => '2015',
        'start_date(2i)' => '3',
        'start_date(3i)' => '1'
      )
      expect(instance.start_date).to be_instance_of(Date)
      expect(instance.start_date.to_s).to eq('2015-03-01')
    end
  end

  describe '.default_instance' do
    it 'returns "once" instance' do
      instance = described_class.default_instance
      expect(instance).to be_instance_of(described_class)
      expect(instance.type).to eq('once')
    end
  end
end
