Sha256: 743fa6ae972cc3b3f33b6ffbf8fc3d8b1a0893fe0f6b9c953d50bb68a4737a57

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

require "spec_helper"

module Uuids
  describe "Base.has_uuids" do

    # Dependencies
    let(:uuids_class) { Uuids::Models::Uuid }

    # For this specification, the City AR model has been prepared in the
    # `spec dummy/app/models/city.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_class.first.try(:record)).to eq subject
      expect(subject.uuids.first).to eq uuids_class.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 the #uuid= method" do
      value = "3ea8fd89-232f-4ed1-90b5-743da173cd7d"
      subject.uuid = value
      expect(subject.uuids.map(&:value)).to include(value)
    end

    it "defines the #uuids= method" do
      value = "3ea8fd89-232f-4ed1-90b5-743da173cd7d"
      subject.uuids = [value]
      expect(subject.uuids.map(&:value)).to include(value)
    end

    it "defines +by_uuid+ class scope" do
      subject.save!
      subject.uuids.create!
      values = subject.reload.uuids.map(&:value)
      expect(City.by_uuid(*values).to_a).to eq [subject]
      expect(City.by_uuid).to eq City.all
    end

    it "creates the first uuid by default" do
      expect(subject.uuid).to be_present
    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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
uuids-5.0.0 spec/tests/lib/base/has_uuids_spec.rb
uuids-4.1.8 spec/uuids/lib/base/has_uuids_spec.rb