require 'test_helper' require 'mocha/test_unit' class TaxRateTest < Test::Unit::TestCase include TestHelper def setup @client = Xeroizer::PublicApplication.new(CONSUMER_KEY, CONSUMER_SECRET) end should "have a primary key value of :name" do assert_equal :name, Xeroizer::Record::TaxRate.primary_key_name end should "build and save a tax rate with components via PUT" do @client.expects(:http_post).with { |client, url, body, extra_params| url == "https://api.xero.com/api.xro/2.0/TaxRates" && body == expected_tax_rate_create_body }.returns(tax_rate_create_successful_response) tax_rate = @client.TaxRate.build(name: 'Test Tax Rate') tax_rate.add_tax_component(name: 'Tax Component', rate: '10.0') tax_rate.save assert_equal "Test Tax Rate", tax_rate.name assert_equal "ACTIVE", tax_rate.status assert tax_rate.tax_type.present? assert_equal 1, tax_rate.tax_components.size tax_component = tax_rate.tax_components.first assert_equal "Tax Component", tax_component.name assert_equal "10.0", tax_component.rate.to_s end should "allow reading of the ReportTaxType" do @client.expects(:http_get).with { |client, url, extra_params| url == "https://api.xero.com/api.xro/2.0/TaxRates" }.returns(tax_rate_get_response) rates = @client.TaxRate.all assert_equal "OUTPUT", rates.first.report_tax_type end should "allow GET with a filter on ReportTaxType" do expected_url = "https://api.xero.com/api.xro/2.0/TaxRates" expected_params = {:where => 'ReportTaxType=="OUTPUT"'} @client.expects(:http_get).with { |client, url, extra_params| url == expected_url && extra_params == expected_params }.returns(tax_rate_get_response) @client.TaxRate.all(where: {report_tax_type: 'OUTPUT'}) end def expected_tax_rate_create_body <<-EOS Test Tax Rate Tax Component 10.0 EOS end def tax_rate_create_successful_response <<-EOS d1ec84aa-189f-4e08-b78f-ad62cf4935f4 OK Xero API Previewer 2014-07-02T19:26:53.3217153Z Test Tax Rate TAX018 true true true true true 10.0000 10.0000 ACTIVE Tax Component 10.0000 false EOS end def tax_rate_get_response <<-EOS 53715e8f-f080-4cd8-a077-1100c0512d44 OK Xero API Previewer 2014-10-28T15:39:58.5477531Z Tax on Sales OUTPUT OUTPUT true true true true true 9.2500 9.2500 ACTIVE Sales Tax 9.2500 false EOS end end