Permalink
Browse files

seed a Random instance instead of the module

Seeding the random module will seed that module globally --
this affects calls to random outside the faker package.
Seeding an instance of the Random class will not affect
other calls to the random module.
This addresses one concern in issue #14.
  • Loading branch information...
1 parent b9af820 commit acd0de23563b69a95611406dddf9c09e04d579b3 @amygdalama amygdalama committed Jun 17, 2015
View
@@ -11,6 +11,7 @@ python:
- "pypy3"
install:
- pip install -e .
+ - pip install -r faker/tests/requirements.txt
- pip install coveralls
script:
- coverage run --source=faker setup.py test
View
@@ -280,6 +280,11 @@ same results.
Tests
-----
+Installing dependencies:
+
+.. code:: bash
+
+ $ pip install -r faker/tests/requirements.txt
Run tests:
View
@@ -12,6 +12,7 @@ install:
- ps: (new-object net.webclient).DownloadFile('https://raw.github.com/pypa/pip/master/contrib/get-pip.py', 'C:/get-pip.py')
- "%PYTHON%/python.exe C:/get-pip.py"
- "%PYTHON%/Scripts/pip.exe install -e ."
+ - "%PYTHON%/Scripts/pip.exe install -r faker/tests/requirements.txt"
test_script:
- "%PYTHON%/Scripts/pip.exe --version"
- "%PYTHON%/python.exe setup.py test"
View
@@ -0,0 +1,3 @@
+dependencies:
+ pre:
+ - pip install -r faker/tests/requirements.txt
View
@@ -7,6 +7,7 @@
_re_token = re.compile(r'\{\{(\s?)(\w+)(\s?)\}\}')
+random = random.Random()
class Generator(object):
@@ -1,8 +1,9 @@
# coding=utf-8
import re
-import random
import string
+
+from faker.generator import random
from faker.utils.distribution import choice_distribution
@@ -1,10 +1,10 @@
# coding=utf-8
from __future__ import unicode_literals
from decimal import Decimal
-import random
from .. import BaseProvider
from .. import date_time
+from faker.generator import random
localized = True
@@ -1,8 +1,9 @@
# coding=utf-8
from __future__ import unicode_literals
from decimal import Decimal
-import random
+
from .. import Provider as AddressProvider
+from faker.generator import random
def contains_point(poly, point):
@@ -1,7 +1,7 @@
from __future__ import unicode_literals
-import random
from ..en import Provider as AddressProvider
+from faker.generator import random
class Provider(AddressProvider):
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
-import random
+
from .. import Provider as AddressProvider
+from faker.generator import random
class Provider(AddressProvider):
@@ -2,12 +2,12 @@
from __future__ import unicode_literals
-import random
import re
from time import time, mktime
from datetime import timedelta
+from faker.generator import random
from faker.utils.datetime_safe import date, datetime, real_date, real_datetime
from faker.utils import is_string
@@ -2,10 +2,10 @@
from __future__ import unicode_literals
from .. import BaseProvider
-import random
import re
import unicodedata
+from faker.generator import random
from faker.providers.lorem.la import Provider as Lorem
from faker.utils.decorators import slugify, slugify_domain
@@ -2,10 +2,10 @@
from __future__ import unicode_literals
import hashlib
-import random
import string
import uuid
+from faker.generator import random
from faker.providers.date_time import Provider as DatetimeProvider
from .. import BaseProvider
@@ -1,7 +1,7 @@
# coding=utf-8
from __future__ import unicode_literals
from .. import Provider as SsnProvider
-import random
+from faker.generator import random
class Provider(SsnProvider):
@@ -2,8 +2,8 @@
from __future__ import unicode_literals
from .. import Provider as SsnProvider
+from faker.generator import random
import datetime
-import random
class Provider(SsnProvider):
@@ -2,7 +2,7 @@
from __future__ import unicode_literals
from .. import Provider as SsnProvider
-import random
+from faker.generator import random
class Provider(SsnProvider):
@@ -2,8 +2,8 @@
from __future__ import unicode_literals
from .. import Provider as SsnProvider
+from faker.generator import random
import datetime
-import random
class Provider(SsnProvider):
@@ -3,8 +3,8 @@
from __future__ import unicode_literals
from datetime import datetime
-import random
+from faker.generator import random
from faker.providers.date_time import Provider as DatetimeProvider
from .. import BaseProvider
@@ -7,17 +7,22 @@
import datetime
import json
import os
-import random
import time
import unittest
import sys
try:
+ from mock import patch
+except ImportError:
+ from unittest.mock import patch
+
+try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from faker import Generator, Factory
+from faker.generator import random
from faker.utils import text, decorators
try:
@@ -366,5 +371,16 @@ def test_no_words_paragraph(self):
self.assertEqual(sentence, '')
+class GeneratorTestCase(unittest.TestCase):
+
+ def setUp(self):
+ self.generator = Generator()
+
+ @patch('random.seed')
+ def test_random_seed_doesnt_seed_system_random(self, mock_system_random):
+ self.generator.seed(0)
+ self.assertFalse(mock_system_random.called)
+
+
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1 @@
+mock==1.0.1
@@ -1,8 +1,7 @@
# coding=utf-8
import bisect
-import random
-
+from faker.generator import random
def random_sample():
return random.uniform(0.0, 1.0)

0 comments on commit acd0de2

Please sign in to comment.