Skip to content

Commit

Permalink
Add functional view test case for invoking content collation
Browse files Browse the repository at this point in the history
  • Loading branch information
karenc committed Apr 11, 2016
1 parent 62d924f commit e2cb49d
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions cnxpublishing/tests/views/test_publishing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
import unittest
from collections import OrderedDict
from copy import deepcopy
import uuid
try:
from unittest import mock
except ImportError:
import mock

import cnxepub
from pyramid import httpexceptions
Expand All @@ -17,6 +22,7 @@
from webtest.forms import Upload

from .. import use_cases
from ..testing import db_connect
from .base import BaseFunctionalViewTestCase


Expand Down Expand Up @@ -1292,3 +1298,68 @@ def test_new_to_publication_license_not_accepted(self):
# because all licenses and roles have been accepted.
self.app_check_state(publication_id, 'Done/Success',
headers=api_key_headers)


class CollateContentTestCase(BaseFunctionalViewTestCase):
def app_post_collate_content(self, ident_hash, headers=None, status=None):
path = '/contents/{}/collate-content'.format(ident_hash)
return self.app.post(path, headers=headers, status=status)

@db_connect
def test_not_book(self, cursor):
binder = use_cases.setup_COMPLEX_BOOK_ONE_in_archive(self, cursor)
cursor.connection.commit()
api_key_headers = self.gen_api_key_headers('some-trust')
self.app_post_collate_content(binder[0][0].ident_hash,
headers=api_key_headers,
status=400)

def test_not_found(self):
random_ident_hash = '{}@1'.format(uuid.uuid4())
api_key_headers = self.gen_api_key_headers('some-trust')
self.app_post_collate_content(random_ident_hash,
headers=api_key_headers,
status=404)

@db_connect
def test(self, cursor):
binder = use_cases.setup_COMPLEX_BOOK_ONE_in_archive(self, cursor)
cursor.connection.commit()
api_key_headers = self.gen_api_key_headers('some-trust')

# FIXME use collate with real ruleset when it is available

# Build some new metadata for the composite document.
metadata = [x.metadata.copy()
for x in cnxepub.flatten_to_documents(binder)][0]
del metadata['cnx-archive-uri']
del metadata['version']
metadata['title'] = "Made up of other things"

# Add some fake collation objects to the book.
content = '<p>composite</p>'
composite_doc = cnxepub.CompositeDocument(None, content, metadata)
composite_section = cnxepub.TranslucentBinder(
nodes=[composite_doc],
metadata={'title': "Other things"})

collated_doc_content = '<p>collated</p>'

def collate(binder_model):
binder_model[0][0].content = collated_doc_content
binder_model.append(composite_section)

with mock.patch('cnxpublishing.collation.collate_models') as mock_collate:
mock_collate.side_effect = collate

self.app_post_collate_content(binder.ident_hash,
headers=api_key_headers)

self.assertEqual(1, mock_collate.call_count)

# Ensure the tree as been stamped.
cursor.execute("SELECT tree_to_json(%s, %s, TRUE)::json;",
(binder.id, binder.metadata['version'],))
collated_tree = cursor.fetchone()[0]
self.assertIn(composite_doc.ident_hash,
cnxepub.flatten_tree_to_ident_hashes(collated_tree))

0 comments on commit e2cb49d

Please sign in to comment.