Sha256: 7f59d4d41cdd37c4e6ebfdb817f48eeaa88c9714b2a6751e16dd195da19721da

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 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.map(&:value).sort.first
      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

1 entries across 1 versions & 1 rubygems

Version Path
uuids-1.0.2 spec/lib/uuids/base_spec.rb