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 random.seed call before every random usage #81

Merged
merged 2 commits into from
Jan 12, 2016
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
61 changes: 42 additions & 19 deletions fauxfactory/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
# -*- coding: utf-8 -*-
"""Generate random data for your tests."""

import datetime
import random
import re
import string
import sys
import unicodedata
import uuid
import warnings

from collections import Iterable
from functools import wraps

from fauxfactory.constants import (
HTML_TAGS, LOREM_IPSUM_TEXT,
MAX_YEARS, MIN_YEARS,
SCHEMES, SUBDOMAINS, TLDS, VALID_NETMASKS
)

__all__ = (
'gen_alpha',
'gen_alphanumeric',
Expand Down Expand Up @@ -28,24 +46,6 @@
'gen_uuid',
)

import datetime
import random
import re
import string
import sys
import unicodedata
import uuid
import warnings

from collections import Iterable
from fauxfactory.constants import (
HTML_TAGS, LOREM_IPSUM_TEXT,
MAX_YEARS, MIN_YEARS,
SCHEMES, SUBDOMAINS, TLDS, VALID_NETMASKS
)
from functools import wraps


# Private Functions -----------------------------------------------------------


Expand Down Expand Up @@ -84,7 +84,7 @@ def _unicode_letters_generator():
range_function = xrange # pylint:disable=undefined-variable
else:
chr_function = chr
range_function = range
range_function = range # pylint:disable=redefined-variable-type

# Use sys.maxunicode instead of 0x10FFFF to avoid the exception below, in a
# narrow Python build (before Python 3.3)
Expand Down Expand Up @@ -156,6 +156,7 @@ def gen_alpha(length=10):
# Validate length argument
_is_positive_int(length)

random.seed()
output_string = u''.join(
random.choice(string.ascii_letters) for i in range(length)
)
Expand All @@ -175,6 +176,7 @@ def gen_alphanumeric(length=10):
# Validate length argument
_is_positive_int(length)

random.seed()
output_string = u''.join(
random.choice(
string.ascii_letters + string.digits
Expand Down Expand Up @@ -218,6 +220,7 @@ def gen_choice(choices):
if len(choices) == 1:
return choices[0]

random.seed()
return random.choice(choices)


Expand All @@ -234,6 +237,8 @@ def gen_cjk(length=10):
# Validate length argument
_is_positive_int(length)

random.seed()

# Generate codepoints, then convert the codepoints to a string. The
# valid range of CJK codepoints is 0x4E00 - 0x9FCC, inclusive. Python 2
# and 3 support the `unichr` and `chr` functions, respectively.
Expand All @@ -257,6 +262,8 @@ def gen_cyrillic(length=10):
# Validate length argument
_is_positive_int(length)

random.seed()

# Generate codepoints, then convert the codepoints to a string. The
# valid range of Cyrillic codepoints is 0x410 - 0x4ff, inclusive. Python 2
# and 3 support the `unichr` and `chr` functions, respectively.
Expand Down Expand Up @@ -299,6 +306,8 @@ def gen_date(min_date=None, max_date=None):
# Check that max_date is not before min_date
assert min_date < max_date

random.seed()

# Pick a day between min and max dates
diff = max_date - min_date
days = random.randint(0, diff.days)
Expand Down Expand Up @@ -337,6 +346,8 @@ def gen_datetime(min_date=None, max_date=None):
# Check that max_date is not before min_date
assert min_date < max_date

random.seed()

# Pick a time between min and max dates
diff = max_date - min_date
seconds = random.randint(0, diff.days * 3600 * 24 + diff.seconds)
Expand Down Expand Up @@ -402,6 +413,7 @@ def gen_integer(min_value=None, max_value=None):
if not isinstance(max_value, integer_types) or max_value > _max_value:
raise ValueError("\'%s\' is not a valid maximum." % max_value)

random.seed()
value = random.randint(min_value, max_value)

return value
Expand Down Expand Up @@ -494,6 +506,8 @@ def gen_latin1(length=10):
for i in range(int(range2[0], 16), int(range2[1], 16)):
output_array.append(i)

random.seed()

if sys.version_info[0] == 2:
output_string = u''.join(
# pylint:disable=E0602
Expand Down Expand Up @@ -559,6 +573,9 @@ def gen_ipaddr(ip3=False, ipv6=False, prefix=()):
raise ValueError(
"Prefix {} is too long for this configuration".format(
repr(prefix)))

random.seed()

if ipv6:
# StackOverflow.com questions: generate-random-ipv6-address
random_fields = [
Expand Down Expand Up @@ -595,6 +612,7 @@ def gen_mac(delimiter=':', multicast=None, locally=None):

if delimiter not in [':', '-']:
raise ValueError('Delimiter is not a valid option: %s' % delimiter)
random.seed()
if multicast is None:
multicast = bool(random.randint(0, 1))
if locally is None:
Expand Down Expand Up @@ -646,6 +664,7 @@ def gen_netmask(min_cidr=1, max_cidr=31):
'max_cidr must be less than {0}, but is {1}'
.format(len(VALID_NETMASKS), max_cidr)
)
random.seed()
return VALID_NETMASKS[random.randint(min_cidr, max_cidr)]


Expand All @@ -661,6 +680,7 @@ def gen_numeric_string(length=10):
# Validate length argument
_is_positive_int(length)

random.seed()
output_string = u''.join(
random.choice(string.digits) for i in range(length)
)
Expand Down Expand Up @@ -688,6 +708,7 @@ def gen_time():

"""

random.seed()
return datetime.time(
random.randint(0, 23),
random.randint(0, 59),
Expand Down Expand Up @@ -752,6 +773,7 @@ def gen_utf8(length=10):
# Validate length argument
_is_positive_int(length)

random.seed()
return u''.join([random.choice(UNICODE_LETTERS) for _ in range(length)])


Expand Down Expand Up @@ -780,6 +802,7 @@ def gen_html(length=10):
# Validate length argument
_is_positive_int(length)

random.seed()
html_tag = random.choice(HTML_TAGS)
output_string = u'<{0}>{1}</{2}>'.format(
html_tag, gen_string("alpha", length), html_tag)
Expand Down
9 changes: 6 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ help:
@echo " docs-clean Remove documentation."
@echo " docs-doctest Check code samples in the documentation."
@echo " docs-html Compile documentation to HTML."
@echo " flake Run flake8."
@echo " lint Run flake8 and pylint."
@echo " test Run unit tests."
@echo " test-all Run unit tests and doctests, measure coverage."
Expand All @@ -20,8 +21,10 @@ docs-doctest:
docs-html:
cd docs && $(MAKE) html

lint:
flake:
flake8 .

lint: flake
pylint --reports=n --disable=I --ignore-imports=y fauxfactory docs/conf.py setup.py
# pylint should also lint the tests/ directory.

Expand All @@ -39,7 +42,7 @@ publish:
test:
$(UNITTEST_CMD) $(UNITTEST_ARGS)

test-all: lint docs-doctest
test-all: flake docs-doctest
coverage run -m $(UNITTEST_MOD) $(UNITTEST_ARGS)

.PHONY: help docs-clean docs-doctest docs-html lint package package-clean publish test test-all
.PHONY: help docs-clean docs-doctest docs-html flake lint package package-clean publish test test-all
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""A setuptools-based script for installing FauxFactory."""
# setuptools is preferred over distutils.
from setuptools import find_packages, setup
import codecs
import os

from setuptools import find_packages, setup


def read(*paths):
"""Build a file path from *paths* and return the contents."""
Expand Down