From 5fe8e3bd14f82c13479c177a0f0894a1bafb2914 Mon Sep 17 00:00:00 2001 From: Spencer Olson Date: Thu, 2 Oct 2014 11:59:09 -0500 Subject: [PATCH] set up grading_period model, migration, and tests closes CNVS-15906 test plan: -run rake db:migrate -verify the migration successfully runs, creating the grading_periods table and adding the index for course_id and account_id Change-Id: If991a00fc79bc4aecabe8189a43432e2158709c8 Reviewed-on: https://gerrit.instructure.com/42161 Tested-by: Jenkins Reviewed-by: Jacob Fugal Reviewed-by: Josh Simpson QA-Review: Amber Taniuchi Product-Review: Spencer Olson --- app/models/grading_period.rb | 15 +++ .../20141001211428_create_grading_periods.rb | 21 ++++ spec/models/grading_period_spec.rb | 103 ++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 app/models/grading_period.rb create mode 100644 db/migrate/20141001211428_create_grading_periods.rb create mode 100644 spec/models/grading_period_spec.rb diff --git a/app/models/grading_period.rb b/app/models/grading_period.rb new file mode 100644 index 00000000000..4a5a3cebdcc --- /dev/null +++ b/app/models/grading_period.rb @@ -0,0 +1,15 @@ +class GradingPeriod < ActiveRecord::Base + attr_accessible :course, :account, :weight, :start_date, :end_date + + belongs_to :course + belongs_to :account + + validates_presence_of :weight, :start_date, :end_date + validate :validate_dates + + def validate_dates + if self.start_date && self.end_date + errors.add(:end_date, t('errors.invalid_grading_period_end_date', "Grading period end date precedes start date")) if self.end_date < self.start_date + end + end +end diff --git a/db/migrate/20141001211428_create_grading_periods.rb b/db/migrate/20141001211428_create_grading_periods.rb new file mode 100644 index 00000000000..fc7dd591071 --- /dev/null +++ b/db/migrate/20141001211428_create_grading_periods.rb @@ -0,0 +1,21 @@ +class CreateGradingPeriods < ActiveRecord::Migration + tag :predeploy + + def self.up + create_table :grading_periods do |t| + t.integer :course_id, :limit => 8 + t.integer :account_id, :limit => 8 + t.float :weight, :null => false + t.datetime :start_date, :null => false + t.datetime :end_date, :null => false + t.timestamps + end + + add_index :grading_periods, :course_id + add_index :grading_periods, :account_id + end + + def self.down + drop_table :grading_periods + end +end diff --git a/spec/models/grading_period_spec.rb b/spec/models/grading_period_spec.rb new file mode 100644 index 00000000000..bd9e3113be9 --- /dev/null +++ b/spec/models/grading_period_spec.rb @@ -0,0 +1,103 @@ +# +# Copyright (C) 2014 Instructure, Inc. +# +# This file is part of Canvas. +# +# Canvas is free software: you can redistribute it and/or modify it under +# the terms of the GNU Affero General Public License as published by the Free +# Software Foundation, version 3 of the License. +# +# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more +# details. +# +# You should have received a copy of the GNU Affero General Public License along +# with this program. If not, see . +# + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb') + +describe GradingPeriod do + before (:all) do + @grading_period = GradingPeriod.create(course_id: 1, weight: 25.0, start_date: Time.zone.now, end_date: 1.day.from_now) + end + + it "should respond to course_id" do + expect(@grading_period).to respond_to(:course_id) + end + + it "should respond to account_id" do + expect(@grading_period).to respond_to(:account_id) + end + + it "should respond to weight" do + expect(@grading_period).to respond_to(:weight) + end + + it "should respond to start_date" do + expect(@grading_period).to respond_to(:start_date) + end + + it "should respond to end_date" do + expect(@grading_period).to respond_to(:end_date) + end + + it "should respond to course" do + expect(@grading_period).to respond_to(:course) + end + + it "should respond to account" do + expect(@grading_period).to respond_to(:account) + end + + context "associations" do + it "should belong to a course" do + association = GradingPeriod.reflect_on_association(:course) + association.macro.should == :belongs_to + end + + it "should belong to an account" do + association = GradingPeriod.reflect_on_association(:account) + association.macro.should == :belongs_to + end + end + + context "validation" do + it "should be valid with appropriate input" do + expect(@grading_period).to be_valid + end + + it "should require a weight" do + expect(GradingPeriod.create(course_id: 1, start_date: Time.zone.now, end_date: 1.day.from_now )).to_not be_valid + end + + it "should require a start_date" do + expect(GradingPeriod.create(course_id: 1, weight: 25.0, end_date: 1.day.from_now)).to_not be_valid + end + + it "should require an end_date" do + expect(GradingPeriod.create(course_id: 1, weight: 25.0, start_date: Time.zone.now)).to_not be_valid + end + + context "when end_date is before the start_date" do + it "should not be able to create a grading period with end_date before the start_date" do + expect(GradingPeriod.create(course_id: 1, weight: 25.0, start_date: 2.days.from_now, end_date: Time.zone.now)).to_not be_valid + end + + it "should not be able to update the end_date to be before the start_date" do + grading_period = GradingPeriod.create(course_id: 1, weight: 25.0, start_date: Time.zone.now, end_date: 1.day.from_now) + expect(grading_period).to be_valid + grading_period.update_attributes(end_date: 1.day.ago) + expect(grading_period).not_to be_valid + end + + it "should not be able to update the start_date to be after the end_date" do + grading_period = GradingPeriod.create(course_id: 1, weight: 25.0, start_date: Time.zone.now, end_date: 1.day.from_now) + expect(grading_period).to be_valid + grading_period.update_attributes(start_date: 2.days.from_now) + expect(grading_period).not_to be_valid + end + end + end +end