Add view abstraction layer for modifying DB data into view data.

This commit is contained in:
Felipe Monteiro 2017-07-31 20:08:38 +01:00
parent 1c942b23e3
commit 6299c4b123
11 changed files with 147 additions and 42 deletions

View File

@ -99,7 +99,7 @@ class DeckhandRequestContext(object):
def __init__(self):
self.user = None
self.roles = ['*']
self.roles = []
self.request_id = str(uuid.uuid4())
def set_user(self, user):

View File

@ -0,0 +1,26 @@
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
#
# 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 string
def to_camel_case(s):
return (s[0].lower() + string.capwords(s, sep='_').replace('_', '')[1:]
if s else s)
class ViewBuilder(object):
"""Model API responses as dictionaries."""
_collection_name = None

View File

@ -15,6 +15,7 @@
import falcon
from deckhand.control import base as api_base
from deckhand.control.views import revision as revision_view
from deckhand.db.sqlalchemy import api as db_api
@ -29,6 +30,7 @@ class RevisionsResource(api_base.BaseResource):
of each revision.
"""
revisions = db_api.revision_get_all()
resp = revision_view.ViewBuilder().list(revisions)
resp.status = falcon.HTTP_200
resp.append_header('Content-Type', 'application/x-yaml')

View File

View File

@ -0,0 +1,38 @@
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
#
# 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.
from deckhand.control import common
class ViewBuilder(common.ViewBuilder):
"""Model revision API responses as a python dictionary."""
_collection_name = 'revisions'
def list(self, revisions):
resp_body = {
'count': len(revisions),
'next': None,
'prev': None,
'results': []
}
for revision in revisions:
result = {}
for attr in ('id', 'created_at'):
result[common.to_camel_case(attr)] = revision[attr]
result['count'] = len(revision.pop('documents'))
resp_body['results'].append(result)
return resp_body

View File

@ -203,7 +203,6 @@ def revision_get(revision_id, session=None):
id=revision_id).one().to_dict()
except sa_orm.exc.NoResultFound:
raise errors.RevisionNotFound(revision=revision_id)
return revision
@ -211,22 +210,7 @@ def revision_get_all(session=None):
"""Return list of all revisions."""
session = session or get_session()
revisions = session.query(models.Revision).all()
revisions_resp = [r.to_dict() for r in revisions]
resp_body = {
'count': len(revisions_resp),
'next': None,
'prev': None,
'revisions': []
}
for revision in revisions_resp:
result = {}
for attr in ('id', 'created_at'):
result[utils.to_camel_case(attr)] = revision[attr]
result['count'] = len(revision.pop('documents'))
resp_body['revisions'].append(result)
return resp_body
return [r.to_dict() for r in revisions]
def revision_get_documents(revision_id, session=None, **filters):

View File

@ -16,15 +16,6 @@ import random
import uuid
def rand_uuid():
"""Generate a random UUID string
:return: a random UUID (e.g. '1dc12c7d-60eb-4b61-a7a2-17cf210155b6')
:rtype: string
"""
return uuidutils.generate_uuid()
def rand_uuid_hex():
"""Generate a random UUID hex string
@ -60,3 +51,12 @@ def rand_bool():
:rtype: boolean
"""
return random.choice([True, False])
def rand_int(min, max):
"""Generate a random integer value between range (`min`, `max`).
:return: a random integer between the range(`min`, `max`).
:rtype: integer
"""
return random.randint(min, max)

View File

@ -15,18 +15,14 @@
from deckhand.tests.unit.db import base
class TestRevisions(base.TestDbBase):
class TestRevisionViews(base.TestDbBase):
def test_list_revisions(self):
def test_list(self):
payload = [base.DocumentFixture.get_minimal_fixture()
for _ in range(4)]
self._create_documents(payload)
revisions = self._list_revisions()
self.assertIsInstance(revisions, dict)
self.assertIn('revisions', revisions)
self.assertIsInstance(revisions['revisions'], list)
revisions = revisions['revisions']
self.assertIsInstance(revisions, list)
self.assertEqual(1, len(revisions))
self.assertEqual(4, revisions[0]["count"])
self.assertEqual(4, len(revisions[0]['documents']))

View File

View File

@ -0,0 +1,66 @@
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
#
# 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.
from deckhand.control.views import revision
from deckhand.tests.unit.db import base
from deckhand.tests import test_utils
class TestRevisionViews(base.TestDbBase):
def setUp(self):
super(TestRevisionViews, self).setUp()
self.view_builder = revision.ViewBuilder()
def test_list_revisions(self):
payload = [base.DocumentFixture.get_minimal_fixture()
for _ in range(4)]
self._create_documents(payload)
revisions = self._list_revisions()
revisions_view = self.view_builder.list(revisions)
expected_attrs = ('next', 'prev', 'results', 'count')
for attr in expected_attrs:
self.assertIn(attr, revisions_view)
# Validate that only 1 revision was returned.
self.assertEqual(1, revisions_view['count'])
# Validate that the first revision has 4 documents.
self.assertIn('id', revisions_view['results'][0])
self.assertIn('count', revisions_view['results'][0])
self.assertEqual(4, revisions_view['results'][0]['count'])
def test_list_many_revisions(self):
docs_count = []
for _ in range(3):
doc_count = test_utils.rand_int(3, 9)
docs_count.append(doc_count)
payload = [base.DocumentFixture.get_minimal_fixture()
for _ in range(doc_count)]
self._create_documents(payload)
revisions = self._list_revisions()
revisions_view = self.view_builder.list(revisions)
expected_attrs = ('next', 'prev', 'results', 'count')
for attr in expected_attrs:
self.assertIn(attr, revisions_view)
# Validate that only 1 revision was returned.
self.assertEqual(3, revisions_view['count'])
# Validate that each revision has correct number of documents.
for idx, doc_count in enumerate(docs_count):
self.assertIn('count', revisions_view['results'][idx])
self.assertIn('id', revisions_view['results'][idx])
self.assertEqual(doc_count, revisions_view['results'][idx][
'count'])

View File

@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import string
def multi_getattr(multi_key, dict_data):
"""Iteratively check for nested attributes in the YAML data.
@ -47,8 +45,3 @@ def multi_getattr(multi_key, dict_data):
data = data.get(attr)
return data
def to_camel_case(s):
return (s[0].lower() + string.capwords(s, sep='_').replace('_', '')[1:]
if s else s)