require 'rails_helper' module Logistics module Core RSpec.describe AdditionalDocumentService do describe '#get_lookup_values' do it 'gets values of a lookup entity' do 2.times { create(:acquisition_mode) } service = AdditionalDocumentService.new items = service.get_lookup_values('Logistics::Core::AcquisitionMode') expect(items.count).to eq 2 end end describe '#get_additional_documents' do it 'gets documents of a lookup entity' do dt1 = create(:document_type) dt2 = create(:document_type) create(:document_type) am = create(:acquisition_mode) create(:additional_document, document_type: dt1, documentable: am) create(:additional_document, document_type: dt2, documentable: am) service = AdditionalDocumentService.new documents = service.get_additional_documents('Logistics::Core::AcquisitionMode', am.id) expect(documents.count).to eq 2 end end describe '#bulk_save' do it 'saves additional documents in bulk' do dt1 = create(:document_type) dt2 = create(:document_type) tt = create(:transaction_type) am = create(:acquisition_mode) data = [ { :document_type_id => dt1.id, :documentable_type => 'Logistics::Core::ExtendedLookup', :documentable_id => am.id, :original => 2, :copy => 2, :certified => 1, :is_endorsed => true, :is_mandatory => true, :transaction_type_id => tt.id }, { :document_type_id => dt2.id, :documentable_type => 'Logistics::Core::ExtendedLookup', :documentable_id => am.id, :original => 1, :copy => 2, :certified => 1, :is_endorsed => true, :is_mandatory => true, :transaction_type_id => tt.id} ] service = AdditionalDocumentService.new success, error = service.bulk_save(data) expect(success).to be_truthy am.reload expect(am.additional_documents.count).to eq 2 end end describe '#bulk_update' do it 'saves additional documents in bulk' do am = create(:acquisition_mode) ad1 = create(:additional_document, documentable: am) ad2 = create(:additional_document, documentable: am) data = [ { :id => ad1.id, :document_type_id => ad1.document_type_id, :documentable_type => 'Logistics::Core::ExtendedLookup', :documentable_id => am.id, :original => 5, :copy => 5, :certified => 1, :is_endorsed => true, :is_mandatory => true }, { :id => ad2.id, :document_type_id => ad2.document_type.id, :documentable_type => 'Logistics::Core::ExtendedLookup', :documentable_id => am.id, :original => 5, :copy => 5, :certified => 1, :is_endorsed => true, :is_mandatory => true } ] service = AdditionalDocumentService.new success, errors = service.bulk_update(data) expect(success).to be_truthy am.reload expect(am.additional_documents.count).to eq 2 expect(am.additional_documents[0].original).to eq 5 expect(am.additional_documents[0].copy).to eq 5 expect(am.additional_documents[1].original).to eq 5 expect(am.additional_documents[1].copy).to eq 5 end end describe '#bulk_update' do it 'deletes additional documents in bulk' do am = create(:acquisition_mode) ad1 = create(:additional_document, documentable: am) ad2 = create(:additional_document, documentable: am) ad3 = create(:additional_document, documentable: am) service = AdditionalDocumentService.new success, errors = service.bulk_delete([ad1.id, ad2.id]) expect(success).to be_truthy expect(errors).to eq '' am.reload expect(am.additional_documents.count).to eq 1 expect(am.additional_documents[0].id).to eq ad3.id end end end end end