mirror of
https://github.com/internetee/registry.git
synced 2025-07-24 03:30:33 +02:00
parent
d85e57d800
commit
7723a30d1b
17 changed files with 240 additions and 200 deletions
32
test/models/invoice/vat_rate_calculator_test.rb
Normal file
32
test/models/invoice/vat_rate_calculator_test.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
require 'test_helper'
|
||||
|
||||
class Invoice::VatRateCalculatorTest < ActiveSupport::TestCase
|
||||
def test_applies_registry_vat_rate_when_registrar_is_vat_liable_locally
|
||||
registry_vat_rate = 20
|
||||
registry = Registry.new(vat_rate: registry_vat_rate, vat_country: Country.new(:us))
|
||||
registrar = Registrar.new(vat_rate: 10, vat_country: Country.new(:us))
|
||||
|
||||
vat_calculator = Invoice::VatRateCalculator.new(registry: registry, registrar: registrar)
|
||||
|
||||
assert_equal registry_vat_rate, vat_calculator.calculate
|
||||
end
|
||||
|
||||
def test_applies_registrar_vat_rate_when_registrar_is_vat_liable_in_foreign_country
|
||||
registrar_vat_rate = 20
|
||||
registry = Registry.new(vat_rate: 10, vat_country: Country.new(:gb))
|
||||
registrar = Registrar.new(vat_rate: registrar_vat_rate, vat_country: Country.new(:us))
|
||||
|
||||
vat_calculator = Invoice::VatRateCalculator.new(registry: registry, registrar: registrar)
|
||||
|
||||
assert_equal registrar_vat_rate, vat_calculator.calculate
|
||||
end
|
||||
|
||||
def test_applies_zero_vat_rate_when_registrar_is_vat_liable_in_foreign_country_and_vat_rate_is_absent
|
||||
registry = Registry.new(vat_country: Country.new(:gb))
|
||||
registrar = Registrar.new(vat_rate: nil, vat_country: Country.new(:us))
|
||||
|
||||
vat_calculator = Invoice::VatRateCalculator.new(registry: registry, registrar: registrar)
|
||||
|
||||
assert vat_calculator.calculate.zero?
|
||||
end
|
||||
end
|
|
@ -33,53 +33,11 @@ class InvoiceTest < ActiveSupport::TestCase
|
|||
assert_not Invoice.overdue.include?(@invoice), 'Should not return non-overdue invoice'
|
||||
end
|
||||
|
||||
def test_optional_vat_rate
|
||||
@invoice.vat_rate = nil
|
||||
assert @invoice.valid?
|
||||
end
|
||||
|
||||
def test_vat_rate_validation
|
||||
@invoice.vat_rate = -1
|
||||
assert @invoice.invalid?
|
||||
|
||||
@invoice.vat_rate = 0
|
||||
assert @invoice.valid?
|
||||
|
||||
@invoice.vat_rate = 99.9
|
||||
assert @invoice.valid?
|
||||
|
||||
@invoice.vat_rate = 100
|
||||
assert @invoice.invalid?
|
||||
end
|
||||
|
||||
def test_serializes_and_deserializes_vat_rate
|
||||
invoice = @invoice.dup
|
||||
invoice.items = @invoice.items
|
||||
invoice.vat_rate = BigDecimal('25.5')
|
||||
invoice.save!
|
||||
invoice.reload
|
||||
assert_equal BigDecimal('25.5'), invoice.vat_rate
|
||||
end
|
||||
|
||||
def test_vat_rate_defaults_to_effective_vat_rate_of_a_registrar
|
||||
registrar = registrars(:bestnames)
|
||||
invoice = @invoice.dup
|
||||
invoice.vat_rate = nil
|
||||
invoice.buyer = registrar
|
||||
invoice.items = @invoice.items
|
||||
|
||||
registrar.stub(:effective_vat_rate, BigDecimal(55)) do
|
||||
invoice.save!
|
||||
end
|
||||
|
||||
assert_equal BigDecimal(55), invoice.vat_rate
|
||||
end
|
||||
|
||||
def test_vat_rate_cannot_be_updated
|
||||
@invoice.vat_rate = BigDecimal(21)
|
||||
@invoice.vat_rate = BigDecimal('25.5')
|
||||
@invoice.save!
|
||||
@invoice.reload
|
||||
refute_equal BigDecimal(21), @invoice.vat_rate
|
||||
assert_equal BigDecimal('25.5'), @invoice.vat_rate
|
||||
end
|
||||
|
||||
def test_calculates_vat_amount
|
||||
|
@ -88,11 +46,6 @@ class InvoiceTest < ActiveSupport::TestCase
|
|||
assert_equal 10, invoice.vat_amount
|
||||
end
|
||||
|
||||
def test_vat_amount_is_zero_when_vat_rate_is_blank
|
||||
@invoice.vat_rate = nil
|
||||
assert_equal 0, @invoice.vat_amount
|
||||
end
|
||||
|
||||
def test_calculates_subtotal
|
||||
line_item = InvoiceItem.new
|
||||
invoice = Invoice.new(items: [line_item, line_item])
|
||||
|
|
|
@ -1,97 +0,0 @@
|
|||
require 'test_helper'
|
||||
|
||||
class RegistrarVATTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@registrar = registrars(:bestnames)
|
||||
end
|
||||
|
||||
def test_optional_vat_no
|
||||
@registrar.vat_no = ''
|
||||
assert @registrar.valid?
|
||||
|
||||
@registrar.vat_no = 'any'
|
||||
assert @registrar.valid?
|
||||
end
|
||||
|
||||
def test_apply_vat_rate_from_registry_when_registrar_is_local_vat_payer
|
||||
Setting.registry_country_code = 'US'
|
||||
@registrar.address_country_code = 'US'
|
||||
|
||||
Registry.instance.stub(:vat_rate, BigDecimal('5.5')) do
|
||||
assert_equal BigDecimal('5.5'), @registrar.effective_vat_rate
|
||||
end
|
||||
end
|
||||
|
||||
def test_require_no_vat_rate_when_registrar_is_local_vat_payer
|
||||
@registrar.vat_rate = 1
|
||||
assert @registrar.invalid?
|
||||
|
||||
@registrar.vat_rate = nil
|
||||
assert @registrar.valid?
|
||||
end
|
||||
|
||||
def test_apply_vat_rate_from_registrar_when_registrar_is_foreign_vat_payer
|
||||
Setting.registry_country_code = 'US'
|
||||
@registrar.address_country_code = 'DE'
|
||||
@registrar.vat_rate = BigDecimal('5.6')
|
||||
assert_equal BigDecimal('5.6'), @registrar.effective_vat_rate
|
||||
end
|
||||
|
||||
def test_require_vat_rate_when_registrar_is_foreign_vat_payer_and_vat_no_is_absent
|
||||
@registrar.address_country_code = 'DE'
|
||||
@registrar.vat_no = ''
|
||||
|
||||
@registrar.vat_rate = ''
|
||||
assert @registrar.invalid?
|
||||
assert @registrar.errors.added?(:vat_rate, :blank)
|
||||
|
||||
@registrar.vat_rate = 5
|
||||
assert @registrar.valid?
|
||||
end
|
||||
|
||||
def test_require_no_vat_rate_when_registrar_is_foreign_vat_payer_and_vat_no_is_present
|
||||
@registrar.address_country_code = 'DE'
|
||||
@registrar.vat_no = 'valid'
|
||||
|
||||
@registrar.vat_rate = 1
|
||||
assert @registrar.invalid?
|
||||
|
||||
@registrar.vat_rate = nil
|
||||
assert @registrar.valid?
|
||||
end
|
||||
|
||||
def test_vat_rate_validation
|
||||
@registrar.address_country_code = 'DE'
|
||||
@registrar.vat_no = ''
|
||||
|
||||
@registrar.vat_rate = -1
|
||||
assert @registrar.invalid?
|
||||
|
||||
@registrar.vat_rate = 0
|
||||
assert @registrar.valid?
|
||||
|
||||
@registrar.vat_rate = 99.9
|
||||
assert @registrar.valid?
|
||||
|
||||
@registrar.vat_rate = 100
|
||||
assert @registrar.invalid?
|
||||
end
|
||||
|
||||
def test_serializes_and_deserializes_vat_rate
|
||||
@registrar.address_country_code = 'DE'
|
||||
@registrar.vat_rate = BigDecimal('25.5')
|
||||
@registrar.save!
|
||||
@registrar.reload
|
||||
assert_equal BigDecimal('25.5'), @registrar.vat_rate
|
||||
end
|
||||
|
||||
def test_parses_vat_rate_as_a_string
|
||||
@registrar.vat_rate = '25.5'
|
||||
assert_equal BigDecimal('25.5'), @registrar.vat_rate
|
||||
end
|
||||
|
||||
def test_treats_empty_vat_rate_as_nil
|
||||
@registrar.vat_rate = ''
|
||||
assert_nil @registrar.vat_rate
|
||||
end
|
||||
end
|
|
@ -111,9 +111,82 @@ class RegistrarTest < ActiveSupport::TestCase
|
|||
assert_equal 'Main Street 1, NY, NY State, 1234', registrar.address
|
||||
end
|
||||
|
||||
def test_invalid_with_vat_rate_when_registrar_is_vat_liable_locally
|
||||
registrar = registrar_with_local_vat_liability
|
||||
|
||||
registrar.vat_rate = 1
|
||||
|
||||
assert registrar.invalid?
|
||||
assert_includes registrar.errors.full_messages_for(:vat_rate),
|
||||
'VAT rate must be blank when a registrar is VAT-registered in the same' \
|
||||
' country as registry'
|
||||
end
|
||||
|
||||
def test_invalid_with_vat_rate_when_registrar_is_vat_liable_in_foreign_country_and_vat_no_is_present
|
||||
registrar = registrar_with_foreign_vat_liability
|
||||
|
||||
registrar.vat_no = 'valid'
|
||||
registrar.vat_rate = 1
|
||||
|
||||
assert registrar.invalid?
|
||||
end
|
||||
|
||||
def test_invalid_without_vat_rate_when_registrar_is_vat_liable_in_foreign_country_and_vat_no_is_absent
|
||||
registrar = registrar_with_foreign_vat_liability
|
||||
|
||||
registrar.vat_no = ''
|
||||
registrar.vat_rate = ''
|
||||
|
||||
assert registrar.invalid?
|
||||
end
|
||||
|
||||
def test_vat_rate_validation
|
||||
registrar = registrar_with_foreign_vat_liability
|
||||
|
||||
registrar.vat_rate = -1
|
||||
assert registrar.invalid?
|
||||
|
||||
registrar.vat_rate = 0
|
||||
assert registrar.valid?
|
||||
|
||||
registrar.vat_rate = 99.9
|
||||
assert registrar.valid?
|
||||
|
||||
registrar.vat_rate = 100
|
||||
assert registrar.invalid?
|
||||
end
|
||||
|
||||
def test_serializes_and_deserializes_vat_rate
|
||||
@registrar.address_country_code = 'DE'
|
||||
@registrar.vat_rate = BigDecimal('25.5')
|
||||
@registrar.save!
|
||||
@registrar.reload
|
||||
assert_equal BigDecimal('25.5'), @registrar.vat_rate
|
||||
end
|
||||
|
||||
def test_aliases_vat_country_to_country
|
||||
vat_country = Country.new(:us)
|
||||
registrar = Registrar.new(vat_country: vat_country)
|
||||
assert_equal vat_country, registrar.vat_country
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid_registrar
|
||||
registrars(:bestnames)
|
||||
end
|
||||
end
|
||||
|
||||
def registrar_with_local_vat_liability
|
||||
registrar = valid_registrar
|
||||
registrar.vat_country = Country.new(:us)
|
||||
Registry.current.vat_country = Country.new(:us)
|
||||
registrar
|
||||
end
|
||||
|
||||
def registrar_with_foreign_vat_liability
|
||||
registrar = valid_registrar
|
||||
registrar.vat_country = Country.new(:gb)
|
||||
Registry.current.vat_country = Country.new(:us)
|
||||
registrar
|
||||
end
|
||||
end
|
|
@ -1,20 +1,12 @@
|
|||
require 'test_helper'
|
||||
|
||||
class RegistryTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@registry = Registry.send(:new)
|
||||
def test_returns_current_registry
|
||||
Setting.registry_vat_prc = 0.2
|
||||
Setting.registry_country_code = 'US'
|
||||
|
||||
registry = Registry.current
|
||||
assert_equal 20, registry.vat_rate
|
||||
assert_equal Country.new(:us), registry.vat_country
|
||||
end
|
||||
|
||||
def test_implements_singleton
|
||||
assert_equal Registry.instance.object_id, Registry.instance.object_id
|
||||
end
|
||||
|
||||
def test_vat_rate
|
||||
original_vat_prc = Setting.registry_vat_prc
|
||||
Setting.registry_vat_prc = 0.25
|
||||
|
||||
assert_equal BigDecimal(25), @registry.vat_rate
|
||||
|
||||
Setting.registry_vat_prc = original_vat_prc
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue