require 'spec_helper' RSpec.describe Term, type: :model do let(:subject) { FactoryGirl.create(:term) } describe 'validations' do it { should validate_presence_of(:canvas_term_id) } it { should validate_uniqueness_of(:canvas_term_id) } end describe 'associations' do it do should have_many(:courses) .with_primary_key(:canvas_term_id) .with_foreign_key(:canvas_term_id) end end describe 'scopes' do describe '.active' do let!(:active_term) { FactoryGirl.create(:term, workflow_state: 'active', start_at: 2.days.ago, end_at: 3.days.from_now) } let!(:inactive_term) { FactoryGirl.create(:term, workflow_state: 'inactive') } let!(:completed_term) { FactoryGirl.create(:term, start_at: 2.months.ago, end_at: 1.month.ago) } let!(:unstarted_term) { FactoryGirl.create(:term, start_at: 6.months.from_now, end_at: 1.year.from_now) } it 'returns terms with an active workflow_state that have a start_date 15 days in the future or earlier and an end_date 15 days in the past or later' do expect(Term.active).to match_array([active_term]) end end end describe '.create_or_update' do let(:term_params) { open_canvas_fixture('terms')['enrollment_terms'][0] } context 'the term does not already exist' do it 'creates it' do expect { Term.create_or_update(term_params) }.to change { Term.count }.by(1) term = Term.last expect(term.name).to eq(term_params['name']) expect(term.start_at).to eq(term_params['start_at']) expect(term.end_at).to eq(term_params['end_at']) expect(term.canvas_term_id).to eq(term_params['id']) expect(term.grading_period_group_id).to eq(term_params['grading_period_group_id']) expect(term.sis_id).to eq(term_params['sis_term_id']) expect(term.workflow_state).to eq(term_params['workflow_state']) end end context 'the term already exists' do let!(:existing_term) { FactoryGirl.create(:term, canvas_term_id: term_params['id']) } it 'updates it' do expect { Term.create_or_update(term_params) }.to_not change { Term.count } existing_term.reload expect(existing_term.name).to eq(term_params['name']) expect(existing_term.start_at).to eq(term_params['start_at']) expect(existing_term.end_at).to eq(term_params['end_at']) expect(existing_term.canvas_term_id).to eq(term_params['id']) expect(existing_term.grading_period_group_id).to eq(term_params['grading_period_group_id']) expect(existing_term.sis_id).to eq(term_params['sis_term_id']) expect(existing_term.workflow_state).to eq(term_params['workflow_state']) end end end end