Add REST API methods for test prefixes

This adds 2 new REST API methods for getting a list of test prefixes
('/tests/prefix') and getting a paginated list of tests for a given
prefix ('/tests/prefix/<prefix>').

Depends-On: I55e37c763f3f3ffc2840fddc8109d601291fcc0f
Change-Id: Ib6584a7a5600037dd33cce8fb4466495ed8e83d1
This commit is contained in:
Tim Buckley 2016-03-02 15:24:46 -07:00
parent 8d1bbea07a
commit 6850e8a487
3 changed files with 48 additions and 1 deletions

View File

@ -344,6 +344,25 @@ def get_tests():
return jsonify({'tests': tests})
@app.route('/tests/prefix', methods=['GET'])
def get_test_prefixes():
with session_scope() as session:
return jsonify(api.get_test_prefixes(session))
@app.route('/tests/prefix/<path:prefix>', methods=['GET'])
def get_tests_by_prefix(prefix):
limit = flask.request.args.get('limit', 100)
offset = flask.request.args.get('offset', 0)
with session_scope() as session:
db_tests = api.get_tests_by_prefix(prefix, session,
limit=limit, offset=offset)
tests = [test.to_dict() for test in db_tests]
return jsonify({'tests': tests})
def _check_db_availability():
try:
global engine

View File

@ -134,6 +134,34 @@ class TestRestAPI(base.TestCase):
self.assertEqual(json.loads(res.data.decode('utf-8')),
expected_response)
@mock.patch('subunit2sql.db.api.get_test_prefixes',
return_value=['tempest', 'common', 'api'])
def test_get_test_prefixes(self, api_mock):
res = self.app.get('/tests/prefix')
self.assertEqual(200, res.status_code)
expected_response = ['tempest', 'common', 'api']
self.assertEqual(json.loads(res.data.decode('utf-8')),
expected_response)
@mock.patch('subunit2sql.db.api.get_tests_by_prefix',
return_value=[models.Test(id='fake_id', test_id='prefix.test',
run_count=1, success=2, failure=2,
run_time=1.0)])
def test_get_tests_by_prefix(self, api_mock):
res = self.app.get('/tests/prefix/prefix')
self.assertEqual(200, res.status_code)
expected_response = {'tests': [{
'id': 'fake_id',
'test_id': 'prefix.test',
'run_count': 1,
'success': 2,
'failure': 2,
'run_time': 1.0
}]}
self.assertEqual(json.loads(res.data.decode('utf-8')),
expected_response)
@mock.patch('subunit2sql.db.api.get_all_runs_by_date',
return_value=[models.Run(
id='fake_id', skips=2, fails=4, passes=2, run_time=21.2,

View File

@ -3,7 +3,7 @@
# process, which may cause wedges in the gate later.
pbr>=1.6 # Apache-2.0
Flask<1.0,>=0.10 # BSD
subunit2sql>=1.3.0 # Apache-2.0
subunit2sql>=1.4.0 # Apache-2.0
SQLAlchemy<1.1.0,>=1.0.10 # MIT
flask-jsonpify # MIT
PyMySQL>=0.6.2 # MIT License