From 00e88c350968de2e295b356a9ef1baa6969919ca Mon Sep 17 00:00:00 2001 From: Ilya Shakhat Date: Wed, 25 Dec 2013 15:16:45 +0400 Subject: [PATCH] Make initialization of test runtime storage easier Change-Id: I6016ad3c304278017aece80b7bcc97ff0f4300cb --- tests/api/test_api.py | 34 ++++++++++++++++++++-------------- tests/api/test_users.py | 8 ++++---- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/tests/api/test_api.py b/tests/api/test_api.py index 670a3ba15..a925e5efa 100644 --- a/tests/api/test_api.py +++ b/tests/api/test_api.py @@ -31,15 +31,9 @@ class TestAPI(testtools.TestCase): @contextlib.contextmanager -def make_runtime_storage(data, record_type=None, **kwargs): - - if record_type: - count = 0 - for record in generate_records(record_type, **kwargs): - record['record_id'] = count - data['record:%s' % count] = record - count += 1 - data['record:count'] = count +def make_runtime_storage(data, *args): + for arg in args: + data.update(arg) runtime_storage_inst = TestStorage(data) setattr(web.app, 'stackalytics_vault', None) @@ -76,8 +70,8 @@ class TestStorage(runtime_storage.RuntimeStorage): yield record -def generate_commits(**kwargs): - for values in product(**kwargs): +def _generate_commits(**kwargs): + for values in _product(**kwargs): commit = { 'commit_id': str(uuid.uuid4()), 'lines_added': 9, 'module': 'nova', 'record_type': 'commit', @@ -99,13 +93,25 @@ def generate_commits(**kwargs): yield commit -def generate_records(record_types, **kwargs): +def _generate_records(**kwargs): + record_types = kwargs.get('record_type', []) if 'commit' in record_types: - for commit in generate_commits(**kwargs): + for commit in _generate_commits(**kwargs): yield commit -def product(**kwargs): +def make_records(**kwargs): + data = {} + count = 0 + for record in _generate_records(**kwargs): + record['record_id'] = count + data['record:%s' % count] = record + count += 1 + data['record:count'] = count + return data + + +def _product(**kwargs): position_to_key = {} values = [] for key, value in kwargs.iteritems(): diff --git a/tests/api/test_users.py b/tests/api/test_users.py index 002cd37f7..11ab530fe 100644 --- a/tests/api/test_users.py +++ b/tests/api/test_users.py @@ -30,8 +30,8 @@ class TestAPIUsers(test_api.TestAPI): {'repos': [{'module': 'nova', 'project_type': 'openstack', 'organization': 'openstack', 'uri': 'git://github.com/openstack/nova.git'}]}, - ['commit'], - user_id=['john_doe', 'bill_smith']): + test_api.make_records(record_type=['commit'], + user_id=['john_doe', 'bill_smith'])): response = self.app.get('/api/1.0/users') users = json.loads(response.data)['users'] self.assertEqual(2, len(users)) @@ -43,8 +43,8 @@ class TestAPIUsers(test_api.TestAPI): {'repos': [{'module': 'nova', 'project_type': 'openstack', 'organization': 'openstack', 'uri': 'git://github.com/openstack/nova.git'}]}, - ['commit'], - user_name=['John Doe', 'Bill Smith']): + test_api.make_records(record_type=['commit'], + user_name=['John Doe', 'Bill Smith'])): response = self.app.get('/api/1.0/users?query=doe') users = json.loads(response.data)['users'] self.assertEqual(1, len(users))