test/unit/invoice_test.rb in xero_gateway-2.1.0 vs test/unit/invoice_test.rb in xero_gateway-2.3.0

- old
+ new

@@ -1,9 +1,20 @@ require File.join(File.dirname(__FILE__), '../test_helper.rb') class InvoiceTest < Test::Unit::TestCase + context "with line item totals" do + + should "allow setting and reading these as instance variables without downloading line items" do + invoice = create_test_invoice(:line_items_downloaded => false, :total => 6969_00) + + assert !invoice.line_items_downloaded? + XeroGateway::Invoice.any_instance.expects(:download_line_items).never + assert_equal 6969_00, invoice.total + end + end + context "building and parsing XML" do should "work vice versa" do invoice = create_test_invoice # Generate the XML message @@ -28,11 +39,11 @@ end end # Tests the sub_total calculation and that setting it manually doesn't modify the data. def test_invoice_sub_total_calculation - invoice = create_test_invoice + invoice = create_test_invoice(:line_items_downloaded => true) line_item = invoice.line_items.first # Make sure that everything adds up to begin with. expected_sub_total = invoice.line_items.inject(BigDecimal.new('0')) { | sum, line_item | line_item.line_amount } assert_equal(expected_sub_total, invoice.sub_total) @@ -48,12 +59,12 @@ expected_sub_total = invoice.line_items.inject(BigDecimal.new('0')) { | sum, line_item | line_item.line_amount } assert_equal(expected_sub_total, invoice.sub_total) end # Tests the total_tax calculation and that setting it manually doesn't modify the data. - def test_invoice_sub_total_calculation - invoice = create_test_invoice + def test_invoice_sub_total_calculation2 + invoice = create_test_invoice(:line_items_downloaded => true) line_item = invoice.line_items.first # Make sure that everything adds up to begin with. expected_total_tax = invoice.line_items.inject(BigDecimal.new('0')) { | sum, line_item | line_item.tax_amount } assert_equal(expected_total_tax, invoice.total_tax) @@ -69,21 +80,22 @@ expected_total_tax = invoice.line_items.inject(BigDecimal.new('0')) { | sum, line_item | line_item.tax_amount } assert_equal(expected_total_tax, invoice.total_tax) end # Tests the total calculation and that setting it manually doesn't modify the data. - def test_invoice_sub_total_calculation - invoice = create_test_invoice + def test_invoice_sub_total_calculation3 + invoice = create_test_invoice(:line_items_downloaded => true) + assert invoice.line_items_downloaded? line_item = invoice.line_items.first # Make sure that everything adds up to begin with. expected_total = invoice.sub_total + invoice.total_tax assert_equal(expected_total, invoice.total) # Change the total and check that it doesn't modify anything. invoice.total = expected_total * 10 - assert_equal(expected_total, invoice.total) + assert_equal(expected_total.to_f, invoice.total.to_f) # Change the quantity of the first line item and make sure that # everything still continues to add up. line_item.quantity = line_item.quantity + 5 assert_not_equal(expected_total, invoice.total) @@ -144,10 +156,11 @@ # Test invoice defaults. assert_equal('ACCREC', invoice.invoice_type) assert_kind_of(Date, invoice.date) assert_kind_of(Date, invoice.due_date) + assert_kind_of(Time, invoice.updated_date_utc) assert_equal('12345', invoice.invoice_number) assert_equal('MY REFERENCE FOR THIS INVOICE', invoice.reference) assert_equal("Exclusive", invoice.line_amount_types) # Test the contact defaults. @@ -243,20 +256,27 @@ invoice = create_test_invoice(:url => 'http://example.com', :branding_theme_id => 'a94a78db-5cc6-4e26-a52b-045237e56e6e') assert_equal 'http://example.com', invoice.url assert_equal 'a94a78db-5cc6-4e26-a52b-045237e56e6e', invoice.branding_theme_id end + def test_updated_date_utc + time = Time.now.utc + invoice = create_test_invoice(:updated_date_utc => time) + assert_equal time, invoice.updated_date_utc + end + private def create_test_invoice(invoice_params = {}, contact_params = {}, line_item_params = []) unless invoice_params.nil? invoice_params = { :invoice_type => 'ACCREC', :date => Date.today, :due_date => Date.today + 10, # 10 days in the future :invoice_number => '12345', :reference => "MY REFERENCE FOR THIS INVOICE", - :line_amount_types => "Exclusive" + :line_amount_types => "Exclusive", + :updated_date_utc => Time.now.utc }.merge(invoice_params) end invoice = XeroGateway::Invoice.new(invoice_params || {}) unless contact_params.nil?