Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test range param to NHS numbers #2947

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading