Added tests for api user endpoint

* Added record generator and test storage
* Updated RuntimeStorage base class API

Change-Id: I96ec43ea686350aec45726d8b6433950d16ba411
This commit is contained in:
Ilya Shakhat
2013-12-25 13:41:03 +04:00
parent 8eea5e3d86
commit 63988c987a
3 changed files with 150 additions and 8 deletions

View File

@@ -51,6 +51,9 @@ class RuntimeStorage(object):
def active_pids(self, pids): def active_pids(self, pids):
pass pass
def get_all_records(self):
pass
class MemcachedStorage(RuntimeStorage): class MemcachedStorage(RuntimeStorage):
def __init__(self, uri): def __init__(self, uri):

View File

@@ -14,8 +14,10 @@
# limitations under the License. # limitations under the License.
import contextlib import contextlib
import itertools
import mock import mock
import testtools import testtools
import uuid
from dashboard import web from dashboard import web
from stackalytics.processor import runtime_storage from stackalytics.processor import runtime_storage
@@ -29,17 +31,18 @@ class TestAPI(testtools.TestCase):
@contextlib.contextmanager @contextlib.contextmanager
def make_runtime_storage(data): def make_runtime_storage(data, record_type=None, **kwargs):
def get_by_key(key):
return data.get(key)
def get_update(pid): if record_type:
return [] 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
runtime_storage_inst = TestStorage(data)
setattr(web.app, 'stackalytics_vault', None) setattr(web.app, 'stackalytics_vault', None)
runtime_storage_inst = mock.Mock(runtime_storage.RuntimeStorage)
runtime_storage_inst.get_by_key = get_by_key
runtime_storage_inst.get_update = get_update
with mock.patch('stackalytics.processor.runtime_storage.' with mock.patch('stackalytics.processor.runtime_storage.'
'get_runtime_storage') as get_runtime_storage_mock: 'get_runtime_storage') as get_runtime_storage_mock:
@@ -48,3 +51,69 @@ def make_runtime_storage(data):
yield runtime_storage_inst yield runtime_storage_inst
finally: finally:
pass pass
class TestStorage(runtime_storage.RuntimeStorage):
def __init__(self, data):
super(TestStorage, self).__init__('test://')
self.data = data
def get_update(self, pid):
for record in self.get_all_records():
yield record
def get_by_key(self, key):
return self.data.get(key)
def set_by_key(self, key, value):
super(TestStorage, self).set_by_key(key, value)
def get_all_records(self):
for n in range(self.get_by_key('record:count') or 0):
record = self.get_by_key('record:%s' % n)
if record:
yield record
def generate_commits(**kwargs):
for values in product(**kwargs):
commit = {
'commit_id': str(uuid.uuid4()),
'lines_added': 9, 'module': 'nova', 'record_type': 'commit',
'message': 'Closes bug 1212953\n\nChange-Id: '
'I33f0f37b6460dc494abf2520dc109c9893ace9e6\n',
'subject': 'Fixed affiliation of Edgar and Sumit', 'loc': 10,
'user_id': 'john_doe',
'primary_key': str(uuid.uuid4()),
'author_email': 'john_doe@ibm.com', 'company_name': 'IBM',
'lines_deleted': 1, 'week': 2275,
'blueprint_id': None, 'bug_id': u'1212953',
'files_changed': 1, 'author_name': u'John Doe',
'date': 1376737923, 'launchpad_id': u'john_doe',
'branches': set([u'master']),
'change_id': u'I33f0f37b6460dc494abf2520dc109c9893ace9e6',
'release': u'icehouse'
}
commit.update(values)
yield commit
def generate_records(record_types, **kwargs):
if 'commit' in record_types:
for commit in generate_commits(**kwargs):
yield commit
def product(**kwargs):
position_to_key = {}
values = []
for key, value in kwargs.iteritems():
position_to_key[len(values)] = key
values.append(value)
for chain in itertools.product(*values):
result = {}
for position, key in position_to_key.iteritems():
result[key] = chain[position]
yield result

70
tests/api/test_users.py Normal file
View File

@@ -0,0 +1,70 @@
# 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 TestAPIUsers(test_api.TestAPI):
def test_users_empty(self):
with test_api.make_runtime_storage({}):
response = self.app.get('/api/1.0/users')
self.assertEqual(200, response.status_code)
def test_users(self):
with test_api.make_runtime_storage(
{'repos': [{'module': 'nova', 'project_type': 'openstack',
'organization': 'openstack',
'uri': 'git://github.com/openstack/nova.git'}]},
['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))
self.assertIn({'id': 'john_doe', 'text': 'John Doe'}, users)
self.assertIn({'id': 'bill_smith', 'text': 'John Doe'}, users)
def test_users_search(self):
with test_api.make_runtime_storage(
{'repos': [{'module': 'nova', 'project_type': 'openstack',
'organization': 'openstack',
'uri': 'git://github.com/openstack/nova.git'}]},
['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))
self.assertIn({'id': 'john_doe', 'text': 'John Doe'}, users)
def test_user_details(self):
with test_api.make_runtime_storage(
{'user:john_doe': {
'seq': 1, 'user_id': 'john_doe', 'user_name': 'John Doe',
'companies': [{'company_name': 'NEC', 'end_date': 0}],
'emails': 'john_doe@gmail.com'}}):
response = self.app.get('/api/1.0/users/john_doe')
user = json.loads(response.data)['user']
self.assertEqual('john_doe', user['user_id'])
def test_user_not_found(self):
with test_api.make_runtime_storage(
{'user:john_doe': {
'seq': 1, 'user_id': 'john_doe', 'user_name': 'John Doe',
'companies': [{'company_name': 'NEC', 'end_date': 0}],
'emails': 'john_doe@gmail.com'}}):
response = self.app.get('/api/1.0/users/nonexistent')
self.assertEqual(404, response.status_code)