Closed pavelzag closed 7 years ago
Thanks @pavelzag for this new method! Could you write some tests as well before we merge it?
@omaciel Sure, will do
@pavelzag here is how I'd write the test (plus some PEP8-related fixes) I'd recommend:
diff --git a/fauxfactory/factories/strings.py b/fauxfactory/factories/strings.py
index 4dbf20d..b015b8c 100644
--- a/fauxfactory/factories/strings.py
+++ b/fauxfactory/factories/strings.py
@@ -43,6 +43,7 @@ def gen_string(str_type, length=None, validator=None, default=None, tries=10):
* latin1
* numeric
* utf8
+ * punctuation
"""
str_types_functions = {
@@ -54,6 +55,7 @@ def gen_string(str_type, length=None, validator=None, default=None, tries=10):
'latin1': gen_latin1,
'numeric': gen_numeric_string,
'utf8': gen_utf8,
+ 'punctuation': gen_special,
}
str_type_lower = str_type.lower() # do not modify user data
if str_type_lower not in str_types_functions.keys():
@@ -299,17 +301,15 @@ def gen_utf8(length=10):
random.seed()
return ''.join([random.choice(UNICODE_LETTERS) for _ in range(length)])
+
@check_len
@check_validation
def gen_special(length=10):
- """Returns a random special characters string.
+ """Return a random special characters string.
:param int length: Length for random data.
:returns: A random string made up of special characters.
:rtype: str
-
"""
-
-
random.seed()
- return u''.join(random.choice(string.punctuation) for _ in range(length))
+ return ''.join(random.choice(string.punctuation) for _ in range(length))
diff --git a/tests/test_strings.py b/tests/test_strings.py
index 4983f22..0d4d6f5 100644
--- a/tests/test_strings.py
+++ b/tests/test_strings.py
@@ -1,5 +1,5 @@
"""Tests for all string generators."""
-
+import string
import unicodedata
import pytest
@@ -14,6 +14,7 @@ from fauxfactory import (
gen_numeric_string,
gen_string,
gen_utf8,
+ gen_special,
)
from fauxfactory.helpers import unicode_letters_generator
@@ -27,6 +28,7 @@ GENERATORS = [
gen_latin1,
gen_numeric_string,
gen_utf8,
+ gen_special,
]
STRING_TYPES = [
@@ -38,6 +40,7 @@ STRING_TYPES = [
'latin1',
'numeric',
'utf8',
+ 'punctuation',
]
@@ -133,3 +136,11 @@ def test_invalid_string_type():
"""Only valid string types can be generated."""
with pytest.raises(ValueError):
gen_string('foo')
+
+
+def test_special_string():
+ """Assert that only punctuation strings are returned."""
+ VALID_CHARS = string.punctuation
+ special_str = gen_special()
+ for char in special_str:
+ assert char in VALID_CHARS
I can add a commit with these changes to your PR and merge it. Would the be ok?
@omaciel Of course, would be perfectly fine! Thanks
Thanks a bunch @pavelzag
Added a function to generate special characters string