Skip to content

Commit

Permalink
Merge branch 'main' into add-pt-br-locales-for-subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
murichristopher authored Jun 7, 2024
2 parents a8b1192 + f11e01a commit c02efb6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
9 changes: 5 additions & 4 deletions doc/default/national_health_service.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Faker::NationalHealthService

```ruby
Faker::NationalHealthService.british_number #=> "403 958 5577"
The NHS sets aside a range of numbers from 999 000 0000 to 999 999 9999 for
test purposes. For more details, see [NHS Digital documentation about
synthetic data](https://digital.nhs.uk/services/e-referral-service/document-library/synthetic-data-in-live-environments#synthetic-data-naming-convention).

# Keyword arguments: number
Faker::NationalHealthService.check_digit(number: 400_012_114) #=> 6
```ruby
Faker::NationalHealthService.british_number #=> "999 464 0232"
```
9 changes: 7 additions & 2 deletions lib/faker/default/national_health_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ class << self
##
# Produces a random British NHS number.
#
# The NHS sets aside a range of numbers from 999 000 0000 to 999 999 9999
# for test purposes.
#
# @return [String]
#
# @example
# Faker::NationalHealthService.british_number #=> "403 958 5577"
# Faker::NationalHealthService.british_number #=> "999 464 0232"
#
# @faker.version 1.9.2
def british_number
base_number = rand(400_000_001...499_999_999)
base_number = rand(999_000_001...999_999_999)
# If the check digit is equivalent to 10, the number is invalid.
# See https://en.wikipedia.org/wiki/NHS_number
base_number -= 1 if check_digit(number: base_number) == 10
Expand All @@ -24,6 +27,8 @@ def british_number
.join
end

private

##
# Produces a random British NHS number's check digit.
#
Expand Down
14 changes: 3 additions & 11 deletions test/faker/default/test_faker_national_health_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,12 @@ def setup
end

def test_nhs_british_number
assert_match(/\A\d{3}\s\d{3}\s\d{4}\z/, @tester.british_number)
assert_match(/\A9{3}\s\d{3}\s\d{4}\z/, @tester.british_number)
end

def test_check_digit_equals_10
Faker::NationalHealthService.stub(:rand, 458_617_434) do
assert_match('458 617 4331', @tester.british_number)
Faker::NationalHealthService.stub(:rand, 999_464_033) do
assert_match('999 464 0321', @tester.british_number)
end
end

def test_check_digit
assert_equal 6, @tester.check_digit(number: 400_012_114)
end

def test_check_digit_11
assert_equal 0, @tester.check_digit(number: 418_513_625)
end
end

0 comments on commit c02efb6

Please sign in to comment.