Sha256: 5815adc858bad41f659b5fad31ec86b0d57264af1d03b0b17a11c9a14f11e17d

Contents?: true

Size: 1.78 KB

Versions: 23

Compression:

Stored size: 1.78 KB

Contents

require "spec_helper"

describe Mongoid::Extensions::Date::Conversions do

  before do
    @time = Date.today.to_time
  end

  describe "#set" do

    context "when string provided" do

      context "when string is a non utc time" do

        it "returns a utc time from the string" do
          Date.set(@time.to_s).should == @time.utc
        end

      end

      context "when string is a date" do

        it "returns a time from the string" do
          date = RUBY_VERSION.start_with?("1.9") ? "15/01/2007" : "01/15/2007"
          Date.set(date).should == Date.new(2007, 1, 15).at_midnight.utc
        end

      end

      context "when string is empty" do

        it "returns nil" do
          Date.set("").should == nil
        end

      end

    end

    context "when time provided" do

      it "returns the time" do
        Date.set(@time).should == @time.utc
      end

    end

    context "when a date provided" do

      it "returns a time from the date" do
        Date.set(@time.to_date).should == @time
      end

    end

  end

  describe "#get" do

    context "when value is nil" do

      it "returns nil" do
        Date.get(nil).should be_nil
      end

    end

    context "when value is not nil" do

      context "when time is UTC" do

        before do
          @utc = Date.new(1974, 12, 1).to_time.utc
        end

        context "when time zone is not utc" do

          before do
            Time.zone = "Eastern Time (US & Canada)"
          end

          after do
            Time.zone = "UTC"
          end

          it "converts to the proper date" do
            Date.get(@utc).should == Date.new(1974, 12, 1)
          end

        end

      end

      it "converts the time back to a date" do
        Date.get(@time).should == @time.to_date
      end

    end

  end

end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
mongoid-pre-2.0.0.beta1 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-2.0.0.alpha spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.2.14 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.2.13 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.2.12 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.2.11 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.2.10 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.2.9 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.2.8 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.2.7 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-pre-2.0.0.pre spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.2.6 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.2.5 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.2.4 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.2.3 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.2.2 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.2.1 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.2.0 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.1.4 spec/unit/mongoid/extensions/date/conversions_spec.rb
mongoid-1.1.3 spec/unit/mongoid/extensions/date/conversions_spec.rb