spec/lib/idy/extension/find_spec.rb in idy-0.1.2 vs spec/lib/idy/extension/find_spec.rb in idy-0.1.3

- old
+ new

@@ -1,73 +1,86 @@ # frozen_string_literal: true require 'rails_helper' RSpec.describe Article do - context 'on reload' do - subject { described_class.create id: 42, title: 'title' } + context 'with idy enabled' do + context 'on reload' do + subject { described_class.create id: 42, title: 'title' } - it 'reloads' do - record = described_class.find(subject.to_param) + it 'reloads' do + record = described_class.find(subject.to_param) - record.update_attribute :title, 'title.updated' + record.update_attribute :title, 'title.updated' - expect(subject.title).to eq 'title' - expect(subject.reload.title).to eq 'title.updated' - end + expect(subject.title).to eq 'title' + expect(subject.reload.title).to eq 'title.updated' + end - context 'while locking' do - it 'does not throw an error' do - expect(-> { subject.lock! }).not_to raise_error + context 'while locking' do + it 'does not throw an error' do + expect(-> { subject.lock! }).not_to raise_error + end end end - end - context 'finding one record as id' do - subject { described_class.find record.id } + context 'finding one record as id' do + subject { described_class.find record.id } - let!(:record) { described_class.create id: 42, title: 'title-1' } + let!(:record) { described_class.create id: 42, title: 'title-1' } - specify { expect(subject).to eq record } - end + specify { expect(subject).to eq record } + end - context 'finding one record as idy' do - subject { described_class.find record.to_param } + context 'finding one record as idy' do + subject { described_class.find record.to_param } - let!(:record) { described_class.create id: 42, title: 'title-1' } + let!(:record) { described_class.create id: 42, title: 'title-1' } - specify { expect(subject).to eq record } - end + specify { expect(subject).to eq record } + end - context 'finding multiple records' do - subject { described_class.find [record_1.to_param, record_2.to_param] } + context 'finding multiple records' do + subject { described_class.find [record_1.to_param, record_2.to_param] } - let!(:record_1) { described_class.create id: 42, title: 'title-1' } - let!(:record_2) { described_class.create id: 1 , title: 'title-2' } + let!(:record_1) { described_class.create id: 42, title: 'title-1' } + let!(:record_2) { described_class.create id: 1 , title: 'title-2' } - it 'finds all' do - expect(subject).to eq [record_1, record_2] + it 'finds all' do + expect(subject).to eq [record_1, record_2] + end end - end - context 'finding via has_many' do - let!(:article) { described_class.create } - let!(:comment) { Comment.create article: article } + context 'finding via has_many' do + let!(:article) { described_class.create } + let!(:comment) { Comment.create article: article } - specify { expect(article.comments.find(comment.id)).to eq comment } + specify { expect(article.comments.find(comment.id)).to eq comment } + end + + context 'when id is an integer like' do + let!(:record) { described_class.create id: 1 } + let!(:idy) { '1a' } + + it 'does not tries to use the integer part on normal find' do + expect { described_class.find(idy) }.to raise_error ActiveRecord::RecordNotFound, %(Couldn't find User with 'idy'="1a") + end + end end - context 'finding a class with no callback declared' do + context 'with idy disabled' do let!(:record) { Clean.create } context 'of one record' do - specify { expect(Clean.find(record.id)).to eq record } + it 'finds by normal id' do + expect(Clean.find(record.id)).to eq record + end end context 'of multiple record' do let!(:record_2) { Clean.create } - specify do + it 'finds by normal id' do expect(Clean.find([record.id, record_2.id])).to eq [record, record_2] end end end end