More tests for revisions-api. Fix minor bugs.
This commit is contained in:
parent
a0df0c459d
commit
adca9575b6
|
@ -17,6 +17,7 @@ import uuid
|
|||
|
||||
import falcon
|
||||
from falcon import request
|
||||
import six
|
||||
|
||||
from deckhand import errors
|
||||
|
||||
|
@ -72,7 +73,8 @@ class BaseResource(object):
|
|||
|
||||
def return_error(self, resp, status_code, message="", retry=False):
|
||||
resp.body = json.dumps(
|
||||
{'type': 'error', 'message': str(message), 'retry': retry})
|
||||
{'type': 'error', 'message': six.text_type(message),
|
||||
'retry': retry})
|
||||
resp.status = status_code
|
||||
|
||||
|
||||
|
@ -80,7 +82,7 @@ class DeckhandRequestContext(object):
|
|||
|
||||
def __init__(self):
|
||||
self.user = None
|
||||
self.roles = ['anyone']
|
||||
self.roles = ['*']
|
||||
self.request_id = str(uuid.uuid4())
|
||||
|
||||
def set_user(self, user):
|
||||
|
|
|
@ -32,16 +32,6 @@ LOG = logging.getLogger(__name__)
|
|||
class DocumentsResource(api_base.BaseResource):
|
||||
"""API resource for realizing CRUD endpoints for Documents."""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(DocumentsResource, self).__init__(**kwargs)
|
||||
self.authorized_roles = ['user']
|
||||
|
||||
def on_get(self, req, resp):
|
||||
pass
|
||||
|
||||
def on_head(self, req, resp):
|
||||
pass
|
||||
|
||||
def on_post(self, req, resp):
|
||||
"""Create a document. Accepts YAML data only."""
|
||||
if req.content_type != 'application/x-yaml':
|
||||
|
@ -73,6 +63,3 @@ class DocumentsResource(api_base.BaseResource):
|
|||
|
||||
resp.status = falcon.HTTP_201
|
||||
resp.body = json.dumps(created_documents)
|
||||
|
||||
def _check_document_exists(self):
|
||||
pass
|
||||
|
|
|
@ -122,8 +122,13 @@ def document_create(values, session=None):
|
|||
session = session or get_session()
|
||||
|
||||
filters = models.Document.UNIQUE_CONSTRAINTS
|
||||
try:
|
||||
existing_document = document_get(**{c: values[c] for c in filters
|
||||
if c != 'revision_id'})
|
||||
except db_exception.DBError:
|
||||
# Ignore bad data at this point. Allow creation to bubble up the error
|
||||
# related to bad data.
|
||||
existing_document = None
|
||||
|
||||
created_document = {}
|
||||
|
||||
|
|
|
@ -103,6 +103,9 @@ class DeckhandBase(models.ModelBase, models.TimestampMixin):
|
|||
# CircularReference.
|
||||
d.pop("_sa_instance_state")
|
||||
|
||||
if 'deleted_at' not in d:
|
||||
d.setdefault('deleted_at', None)
|
||||
|
||||
for k in ["created_at", "updated_at", "deleted_at", "deleted"]:
|
||||
if k in d and d[k]:
|
||||
d[k] = d[k].isoformat()
|
||||
|
|
|
@ -80,6 +80,7 @@ class DocumentValidation(object):
|
|||
raise errors.InvalidFormat(
|
||||
"Could not find 'abstract' property from document.")
|
||||
|
||||
# TODO: This should be done inside a different module.
|
||||
if is_abstract:
|
||||
LOG.info(
|
||||
"Skipping validation for the document because it is abstract")
|
||||
|
|
|
@ -24,7 +24,12 @@ from deckhand.tests.unit import base
|
|||
|
||||
class DocumentFixture(object):
|
||||
|
||||
def get_minimal_fixture(self, **kwargs):
|
||||
EXPECTED_FIELDS = ("created_at", "updated_at", "deleted_at", "deleted",
|
||||
"id", "schema", "name", "_metadata", "data",
|
||||
"revision_id")
|
||||
|
||||
@staticmethod
|
||||
def get_minimal_fixture(**kwargs):
|
||||
fixture = {'data': 'fake document data',
|
||||
'metadata': {'name': 'fake metadata'},
|
||||
'schema': 'deckhand/v1'}
|
||||
|
@ -34,13 +39,38 @@ class DocumentFixture(object):
|
|||
|
||||
class TestDocumentsApi(base.DeckhandWithDBTestCase):
|
||||
|
||||
def _validate_document(self, expected, actual):
|
||||
expected['_metadata'] = expected.pop('metadata')
|
||||
def _create_document(self, payload):
|
||||
doc = db_api.document_create(payload)
|
||||
self._validate_document(expected=payload, actual=doc)
|
||||
return doc
|
||||
|
||||
def _get_document(self, **fields):
|
||||
doc = db_api.document_get(**fields)
|
||||
self._validate_document(actual=doc)
|
||||
return doc
|
||||
|
||||
def _get_revision(self, revision_id):
|
||||
revision = db_api.revision_get(revision_id)
|
||||
self._validate_revision(revision)
|
||||
return revision
|
||||
|
||||
def _validate_document(self, actual, expected=None, is_deleted=False):
|
||||
# Validate that the document has all expected fields and is a dict.
|
||||
expected_fields = list(DocumentFixture.EXPECTED_FIELDS)
|
||||
if not is_deleted:
|
||||
expected_fields.remove('deleted_at')
|
||||
|
||||
# TODO: Validate "status" fields, like created_at.
|
||||
self.assertIsInstance(actual, dict)
|
||||
for field in expected_fields:
|
||||
self.assertIn(field, actual)
|
||||
|
||||
# ``_metadata`` is used in the DB schema as ``metadata`` is reserved.
|
||||
actual['metadata'] = actual.pop('_metadata')
|
||||
|
||||
if expected:
|
||||
# Validate that the expected values are equivalent to actual
|
||||
# values.
|
||||
for key, val in expected.items():
|
||||
self.assertIn(key, actual)
|
||||
self.assertEqual(val, actual[key])
|
||||
|
||||
def _validate_revision(self, revision):
|
||||
|
@ -50,12 +80,34 @@ class TestDocumentsApi(base.DeckhandWithDBTestCase):
|
|||
self.assertThat(revision[attr], matchers.MatchesAny(
|
||||
matchers.Is(None), matchers.IsInstance(unicode)))
|
||||
|
||||
def test_create_document(self):
|
||||
fixture = DocumentFixture().get_minimal_fixture()
|
||||
document = db_api.document_create(fixture)
|
||||
self._validate_document(fixture, document)
|
||||
def _validate_revision_connections(self, parent_document, parent_revision,
|
||||
child_document, child_revision,
|
||||
parent_child_connected=True):
|
||||
self.assertNotEqual(child_revision['id'], parent_revision['id'])
|
||||
self.assertEqual(parent_document['revision_id'], parent_revision['id'])
|
||||
self.assertEqual(child_document['revision_id'], child_revision['id'])
|
||||
|
||||
revision = db_api.revision_get(document['revision_id'])
|
||||
# Validate that the revisions are distinct and connected together.
|
||||
if parent_child_connected:
|
||||
self.assertEqual(parent_revision['child_id'], child_revision['id'])
|
||||
self.assertEqual(
|
||||
child_revision['parent_id'], parent_revision['id'])
|
||||
# Validate that the revisions are distinct but unconnected.
|
||||
else:
|
||||
self.assertIsNone(parent_revision['child_id'])
|
||||
self.assertIsNone(child_revision['parent_id'])
|
||||
|
||||
def test_create_and_get_document(self):
|
||||
payload = DocumentFixture.get_minimal_fixture()
|
||||
document = self._create_document(payload)
|
||||
retrieved_document = self._get_document(id=document['id'])
|
||||
self.assertEqual(document, retrieved_document)
|
||||
|
||||
def test_create_document_and_get_revision(self):
|
||||
payload = DocumentFixture.get_minimal_fixture()
|
||||
document = self._create_document(payload)
|
||||
|
||||
revision = self._get_revision(document['revision_id'])
|
||||
self._validate_revision(revision)
|
||||
self.assertEqual(document['revision_id'], revision['id'])
|
||||
|
||||
|
@ -69,27 +121,82 @@ class TestDocumentsApi(base.DeckhandWithDBTestCase):
|
|||
v |
|
||||
child_document --> child_revision
|
||||
"""
|
||||
fixture = DocumentFixture().get_minimal_fixture()
|
||||
child_document = db_api.document_create(fixture)
|
||||
child_payload = DocumentFixture.get_minimal_fixture()
|
||||
child_document = self._create_document(child_payload)
|
||||
|
||||
fixture['metadata'] = {'name': 'modified fake metadata'}
|
||||
parent_document = db_api.document_create(fixture)
|
||||
self._validate_document(fixture, parent_document)
|
||||
parent_payload = DocumentFixture.get_minimal_fixture()
|
||||
parent_payload['data'] = 'fake updated document data'
|
||||
parent_document = self._create_document(parent_payload)
|
||||
|
||||
# Validate that the new document was created.
|
||||
self.assertEqual({'name': 'modified fake metadata'},
|
||||
parent_document['_metadata'])
|
||||
self.assertEqual('fake updated document data', parent_document['data'])
|
||||
self.assertNotEqual(child_document['id'], parent_document['id'])
|
||||
|
||||
# Validate that the parent document has a different revision and
|
||||
# that the revisions and document links are correct.
|
||||
child_revision = db_api.revision_get(child_document['revision_id'])
|
||||
parent_revision = db_api.revision_get(parent_document['revision_id'])
|
||||
for revision in (child_revision, parent_revision):
|
||||
self._validate_revision(revision)
|
||||
child_revision = self._get_revision(child_document['revision_id'])
|
||||
parent_revision = self._get_revision(parent_document['revision_id'])
|
||||
|
||||
self.assertNotEqual(child_revision['id'], parent_revision['id'])
|
||||
self.assertEqual(parent_document['revision_id'],
|
||||
parent_revision['id'])
|
||||
self.assertEqual(child_document['revision_id'], child_revision['id'])
|
||||
self.assertEqual(parent_document['revision_id'], parent_revision['id'])
|
||||
self._validate_revision_connections(
|
||||
parent_document, parent_revision, child_document, child_revision)
|
||||
|
||||
def test_create_and_update_document_schema(self):
|
||||
"""
|
||||
Check that the following relationship is true:
|
||||
|
||||
parent_document --> parent_revision
|
||||
child_document --> child_revision
|
||||
|
||||
"schema" is unique so changing it results in a new document being
|
||||
created.
|
||||
"""
|
||||
child_payload = DocumentFixture.get_minimal_fixture()
|
||||
child_document = self._create_document(child_payload)
|
||||
|
||||
parent_payload = DocumentFixture.get_minimal_fixture()
|
||||
parent_payload['schema'] = 'deckhand/v2'
|
||||
parent_document = self._create_document(parent_payload)
|
||||
|
||||
# Validate that the new document was created.
|
||||
self.assertEqual('deckhand/v2', parent_document['schema'])
|
||||
self.assertNotEqual(child_document['id'], parent_document['id'])
|
||||
|
||||
# Validate that the parent document has a different revision and
|
||||
# that the revisions and document links are correct.
|
||||
child_revision = self._get_revision(child_document['revision_id'])
|
||||
parent_revision = self._get_revision(parent_document['revision_id'])
|
||||
|
||||
self._validate_revision_connections(
|
||||
parent_document, parent_revision, child_document, child_revision,
|
||||
False)
|
||||
|
||||
def test_create_and_update_document_metadata_name(self):
|
||||
"""
|
||||
Check that the following relationship is true:
|
||||
|
||||
parent_document --> parent_revision
|
||||
child_document --> child_revision
|
||||
|
||||
"metadata.name" is unique so changing it results in a new document
|
||||
being created.
|
||||
"""
|
||||
child_payload = DocumentFixture.get_minimal_fixture()
|
||||
child_document = self._create_document(child_payload)
|
||||
|
||||
parent_payload = DocumentFixture.get_minimal_fixture()
|
||||
parent_payload['metadata'] = {'name': 'fake updated metadata'}
|
||||
parent_document = self._create_document(parent_payload)
|
||||
|
||||
# Validate that the new document was created.
|
||||
self.assertEqual({'name': 'fake updated metadata'},
|
||||
parent_document['metadata'])
|
||||
self.assertNotEqual(child_document['id'], parent_document['id'])
|
||||
|
||||
# Validate that the parent document has a different revision and
|
||||
# that the revisions and document links are correct.
|
||||
child_revision = self._get_revision(child_document['revision_id'])
|
||||
parent_revision = self._get_revision(parent_document['revision_id'])
|
||||
|
||||
self._validate_revision_connections(
|
||||
parent_document, parent_revision, child_document, child_revision,
|
||||
False)
|
||||
|
|
Loading…
Reference in New Issue