Skip to content

Commit 6fa5e28

Browse files
patakkjust6660
andauthored
Follow-up to #2287: Add requested tests (#2358)
* Fix: Ensure deterministic locale selection in multi-locale mode * Add missing tests for PR #2287 don't increase any version part. --------- Co-authored-by: just6660 <just6660@gmail.com>
1 parent ce98d84 commit 6fa5e28

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

faker/proxy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .config import DEFAULT_LOCALE
1212
from .exceptions import UniquenessException
1313
from .factory import Factory
14-
from .generator import Generator, random
14+
from .generator import Generator
1515
from .typing import SeedType
1616
from .utils.distribution import choices_distribution
1717

tests/test_proxy.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,43 @@ def test_dir_include_all_providers_attribute_in_list(self):
437437
attributes = dir(fake)
438438
assert attributes == expected
439439

440+
def test_select_factory_distribution_uses_instance_random(self):
441+
from faker.utils.distribution import choices_distribution
442+
443+
locale = OrderedDict([("de_DE", 3), ("en_US", 2), ("ja_JP", 5)])
444+
fake = Faker(locale)
445+
fake.seed_instance(12345)
446+
447+
instance_random = fake._factories[0].random
448+
with patch("faker.proxy.choices_distribution", wraps=choices_distribution) as mock_dist:
449+
fake.name()
450+
mock_dist.assert_called_once()
451+
args = mock_dist.call_args[0]
452+
assert args[2] is instance_random
453+
454+
def test_seed_instance_deterministic_multi_locale_no_weights(self):
455+
fake = Faker(["en_US", "ja_JP", "de_DE"])
456+
fake.seed_instance(12345)
457+
first_run = [fake.name() for _ in range(20)]
458+
459+
fake2 = Faker(["en_US", "ja_JP", "de_DE"])
460+
fake2.seed_instance(12345)
461+
second_run = [fake2.name() for _ in range(20)]
462+
463+
assert first_run == second_run
464+
465+
def test_seed_instance_deterministic_multi_locale_with_weights(self):
466+
locale = OrderedDict([("de_DE", 3), ("en_US", 2), ("ja_JP", 5)])
467+
fake = Faker(locale)
468+
fake.seed_instance(12345)
469+
first_run = [fake.name() for _ in range(20)]
470+
471+
fake2 = Faker(locale)
472+
fake2.seed_instance(12345)
473+
second_run = [fake2.name() for _ in range(20)]
474+
475+
assert first_run == second_run
476+
440477
def test_copy(self):
441478
fake = Faker("it_IT")
442479
fake2 = copy.deepcopy(fake)

0 commit comments

Comments
 (0)