Skip to content

Commit

Permalink
Add Faker::Finance.vat_number (faker-ruby#1348)
Browse files Browse the repository at this point in the history
* Add Faker::Finance.vat_number, its tests and docs

* Update changelog

* Add tests and reorder locales

* Minor changes

* Update test_faker_finance.rb

* Minor change
  • Loading branch information
vbrazo authored Aug 29, 2018
1 parent b4c4e21 commit 9d31f01
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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)
Expand Down
4 changes: 4 additions & 0 deletions doc/finance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
11 changes: 11 additions & 0 deletions lib/faker/finance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
60 changes: 60 additions & 0 deletions lib/locales/en/finance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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##########"
25 changes: 25 additions & 0 deletions test/test_faker_finance.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 9d31f01

Please sign in to comment.