foodsoft/spec/models/group_order_invoice_spec.rb

61 lines
2.2 KiB
Ruby
Raw Normal View History

2021-12-23 01:52:37 +01:00
require_relative '../spec_helper'
describe GroupOrderInvoice do
let(:user) { create :user, groups: [create(:ordergroup)] }
let(:supplier) { create :supplier }
let(:article) { create :article, supplier: supplier }
2021-12-24 13:35:26 +01:00
let(:order) { create :order }
let(:group_order) { create :group_order, order: order, ordergroup: user.ordergroup }
2021-12-23 01:52:37 +01:00
describe 'erroneous group order invoice' do
let(:goi) { create :group_order_invoice, group_order_id: group_order.id }
it 'does not create group order invoice if tax_number not set' do
2021-12-24 13:35:26 +01:00
expect { goi }.to raise_error(ActiveRecord::RecordInvalid)
2021-12-23 01:52:37 +01:00
end
end
describe 'valid group order invoice' do
before do
2021-12-24 13:35:26 +01:00
FoodsoftConfig[:contact][:tax_number] = 123_457_8
2021-12-23 01:52:37 +01:00
end
invoice_number1 = Time.now.strftime("%Y%m%d") + '0001'
invoice_number2 = Time.now.strftime("%Y%m%d") + '0002'
2021-12-24 13:35:26 +01:00
let(:user2) { create :user, groups: [create(:ordergroup)] }
2021-12-23 01:52:37 +01:00
2021-12-24 13:35:26 +01:00
let(:goi1) { create :group_order_invoice, group_order_id: group_order.id }
let(:goi2) { create :group_order_invoice, group_order_id: group_order.id }
2021-12-23 01:52:37 +01:00
2021-12-24 13:35:26 +01:00
let(:group_order2) { create :group_order, order: order, ordergroup: user2.ordergroup }
2021-12-23 01:52:37 +01:00
2021-12-24 13:35:26 +01:00
let(:goi3) { create :group_order_invoice, group_order_id: group_order2.id }
let(:goi4) { create :group_order_invoice, group_order_id: group_order2.id, invoice_number: invoice_number1 }
2021-12-23 01:52:37 +01:00
it 'creates group order invoice if tax_number is set' do
2021-12-24 13:35:26 +01:00
expect(goi1).to be_valid
2021-12-23 01:52:37 +01:00
end
it 'sets invoice_number according to date' do
number = Time.now.strftime("%Y%m%d") + '0001'
2021-12-24 13:35:26 +01:00
expect(goi1.invoice_number).to eq(number.to_i)
2021-12-23 01:52:37 +01:00
end
it 'fails to create if group_order_id is used multiple times for creation' do
2021-12-24 13:35:26 +01:00
expect(goi1.group_order.id).to eq(group_order.id)
expect { goi2 }.to raise_error(ActiveRecord::RecordNotUnique)
2021-12-23 01:52:37 +01:00
end
it 'creates two different group order invoice with different invoice_numbers' do
2021-12-24 13:35:26 +01:00
expect(goi1.invoice_number).to eq(invoice_number1.to_i)
expect(goi3.invoice_number).to eq(invoice_number2.to_i)
2021-12-23 01:52:37 +01:00
end
it 'fails to create two different group order invoice with same invoice_numbers' do
2021-12-24 13:35:26 +01:00
goi1
expect { goi4 }.to raise_error(ActiveRecord::RecordInvalid)
2021-12-23 01:52:37 +01:00
end
end
2021-12-24 13:35:26 +01:00
end