From 255bc3b882ec3477fdeab4692e75a808b209e37c Mon Sep 17 00:00:00 2001 From: Ilya Shakhat Date: Wed, 25 Dec 2013 18:39:44 +0400 Subject: [PATCH] Added test for aggregated module stats Change-Id: Id9bca5a1acebd69d166ed30539c77b21df7df55f --- tests/api/test_api.py | 63 +++++++++++++++++++++++++++++------------ tests/api/test_stats.py | 43 ++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 18 deletions(-) create mode 100644 tests/api/test_stats.py diff --git a/tests/api/test_api.py b/tests/api/test_api.py index a925e5efa..14af00588 100644 --- a/tests/api/test_api.py +++ b/tests/api/test_api.py @@ -31,9 +31,8 @@ class TestAPI(testtools.TestCase): @contextlib.contextmanager -def make_runtime_storage(data, *args): - for arg in args: - data.update(arg) +def make_runtime_storage(data, *generators): + _add_generated_records(data, *generators) runtime_storage_inst = TestStorage(data) setattr(web.app, 'stackalytics_vault', None) @@ -47,6 +46,19 @@ def make_runtime_storage(data, *args): pass +def make_records(**kwargs): + def generate_records(): + record_types = kwargs.get('record_type', []) + if 'commit' in record_types: + for commit in _generate_commits(algebraic_product(**kwargs)): + yield commit + elif 'mark' in record_types: + for mark in _generate_marks(algebraic_product(**kwargs)): + yield mark + + return generate_records + + class TestStorage(runtime_storage.RuntimeStorage): def __init__(self, data): @@ -70,8 +82,8 @@ class TestStorage(runtime_storage.RuntimeStorage): yield record -def _generate_commits(**kwargs): - for values in _product(**kwargs): +def _generate_commits(values_list): + for values in values_list: commit = { 'commit_id': str(uuid.uuid4()), 'lines_added': 9, 'module': 'nova', 'record_type': 'commit', @@ -93,25 +105,40 @@ def _generate_commits(**kwargs): yield commit -def _generate_records(**kwargs): - record_types = kwargs.get('record_type', []) - if 'commit' in record_types: - for commit in _generate_commits(**kwargs): - yield commit +def _generate_marks(values_list): + for values in values_list: + mark = { + 'record_type': 'mark', + 'id': str(uuid.uuid4()), + 'module': 'nova', + 'message': str(uuid.uuid4()), + 'subject': 'Fixed affiliation of Edgar and Sumit', + 'user_id': 'john_doe', + 'primary_key': str(uuid.uuid4()), + 'author_email': 'john_doe@ibm.com', 'company_name': 'IBM', + 'week': 2275, + 'blueprint_id': None, 'bug_id': '1212953', + 'author_name': 'John Doe', + 'date': 1376737923, 'launchpad_id': 'john_doe', + 'branches': set(['master']), + 'change_id': 'I33f0f37b6460dc494abf2520dc109c9893ace9e6', + 'release': 'icehouse' + } + mark.update(values) + yield mark -def make_records(**kwargs): - data = {} +def _add_generated_records(data, *generators): count = 0 - for record in _generate_records(**kwargs): - record['record_id'] = count - data['record:%s' % count] = record - count += 1 + for gen in generators: + for record in gen(): + record['record_id'] = count + data['record:%s' % count] = record + count += 1 data['record:count'] = count - return data -def _product(**kwargs): +def algebraic_product(**kwargs): position_to_key = {} values = [] for key, value in kwargs.iteritems(): diff --git a/tests/api/test_stats.py b/tests/api/test_stats.py new file mode 100644 index 000000000..d79b5488e --- /dev/null +++ b/tests/api/test_stats.py @@ -0,0 +1,43 @@ +# Copyright (c) 2013 Mirantis Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json + +from tests.api import test_api + + +class TestAPIStats(test_api.TestAPI): + + def test_get_modules(self): + with test_api.make_runtime_storage( + {'repos': [{'module': 'nova', 'project_type': 'openstack', + 'organization': 'openstack', + 'uri': 'git://github.com/openstack/nova.git'}, + {'module': 'glance', 'project_type': 'openstack', + 'organization': 'openstack', + 'uri': 'git://github.com/openstack/glance.git'}]}, + test_api.make_records(record_type=['commit'], + loc=[10, 20, 30], + module=['nova', 'glance']), + test_api.make_records(record_type=['commit'], + loc=[100, 200, 300], + module=['glance'])): + response = self.app.get('/api/1.0/stats/modules?metric=loc') + stats = json.loads(response.data)['stats'] + self.assertEqual(2, len(stats)) + self.assertEqual(660, stats[0]['metric']) + self.assertEqual('glance', stats[0]['id']) + self.assertEqual(60, stats[1]['metric']) + self.assertEqual('nova', stats[1]['id'])