diff --git a/CHANGELOG.md b/CHANGELOG.md index 8763c7fb6a..e569f664fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - [PR #1329](https://github.com/stympy/faker/pull/1329) Update docs on behavior of price [@softwaregravy](https://github.com/softwaregravy) ### Feature Request +- [PR #1348](https://github.com/stympy/faker/pull/1348) Add Faker::Finance.vat_number [@vbrazo](https://github.com/vbrazo) - [PR #1342](https://github.com/stympy/faker/pull/1342) Added Faker::CryptoCoin scope [@jacksonpires](https://github.com/jacksonpires) - [PR #1338](https://github.com/stympy/faker/pull/1338) Add new translations to the en-ZA locale [@bradleymarques](https://github.com/bradleymarques) - [PR #1341](https://github.com/stympy/faker/pull/1341) Add Faker::Construction [@benwyrosdick](https://github.com/benwyrosdick) diff --git a/doc/finance.md b/doc/finance.md index 3fd94575a7..ca0b3ad191 100644 --- a/doc/finance.md +++ b/doc/finance.md @@ -5,4 +5,8 @@ Faker::Finance.credit_card #=> "3018-348979-1853" Faker::Finance.credit_card(:mastercard) #=> "6771-8921-2291-6236" Faker::Finance.credit_card(:mastercard, :visa) #=> "4448-8934-1277-7195" + +# Random vat_number +Faker::Finance.vat_number #=> "BR38.395.329/2471-83" +Faker::Finance.vat_number('DE') #=> "DE593306671" ``` \ No newline at end of file diff --git a/lib/faker/finance.rb b/lib/faker/finance.rb index 55c42a60af..c558816b16 100644 --- a/lib/faker/finance.rb +++ b/lib/faker/finance.rb @@ -18,12 +18,23 @@ def credit_card(*types) multiplier = (multiplier == 2 ? 1 : 2) sum + (digit * multiplier).to_s.split('').map(&:to_i).inject(0) { |digit_sum, cur| digit_sum + cur } end + # the sum plus whatever the last digit is must be a multiple of 10. So, the # last digit must be 10 - the last digit of the sum. luhn_digit = (10 - (luhn_sum % 10)) % 10 template.gsub('L', luhn_digit.to_s) end + + def vat_number(country = 'BR') + numerify(fetch("finance.vat_number.#{country}")) + rescue I18n::MissingTranslationData + raise ArgumentError, "Could not find vat number for #{country}" + end + + def vat_number_keys + translate('faker.finance.vat_number').keys + end end end end diff --git a/lib/locales/en/finance.yml b/lib/locales/en/finance.yml index 7e16059db9..0cf7b7f06c 100644 --- a/lib/locales/en/finance.yml +++ b/lib/locales/en/finance.yml @@ -48,3 +48,63 @@ en: - /6706#########{5,6}L/ - /6771#########{5,6}L/ - /6709#########{5,6}L/ + vat_number: + AT: "ATU########" + AR: "AR###########" + AU: "AU###########" + BR: "BR##.###.###/####-##" + BY: "УНП #########" + BE: "BE0#########" + BG: + - "BG#########" + - "BG##########" + CH: "CH######" + CL: "CL########-#" + CZ: + - "CZ########" + - "CZ#########" + - "CZ##########" + DK: "DK## ## ## ##" + EC: "EC#############" + EE: "EE#########" + FI: "FI########" + DE: "DE#########" + GB: + - "GB### #### ##" + - "GB### #### ## ###" + - "GBGD###" + - "GBHA###" + - "GB888 8### ##" + GR: "EL#########" + HR: "HR### ### ### ##" + "NO": "NO#########" + HU: "HU########" + IT: "IT##########" + LT: + - "LT#########" + - "LT############" + LU: "LU########" + LV: "LV###########" + MT: "MT########" + MX: "MX#### ###### ###" + PH: "PH### ### ### ###" + PL: "PL##########" + PT: "PT#########" + RO: + - "RO##" + - "RO###" + - "RO####" + - "RO#####" + - "RO######" + - "RO#######" + - "RO########" + - "RO#########" + - "RO##########" + RU: + - "RU##########" + - "RU############" + SE: "SE##########01" + SI: "SI########" + SK: "SK##########" + SM: "SM#####" + UA: "UA##########" diff --git a/test/test_faker_finance.rb b/test/test_faker_finance.rb new file mode 100644 index 0000000000..f35756cacc --- /dev/null +++ b/test/test_faker_finance.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require_relative 'test_helper' + +class TestFakerFinance < Test::Unit::TestCase + def setup + Faker::Config.locale = nil + end + + def test_vat_number + assert Faker::Finance.vat_number.match(/\w+/) + end + + def test_vat_number_with_invalid_params + assert_raise ArgumentError do + Faker::Finance.vat_number(Faker::Lorem.word) + end + end + + def test_vat_number_with_valid_params + Faker::Finance.vat_number_keys.each do |country| + assert Faker::Finance.vat_number(country).match(/\w+/) + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 53e3f20382..0f60325634 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -3,7 +3,7 @@ require 'simplecov' SimpleCov.formatter = SimpleCov::Formatter::HTMLFormatter SimpleCov.start do - add_filter ['.bundle', 'test', 'lib/extensions'] + add_filter ['.bundle', 'lib/extensions', 'test'] end require 'test/unit'