require 'spec_helper'

describe FeeScheduleMileage do
  describe 'Validations' do
    it "should create a valid factory" do
      FactoryGirl.create(:fee_schedule_mileage).should be_valid
    end

    it {should validate_presence_of :fee_schedule_id}
    it {should validate_presence_of :billable_rate}
    it {should validate_presence_of :payable_rate}

    describe "should validate billable_rate is a number greater than 0" do

      context "should be a number" do
        before do
          @not_number = FactoryGirl.build(:fee_schedule_mileage, billable_rate: "Ten")
          @is_number = FactoryGirl.build(:fee_schedule_mileage, billable_rate: "10")
          @negative_number = FactoryGirl.build(:fee_schedule_mileage, billable_rate: "-1")
        end

        it "should be invalid without a number" do
          @not_number.should_not be_valid
        end

        it "should be valid with a number" do
          @is_number.should be_valid
        end

        it "should be invalid with a negative_number" do
          @negative_number.should_not be_valid
        end

      end
    end
    
    describe "should validate payable_rate is a number greater than 0" do

      context "should be a number" do
        before do
          @not_number = FactoryGirl.build(:fee_schedule_mileage, payable_rate: "Ten")
          @is_number = FactoryGirl.build(:fee_schedule_mileage, payable_rate: "10")
          @negative_number = FactoryGirl.build(:fee_schedule_mileage, payable_rate: "-1")
        end

        it "should be invalid without a number" do
          @not_number.should_not be_valid
        end

        it "should be valid with a number" do
          @is_number.should be_valid
        end

        it "should be invalid with a negative_number" do
          @negative_number.should_not be_valid
        end

      end
    end

    describe "should validate billable_rate has no more than two decimal places" do

      context "should be a number" do
        before do
          @billable1 = FactoryGirl.build(:fee_schedule_mileage, billable_rate: "10.2")
          @billable2 = FactoryGirl.build(:fee_schedule_mileage, billable_rate: "10.12")
          @billable3 = FactoryGirl.build(:fee_schedule_mileage, billable_rate: "10.123")
        end

        it "should be invalid with more than two decimal places" do
          @billable3.should_not be_valid
        end

        it "should be valid with one decimal place" do
          @billable1.should be_valid
        end

        it "should be valid with two decimal places" do
          @billable2.should be_valid
        end

      end
    end

    describe "should validate payable_rate has no more than two decimal places" do

      context "should be a number" do
        before do
          @payable1 = FactoryGirl.build(:fee_schedule_mileage, payable_rate: "10.2")
          @payable2 = FactoryGirl.build(:fee_schedule_mileage, payable_rate: "10.12")
          @payable3 = FactoryGirl.build(:fee_schedule_mileage, payable_rate: "10.123")
        end

        it "should be invalid with more than two decimal places" do
          @payable3.should_not be_valid
        end

        it "should be valid with one decimal place" do
          @payable1.should be_valid
        end

        it "should be valid with two decimal places" do
          @payable2.should be_valid
        end

      end
    end

    describe "free_miles should be an integer greater than or equal to 0" do
      context "it should be an integer" do
        before do
          @not_number = FactoryGirl.build(:fee_schedule_mileage, free_miles: "Zero")
          @not_integer = FactoryGirl.build(:fee_schedule_mileage, free_miles: "10.1")
          @integer = FactoryGirl.build(:fee_schedule_mileage, free_miles: "10")
        end

        it "should not be valid without a number" do
          @not_number.should_not be_valid
        end

        it "should not be valid without an integer" do
          @not_integer.should_not be_valid
        end

        it "should be valid with an integer" do
          @integer.should be_valid
        end

      end

      context "it should only be valid with an integer greater than or equal to zero" do

        before do
          @negative_number = FactoryGirl.build(:fee_schedule_mileage, free_miles: "-1")
          @zero = FactoryGirl.build(:fee_schedule_mileage, free_miles: "0")
          @positive_number = FactoryGirl.build(:fee_schedule_mileage, free_miles: "1")
        end

        it "should not be valid with a negative number" do
          @negative_number.should_not be_valid
        end
        
        it "should be valid with a positive number" do
          @positive_number.should be_valid
        end

        it "should be valid with a zero" do
          @zero.should be_valid
        end

      end
    end

  end

end
