Sha256: 53cba51a6fa5238bcff4803dad68468241ab602e9c264055ce6f93774bfed5dd

Contents?: true

Size: 1.81 KB

Versions: 68

Compression:

Stored size: 1.81 KB

Contents

require "spec_helper"

describe Mongoid::Timestamps::Updated::Short do

  describe ".included" do

    let(:agent) do
      ShortAgent.new
    end

    let(:fields) do
      ShortAgent.fields
    end

    before do
      agent.run_callbacks(:create)
      agent.run_callbacks(:save)
    end

    it "does not add c_at to the document" do
      expect(fields["c_at"]).to be_nil
    end

    it "adds u_at to the document" do
      expect(fields["u_at"]).to_not be_nil
    end

    it "does not add the long updated_at" do
      expect(fields["updated_at"]).to be_nil
    end

    it "forces the updated_at timestamps to UTC" do
      expect(agent.updated_at).to be_within(10).of(Time.now.utc)
    end

    it "aliases the raw field" do
      expect(agent.u_at).to eq(agent.updated_at)
    end
  end

  context "when the document is new" do

    context "when providing the timestamp" do

      let(:time) do
        Time.new(2012, 1, 1)
      end

      let(:doc) do
        ShortAgent.create(updated_at: time)
      end

      it "does not override it with the default" do
        expect(doc.updated_at).to eq(time)
      end

      it "does not persist an auto value" do
        expect(doc.reload.updated_at).to eq(time)
      end
    end
  end

  context "when the document has not changed" do

    let(:agent) do
      ShortAgent.instantiate("_id" => BSON::ObjectId.new, "account_ids" => [])
    end

    before do
      agent.new_record = false
    end

    it "does not run the update callbacks" do
      expect(agent).to receive(:updated_at=).never
      agent.save
    end
  end

  context "when the document is created" do

    let(:agent) do
      ShortAgent.create
    end

    it "runs the update callbacks" do
      expect(agent.updated_at).to_not be_nil
      expect(agent.updated_at).to be_within(10).of(Time.now.utc)
    end
  end
end

Version data entries

68 entries across 63 versions & 3 rubygems

Version Path
mongoid-7.0.13 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-7.0.12 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-6.4.8 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-7.0.11 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-7.0.10 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-6.4.7 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-6.4.5 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-7.0.8 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-7.0.7 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-7.0.6 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-5.4.1 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-7.0.5 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-7.0.4 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-6.4.4 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-7.0.3 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-6.4.2 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-7.0.2 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-7.0.1 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-6.4.1 spec/mongoid/timestamps/updated/short_spec.rb
mongoid-5.4.0 spec/mongoid/timestamps/updated/short_spec.rb