Refactor file_data to use either list or dict in JSON data

This commit is contained in:
Carles Barrobés
2013-02-25 23:27:18 +00:00
parent ea48343843
commit a5149f3b9c
5 changed files with 74 additions and 43 deletions

41
ddt.py
View File

@@ -76,24 +76,31 @@ def ddt(cls):
return func(self, *args, **kwargs)
return wrapper
for name, f in list(cls.__dict__.items()):
if hasattr(f, DATA_ATTR):
for v in getattr(f, DATA_ATTR):
def process_file_data(name, func, file_attr):
"""
Process the parameter in the `file_data` decorator.
"""
cls_path = os.path.abspath(inspect.getsourcefile(cls))
data_file_path = os.path.join(os.path.dirname(cls_path), file_attr)
if os.path.exists(data_file_path):
data = json.loads(open(data_file_path).read())
for elem in data:
if isinstance(data, dict):
key, value = elem, data[elem]
test_name = "{0}_{1}".format(name, key)
elif isinstance(data, list):
value = elem
test_name = "{0}_{1}".format(name, value)
setattr(cls, test_name, feed_data(func, value))
for name, func in list(cls.__dict__.items()):
if hasattr(func, DATA_ATTR):
for v in getattr(func, DATA_ATTR):
test_name = getattr(v, "__name__", "{0}_{1}".format(name, v))
setattr(cls, test_name, feed_data(f, v))
setattr(cls, test_name, feed_data(func, v))
delattr(cls, name)
elif hasattr(f, FILE_ATTR):
file_attr = getattr(f, FILE_ATTR)
cls_path = os.path.abspath(inspect.getsourcefile(cls))
data_file_path = os.path.join(os.path.dirname(cls_path), file_attr)
if os.path.exists(data_file_path):
data = json.loads(open(data_file_path).read())
for key, value in data.items():
test_name = getattr(
value,
"__name__",
"{0}_{1}".format(key, value)
)
setattr(cls, test_name, feed_data(f, value))
elif hasattr(func, FILE_ATTR):
file_attr = getattr(func, FILE_ATTR)
process_file_data(name, func, file_attr)
delattr(cls, name)
return cls

View File

@@ -1,4 +0,0 @@
{
"test_one": [ 10, 12, 15 ],
"test_two": [ 15, 12, 50 ]
}

4
test/test_data_dict.json Normal file
View File

@@ -0,0 +1,4 @@
{
"unsorted_list": [ 10, 12, 15 ],
"sorted_list": [ 15, 12, 50 ]
}

View File

@@ -16,6 +16,9 @@ def annotated(a, b):
@ddt
class FooTestCase(unittest.TestCase):
def test_undecorated(self):
self.assertTrue(larger_than_two(24))
@data(3, 4, 12, 23)
def test_larger_than_two(self, value):
self.assertTrue(larger_than_two(value))
@@ -29,6 +32,10 @@ class FooTestCase(unittest.TestCase):
a, b = value
self.assertGreater(a, b)
@file_data('test_data.json')
def test_file_data(self, value):
@file_data('test_data_dict.json')
def test_file_data_dict(self, value):
self.assertTrue(has_three_elements(value))
@file_data('test_data_list.json')
def test_file_data_list(self, value):
self.assertTrue(has_three_elements(value))

View File

@@ -6,7 +6,9 @@ from nose.tools import assert_equal, assert_is_not_none
@ddt
class Dummy(object):
"""Dummy class to test decorators on"""
"""
Dummy class to test the data decorator on
"""
@data(1, 2, 3, 4)
def test_something(self, value):
@@ -15,16 +17,19 @@ class Dummy(object):
@ddt
class FileDataDummy(object):
"""Dummy class to test decorators on
specifically the file_data decorator"""
"""
Dummy class to test the file_data decorator on
"""
@file_data("test_data.json")
@file_data("test_data_dict.json")
def test_something_again(self, value):
return value
def test_data_decorator():
"""Test the ``data`` method decorator"""
"""
Test the ``data`` method decorator
"""
def hello():
pass
@@ -42,15 +47,17 @@ def test_data_decorator():
assert_equal(getattr(data_hello, extra_attr), (1, 2))
def test_file_data_decorator():
""" Test the ``file_data`` method decorator"""
def test_file_data_decorator_with_dict():
"""
Test the ``file_data`` method decorator
"""
def hello():
pass
pre_size = len(hello.__dict__)
keys = set(hello.__dict__.keys())
data_hello = data("test_data.json")(hello)
data_hello = data("test_data_dict.json")(hello)
dh_keys = set(data_hello.__dict__.keys())
post_size = len(data_hello.__dict__)
@@ -59,45 +66,53 @@ def test_file_data_decorator():
extra_attrs = dh_keys - keys
assert_equal(len(extra_attrs), 1)
extra_attr = extra_attrs.pop()
assert_equal(getattr(data_hello, extra_attr), ("test_data.json",))
assert_equal(getattr(data_hello, extra_attr), ("test_data_dict.json",))
is_test = lambda x: x.startswith('test_')
def test_ddt():
"""Test the ``ddt`` class decorator"""
"""
Test the ``ddt`` class decorator
"""
tests = len(list(filter(is_test, Dummy.__dict__)))
assert_equal(tests, 4)
def test_file_data_test_creation():
"""Test that the ``file_data`` decorator creates
two tests"""
"""
Test that the ``file_data`` decorator creates two tests
"""
tests = len(list(filter(is_test, FileDataDummy.__dict__)))
assert_equal(tests, 2)
def test_file_data_test_names():
"""Test that ``file_data`` creates tests with the
correct name as specified in the JSON"""
def test_file_data_test_names_dict():
"""
Test that ``file_data`` creates tests with the correct name
Name is the the function name plus the key in the JSON data,
when it is parsed as a dictionary.
"""
tests = set(filter(is_test, FileDataDummy.__dict__))
tests_dir = os.path.dirname(__file__)
test_data_path = os.path.join(tests_dir, 'test_data.json')
test_data_path = os.path.join(tests_dir, 'test_data_dict.json')
test_data = json.loads(open(test_data_path).read())
created_tests = set([
"{0}_{1}".format(name, value) for name, value in test_data.items()
"test_something_again_{0}".format(name) for name in test_data.keys()
])
assert_equal(tests, created_tests)
def test_feed_data_data():
"""Test that data is fed to the decorated tests"""
"""
Test that data is fed to the decorated tests
"""
tests = filter(is_test, Dummy.__dict__)
values = []
@@ -110,7 +125,9 @@ def test_feed_data_data():
def test_feed_data_file_data():
"""Test that data is fed to the decorated tests"""
"""
Test that data is fed to the decorated tests from a file
"""
tests = filter(is_test, FileDataDummy.__dict__)
values = []