Add tests to invoice

This commit is contained in:
Martin Lensment 2015-04-10 13:19:54 +03:00
parent 416842f924
commit 7eff257434
7 changed files with 119 additions and 26 deletions

View file

@ -4,6 +4,8 @@ class Invoice < ActiveRecord::Base
has_many :invoice_items has_many :invoice_items
accepts_nested_attributes_for :invoice_items accepts_nested_attributes_for :invoice_items
validates :invoice_type, :due_date, :currency, :seller_name, :seller_iban, :buyer_name, :invoice_items, :vat_prc, presence: true
def seller_address def seller_address
[seller_street, seller_city, seller_state, seller_zip].reject(&:blank?).compact.join(', ') [seller_street, seller_city, seller_state, seller_zip].reject(&:blank?).compact.join(', ')
end end

View file

@ -1,18 +1,18 @@
class AddInvoiceColumns < ActiveRecord::Migration class AddInvoiceColumns < ActiveRecord::Migration
def change def change
# invoice info # invoice info
# invoice number comes from id # invoice number comes from id # e-invoice
add_column :invoices, :invoice_type, :string, null: false # CRE / DEB add_column :invoices, :invoice_type, :string, null: false # CRE / DEB # e-invoice
# add_column :invoices, :document_name, :string, null: false # Invoice / credit invoice ... # add_column :invoices, :document_name, :string, null: false # Invoice / credit invoice ... # e-invoice
add_column :invoices, :due_date, :datetime, null: false add_column :invoices, :due_date, :datetime, null: false # e-invoice
add_column :invoices, :payment_term, :string # free text add_column :invoices, :payment_term, :string # free text
add_column :invoices, :currency, :string, null: false add_column :invoices, :currency, :string, null: false # e-invoice
add_column :invoices, :description, :string add_column :invoices, :description, :string
add_column :invoices, :reference_no, :string add_column :invoices, :reference_no, :string
add_column :invoices, :vat_prc, :decimal add_column :invoices, :vat_prc, :decimal, null: false
#add_column :invoices, :total_sum, :decimal # calculate on the fly #add_column :invoices, :total_sum, :decimal # calculate on the fly # e-invoice
add_column :invoices, :paid_at, :datetime # maybe figure this out from transactions add_column :invoices, :paid_at, :datetime # maybe figure this out from transactions
# seller info # seller info
@ -20,11 +20,11 @@ class AddInvoiceColumns < ActiveRecord::Migration
# add_column :invoices, :sellable_type, :string # add_column :invoices, :sellable_type, :string
add_column :invoices, :seller_id, :integer add_column :invoices, :seller_id, :integer
add_column :invoices, :seller_name, :string, null: false add_column :invoices, :seller_name, :string, null: false # e-invoice
add_column :invoices, :seller_reg_no, :string add_column :invoices, :seller_reg_no, :string
add_column :invoices, :seller_iban, :string, null: false add_column :invoices, :seller_iban, :string, null: false # e-invoice
add_column :invoices, :seller_bank, :string, null: false add_column :invoices, :seller_bank, :string
add_column :invoices, :seller_swift, :string, null: false add_column :invoices, :seller_swift, :string
add_column :invoices, :seller_vat_no, :string add_column :invoices, :seller_vat_no, :string
add_column :invoices, :seller_country_code, :string add_column :invoices, :seller_country_code, :string
@ -43,8 +43,8 @@ class AddInvoiceColumns < ActiveRecord::Migration
# add_column :invoices, :payable_type, :string # add_column :invoices, :payable_type, :string
add_column :invoices, :buyer_id, :integer add_column :invoices, :buyer_id, :integer
add_column :invoices, :buyer_name, :string, null: false add_column :invoices, :buyer_name, :string, null: false # e-invoice
add_column :invoices, :buyer_reg_no, :string, null: false add_column :invoices, :buyer_reg_no, :string
add_column :invoices, :buyer_country_code, :string add_column :invoices, :buyer_country_code, :string
add_column :invoices, :buyer_state, :string add_column :invoices, :buyer_state, :string
@ -55,7 +55,7 @@ class AddInvoiceColumns < ActiveRecord::Migration
add_column :invoices, :buyer_url, :string add_column :invoices, :buyer_url, :string
add_column :invoices, :buyer_email, :string add_column :invoices, :buyer_email, :string
add_column :invoices, :buyer_contact_name, :string # add_column :invoices, :buyer_contact_name, :string
end end
end end

View file

@ -3,7 +3,7 @@ class CreateInvoiceItems < ActiveRecord::Migration
create_table :invoice_items do |t| create_table :invoice_items do |t|
t.integer :invoice_id t.integer :invoice_id
# t.string :product_code # t.string :product_code
t.string :description, null: false t.string :description, null: false # e-invoice
t.string :unit t.string :unit
t.integer :amount t.integer :amount
t.decimal :price t.decimal :price

View file

@ -225,14 +225,14 @@ ActiveRecord::Schema.define(version: 20150413140933) do
t.string "currency", null: false t.string "currency", null: false
t.string "description" t.string "description"
t.string "reference_no" t.string "reference_no"
t.decimal "vat_prc" t.decimal "vat_prc", null: false
t.datetime "paid_at" t.datetime "paid_at"
t.integer "seller_id" t.integer "seller_id"
t.string "seller_name", null: false t.string "seller_name", null: false
t.string "seller_reg_no" t.string "seller_reg_no"
t.string "seller_iban", null: false t.string "seller_iban", null: false
t.string "seller_bank", null: false t.string "seller_bank"
t.string "seller_swift", null: false t.string "seller_swift"
t.string "seller_vat_no" t.string "seller_vat_no"
t.string "seller_country_code" t.string "seller_country_code"
t.string "seller_state" t.string "seller_state"
@ -245,7 +245,7 @@ ActiveRecord::Schema.define(version: 20150413140933) do
t.string "seller_contact_name" t.string "seller_contact_name"
t.integer "buyer_id" t.integer "buyer_id"
t.string "buyer_name", null: false t.string "buyer_name", null: false
t.string "buyer_reg_no", null: false t.string "buyer_reg_no"
t.string "buyer_country_code" t.string "buyer_country_code"
t.string "buyer_state" t.string "buyer_state"
t.string "buyer_street" t.string "buyer_street"
@ -254,7 +254,6 @@ ActiveRecord::Schema.define(version: 20150413140933) do
t.string "buyer_phone" t.string "buyer_phone"
t.string "buyer_url" t.string "buyer_url"
t.string "buyer_email" t.string "buyer_email"
t.string "buyer_contact_name"
end end
create_table "keyrelays", force: :cascade do |t| create_table "keyrelays", force: :cascade do |t|

View file

@ -1,9 +1,12 @@
Fabricator(:invoice) do Fabricator(:invoice) do
invoice_type 'DEB' buyer_name 'Registrar 1'
due_date { Time.zone.now.to_date + 1.day }
payment_term { 'Prepayment' }
currency { 'EUR' } currency { 'EUR' }
description { 'Invoice no. 1' } due_date { Time.zone.now.to_date + 1.day }
description { 'Invoice no. 1' } invoice_type 'DEB'
domain seller_iban { '123' }
seller_name { 'EIS' }
seller_city { 'Tallinn' }
seller_street { 'Paldiski mnt. 123' }
invoice_items(count: 2)
vat_prc 0.2
end end

View file

@ -0,0 +1,6 @@
Fabricator(:invoice_item) do
description { 'add money' }
unit 1
amount 1
price 150
end

View file

@ -0,0 +1,83 @@
require 'rails_helper'
describe Invoice do
it { should belong_to(:seller) }
it { should belong_to(:buyer) }
it { should have_many(:invoice_items) }
context 'with invalid attribute' do
before :all do
@invoice = Invoice.new
end
it 'should not be valid' do
@invoice.valid?
@invoice.errors.full_messages.should match_array([
"Buyer name is missing",
"Currency is missing",
"Due date is missing",
"Invoice items is missing",
"Invoice type is missing",
"Seller iban is missing",
"Seller name is missing",
"Vat prc is missing"
])
end
# it 'should not have any versions' do
# @invoice.versions.should == []
# end
end
context 'with valid attributes' do
before :all do
@invoice = Fabricate(:invoice)
end
it 'should be valid' do
@invoice.valid?
@invoice.errors.full_messages.should match_array([])
end
it 'should be valid twice' do
@invoice = Fabricate(:invoice)
@invoice.valid?
@invoice.errors.full_messages.should match_array([])
end
it 'should be valid twice' do
@invoice = Fabricate(:invoice)
@invoice.valid?
@invoice.errors.full_messages.should match_array([])
end
it 'should return correct addresses' do
@invoice = Fabricate(:invoice)
@invoice.seller_address.should == 'Paldiski mnt. 123, Tallinn'
end
it 'should calculate totals correctly' do
@invoice = Fabricate(:invoice)
@invoice.vat_prc.should == BigDecimal.new('0.2')
@invoice.total_without_vat.should == BigDecimal.new('300.0')
@invoice.total_vat.should == BigDecimal.new('60.0')
@invoice.total.should == BigDecimal.new('360.0')
ii = @invoice.items.first
ii.item_total_without_vat.should == BigDecimal.new('150.0')
ii = @invoice.items.last
ii.item_total_without_vat.should == BigDecimal.new('150.0')
end
# it 'should have one version' do
# with_versioning do
# @invoice.versions.should == []
# @invoice.body = 'New body'
# @invoice.save
# @invoice.errors.full_messages.should match_array([])
# @invoice.versions.size.should == 1
# end
# end
end
end