Skip to content

Instantly share code, notes, and snippets.

@omaciel
Last active August 29, 2015 13:56
Show Gist options
  • Save omaciel/8945843 to your computer and use it in GitHub Desktop.
Save omaciel/8945843 to your computer and use it in GitHub Desktop.
Using SingleDispatcher to overload a random data generator.
# -*- coding: utf-8 -*-
import sys
from robottelo.common.helpers import generate_string
try:
from singledispatch import singledispatch
except ImportError:
print "Please install the singledispatcher module"
sys.exit(-1)
@singledispatch
def foo(arg):
"""
Generates random data for tests
"""
raise NotImplementedError("Foo")
@foo.register(str)
def _s(arg):
"""
Given a string return a dictionary containing random
data of a certain data type and length.
@type arg: str
@param arg: String containing a key name, data type
and length. Multiple entries can be used,
separated by commas.
@rtype: dict
@return: Returns a dictionary containing random data
matching the data type and length specified.
>>> print foo('name alpha 2')
{'name': 'vN'}
>>> print foo('name alpha 2, age numeric 2')
{'age': '90', 'name': 'GQ'}
"""
result = {}
for v in arg.split(","):
key, data_type, size = filter(None, v.split(" "))
result[key] = generate_string(data_type, int(size))
return result
@foo.register(list)
def _l(key_names, data_type='alpha', size=5):
"""
Given a list of key names, returns a dictionary
containing random data with the specified data
type and length.
@type key_names: list
@param key_names: List of key names.
@type data_type: str
@param data_type: One of the valid data types supported
by ``generate_string``.
@type size: int
@param size: Length for random data.
@rtype: dict
@return: Returns a dictionary containing random data
matching the data type and length specified.
>>> print foo(['name'])
{'name': 'HBebi'}
>>> print foo(['name'], 'latin1')
{'name': u'\xe8\xcd\xd8\xe6\xdb'}
>>> print foo(['name', 'age'], 'alpha', 6)
{'age': 'bKKYtJ', 'name': 'WbdWZd'}
"""
result = {}
for key in key_names:
result[key] = generate_string(data_type, size)
return result
@foo.register(tuple)
def _t(key_names, data_type='alpha', size=5):
"""
Given a list of key names, returns a dictionary
containing random data with the specified data
type and length.
@type key_names: tuple
@param key_names: Tuple with a list of key names.
@type data_type: str
@param data_type: One of the valid data types supported
by ``generate_string``.
@type size: int
@param size: Length for random data.
@rtype: dict
@return: Returns a dictionary containing random data
matching the data type and length specified.
>>> print foo(('make',))
{'make': 'zWlsz'}
>>> print foo(('name', 'age'),)
{'age': 'jZJmI', 'name': 'lxicW'}
>>> print foo(('name', 'age'), 'numeric', 2)
{'age': '50', 'name': '80'}
"""
result = {}
for key in key_names:
result[key] = generate_string(data_type, size)
return result
if __name__ == "__main__":
import doctest
doctest.testmod()
data1 = (
        (('name', 'label', 'description'), 'alpha'),
        (('name', 'label', 'description',), 'numeric'),
        (('name', 'label', 'description',), 'alphanumeric'),
        (('name', 'label', 'description',), 'utf8', 3),
        (('name', 'label', 'description',), 'latin1', 5),
        (('name', 'label', 'description',), 'html'),
        )

data2 = [
         'name alpha 3, label alpha 3, description alpha 15',
         'name alphanumeric 3, label alphanumeric 3, description alphanumeric 15',
         'name latin1 4, label alpha 3, description alphanumeric 15',
         'name utf8 3, label alpha 3, description utf8 15',
         ]

data3 = [
         [['first-name']],
         [['first-name', 'last-name'], 'alphanumeric'],
         [['first-name', 'last-name'], 'latin1', 2]
         ]

print "Data 1"
for data in data1:
    print foo(*data)
print "\nData 2"
for data in data2:
    print foo(data)
print "\nData 3"
for data in data3:
    print foo(*data)

Data 1
{'description': 'NLyNt', 'name': 'JLBQn', 'label': 'LJSRT'}
{'description': '62092', 'name': '09339', 'label': '62568'}
{'description': 'WUq04', 'name': 'ee9Sc', 'label': 'XFdqC'}
{'description': u'\u53f2\u7253\u9ced', 'name': u'\u8f8b\u68dc\u55c4', 'label': u'\u85cd\u7415\u759a'}
{'description': u'\xe1\xc6\xef\xcf\xf1', 'name': u'\xcf\xd1\xc2\xd3\xdf', 'label': u'\xdb\xc4\xdc\xdd\xd3'}
{'description': '<label>FTxKx</label>', 'name': '<s>KLgUw</s>', 'label': '<h3>eBlxu</h3>'}

Data 2
{'description': 'bMRKtVRumwXOtPC', 'name': 'EOD', 'label': 'Khf'}
{'description': 'hAUYerOiY9xnJ3K', 'name': 'pXg', 'label': 'xL6'}
{'description': 'vmHwQupMtFTb7xq', 'name': u'\xcc\xdd\xd4\xdb', 'label': 'HlB'}
{'description': u'\u9baa\u8c25\u767f\u4e90\u8eb3\u5def\u8cbb\u95a0\u8506\u89fd\u94c7\u6712\u4f7a\u874c\u7acf', 'name': u'\u5e2a\u65df\u98e7', 'label': 'Rgv'}

Data 3
{'first-name': 'tTaXV'}
{'first-name': 'X3O8c', 'last-name': 'L0oys'}
{'first-name': u'\xe4\xde', 'last-name': u'\xeb\xe1'}
@elyezer
Copy link

elyezer commented Feb 12, 2014

To vote, I like the 1 and 3. Maybe having both one for rapid data (example 1) and other for more options (example 3).

@omaciel
Copy link
Author

omaciel commented Feb 13, 2014

@elyezer, made some considerable changes. Input/feedback welcome :)

@sthirugn
Copy link

I like all three :)

btw this can be of great help for our test cases!

@omaciel
Copy link
Author

omaciel commented Feb 15, 2014

@sthirugn you can use all 3 with this latest implementatio. The idea is to use it with Robottelo :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment