require 'rails_helper' module Logistics module Core RSpec.describe OperationService do before(:all) do @service = OperationService.new end describe '#save' do it 'creates operation' do dt = create(:declaration_type) tm = create(:transport_mode) ofr = create(:offer_request) service = OperationService.new params = { operation_number: 'OP123', contract_number: 'CN001', declaration_type_id: dt.id, is_new_customer: true, transport_mode_id: tm.id, offer_request_id: ofr.id } result, op = service.save(params) expect(result).to be_truthy expect(op.operation_number).to eq 'OP123' expect(op.contract_number).to eq 'CN001' expect(op.is_new_customer).to be_truthy end it 'saves operation with additional documents' do dt = create(:declaration_type) tm = create(:transport_mode) ofr = create(:offer_request) 2.times { create(:additional_document, documentable: dt) } create(:additional_document, documentable: tm) service = OperationService.new params = { operation_number: 'OP123', contract_number: 'CN001', declaration_type_id: dt.id, is_new_customer: true, transport_mode_id: tm.id, offer_request_id: ofr.id } result, op = service.save(params) expect(result).to be_truthy expect(op.operation_documents.count).to eq 3 end it 'saves operation with common documents' do dt = create(:declaration_type) tm = create(:transport_mode) ofr = create(:offer_request) 2.times { create(:common_document) } service = OperationService.new params = { operation_number: 'OP123', contract_number: 'CN001', declaration_type_id: dt.id, is_new_customer: true, transport_mode_id: tm.id, offer_request_id: ofr.id } result, op = service.save(params) expect(result).to be_truthy expect(op.operation_documents.count).to eq 2 end it 'adds entry to operation summary' do dt = create(:declaration_type) tm = create(:transport_mode) ofr = create(:offer_request) service = OperationService.new params = { operation_number: 'OP123', contract_number: 'CN001', declaration_type_id: dt.id, is_new_customer: true, transport_mode_id: tm.id, offer_request_id: ofr.id } result, op = service.save(params) summary = OperationSummary.find_by(operation: op) expect(result).to be_truthy expect(summary).not_to be_nil end end describe '#update_owner_data' do it 'sets owners of operations' do u1 = create(:user) op1 = create(:operation) op2 = create(:operation) params = [ { owner_id: u1.id, op_ids: [op1.id, op2.id] } ] resp = @service.instance_eval { update_owner_data([op1, op2], params) } expect(resp[0].owner_id).to eq u1.id expect(resp[1].owner_id).to eq u1.id end end describe '#assign_owner' do it 'assigns owners to operations' do u1 = create(:user) u2 = create(:user) op1 = create(:operation) op2 = create(:operation) op3 = create(:operation) op4 = create(:operation, owner: nil) params = [ { owner_id: u1.id, op_ids: [op1.id, op2.id] }, { owner_id: u2.id, op_ids: [op3.id] } ] resp = @service.assign_owner(params) op1.reload op2.reload op3.reload op4.reload expect(resp.success).to be_truthy expect(op1.owner_id).to eq u1.id expect(op2.owner_id).to eq u1.id expect(op3.owner_id).to eq u2.id expect(op4.owner_id).to be_nil end end end end end