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