Sha256: c889b65a325e0dc6e759885a6abcb9eb571b04477d0362aafaed97041133afc4

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

require "spec_helper"

module Uuids
  describe Base do

    describe ".included" do

      context "from non AR class" do

        before { class Test; end }
        after  { begin; Uuids::Base.send :remove_constant, :Test; rescue; end }

        it "fails" do
          expect { Test.include Uuids::Base }.to raise_error
        end
      end

      context "from AR model" do

        it "doesn't fail" do
          expect { City.include Uuids::Base }.not_to raise_error
        end
      end
    end

    describe ".has_uuids" do

      # For running this specification, the City AR model prepared in the
      # `spec dummy/app/models/item.rb`. The model included Uuids::Base.
      before  { City.send :has_uuids }
      subject { City.new }

      it "defines the #uuids attribute" do
        subject.uuids.new value: SecureRandom.uuid
        subject.save!
        expect(Uuids::Uuid.first.try(:record)).to eq subject
        expect(subject.uuids.first).to eq Uuids::Uuid.first
      end

      it "defines the #uuid method" do
        2.times { subject.uuids.new value: SecureRandom.uuid }
        subject.save!
        expect(subject.uuid).to eq subject.uuids.first.value
      end

      it "defines +by_uuid+ class scope" do
        subject.save!
        expect(City.by_uuid(subject.uuid).first).to eq subject
      end

      it "creates the first uuid by default" do
        subject.save!
        expect(subject.uuid).not_to be_blank
      end

      it "requires the #uuids attribute" do
        subject.save!
        subject.uuids.each(&:delete)
        expect(subject.reload).not_to be_valid
      end

      it "forbids destruction if uuids present" do
        subject.save!
        expect { subject.destroy! }.to raise_error
        subject.uuids.each(&:delete)
        subject.reload
        expect { subject.destroy! }.not_to raise_error
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
uuids-1.0.1 spec/lib/uuids/base_spec.rb
uuids-1.0.0 spec/lib/uuids/base_spec.rb
uuids-1.0.0.pre.rc1 spec/lib/uuids/base_spec.rb