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

Fix global clear of unique values for Faker::UniqueGenerator #1355

Merged
merged 3 commits into from
Sep 18, 2018
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## HEAD Unreleased

### Bug/Fixes
- [PR #1368](https://github.com/stympy/faker/pull/1368) Don't force enforce_available_locales [@deivid-rodriguez](https://github.com/deivid-rodriguez)
- [PR #1355](https://github.com/stympy/faker/pull/1355) Fix global clear of unique values for Faker::UniqueGenerator [@kolasss](https://github.com/kolasss)
- [PR #1335](https://github.com/stympy/faker/pull/1335) Fix Company.luhn_algorithm and add missing tests [@01max](https://github.com/01max)
- [PR #1334](https://github.com/stympy/faker/pull/1334) Faker::Number.leading_zero_number should always start with 0 [@vbrazo](https://github.com/vbrazo)
- [PR #1317](https://github.com/stympy/faker/pull/1317) Change Faker::Lorem.multibyte logic [@ShabelnikM](https://github.com/ShabelnikM)
Expand All @@ -27,6 +29,7 @@
- [PR #1329](https://github.com/stympy/faker/pull/1329) Update docs on behavior of price [@softwaregravy](https://github.com/softwaregravy)

### Feature Request
- [PR #1156](https://github.com/stympy/faker/pull/1156) Add Faker::Json [@the-wendell](https://github.com/the-wendell)
- [PR #1359](https://github.com/stympy/faker/pull/1359) Add Faker::Tezos [@Pierre-Michard](https://github.com/Pierre-Michard)
- [PR #1366](https://github.com/stympy/faker/pull/1366) Add Faker::Seinfeld.business [@dsgraham](https://github.com/dsgraham)
- [PR #1358](https://github.com/stympy/faker/pull/1358) Add cat breed for Japanese [@yizknn](https://github.com/yizknn)
Expand Down
3 changes: 2 additions & 1 deletion lib/helpers/unique_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ class << self

def initialize(generator, max_retries)
@generator = generator
self.class.marked_unique.add(self)
@max_retries = max_retries
@previous_results = Hash.new { |hash, key| hash[key] = Set.new }
end

# rubocop:disable Style/MethodMissingSuper
def method_missing(name, *arguments)
self.class.marked_unique.add(self)

@max_retries.times do
result = @generator.public_send(name, *arguments)

Expand Down
48 changes: 47 additions & 1 deletion test/test_faker_unique_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def stubbed_generator.test
generator.test
end

Faker::UniqueGenerator.clear
generator.clear

assert_equal(1, generator.test)

Expand All @@ -75,4 +75,50 @@ def stubbed_generator.test

assert_equal(1, generator.test)
end

def test_clears_unique_values_for_all_generators
stubbed_generator = Object.new
def stubbed_generator.test
1
end

stubbed_generator2 = Object.new
def stubbed_generator2.test
2
end

generator1 = Faker::UniqueGenerator.new(stubbed_generator, 3)
generator2 = Faker::UniqueGenerator.new(stubbed_generator2, 3)

assert_equal(1, generator1.test)
assert_equal(2, generator2.test)

assert_raises Faker::UniqueGenerator::RetryLimitExceeded do
generator1.test
end
assert_raises Faker::UniqueGenerator::RetryLimitExceeded do
generator2.test
end

Faker::UniqueGenerator.clear

assert_nothing_raised Faker::UniqueGenerator::RetryLimitExceeded do
assert_equal(1, generator1.test)
assert_equal(2, generator2.test)
end

assert_raises Faker::UniqueGenerator::RetryLimitExceeded do
generator1.test
end
assert_raises Faker::UniqueGenerator::RetryLimitExceeded do
generator2.test
end

Faker::UniqueGenerator.clear

assert_nothing_raised Faker::UniqueGenerator::RetryLimitExceeded do
assert_equal(1, generator1.test)
assert_equal(2, generator2.test)
end
end
end