Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #162 from Connexions/testing-in-practice
Browse files Browse the repository at this point in the history
Intercept testing in practice
  • Loading branch information
reedstrm committed Dec 10, 2014
2 parents 4cf3a96 + 2bc0b23 commit 9f20214
Show file tree
Hide file tree
Showing 8 changed files with 611 additions and 1,064 deletions.
11 changes: 10 additions & 1 deletion cnxauthoring/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,25 @@


class DocumentNotFoundError(Exception):

def __init__(self, document_id):
self.message = 'Document Not Found: {}'.format(document_id)

def __str__(self):
return self.message


class ArchiveConnectionError(Exception):

def __init__(self, message=None):
self.message = message

def __str__(self):
return self.message


class PublishingError(Exception):

def __init__(self, response):
self.message = 'Publishing Error: {} {}'.format(
response.status_code, response.content)
Expand All @@ -44,7 +54,6 @@ def __str__(self):
return self.message



class License:
"""A declaration of authority typically assigned to things."""

Expand Down
68 changes: 59 additions & 9 deletions cnxauthoring/tests/intercept.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
add_wsgi_intercept, remove_wsgi_intercept,
)

from cnxarchive import config
from cnxarchive.database import initdb as archive_initdb
from cnxarchive import main as archive_main
from cnxpublishing.db import initdb as publishing_initdb
from cnxpublishing.main import main as publishing_main

from .testing import integration_test_settings


Expand Down Expand Up @@ -57,6 +63,51 @@ def _parse_url_from_settings(settings, url_key):
return host, port


def _amend_archive_data():
"""This contains data modifications to archive that are specific to
the authoring tests.
"""
# **Only add to this function if you really really must.**
# The idea is to utilize as much of the archive data as possible.
# We do this because that data has been tested and adding things here
# may unintentionally insert an assumption about the data in archive.

conn_str = publishing_settings()[config.CONNECTION_STRING]

with psycopg2.connect(conn_str) as db_connection:
with db_connection.cursor() as cursor:
cursor.execute("""\
INSERT INTO document_controls (uuid, licenseid) VALUES
('a3f7c934-2a89-4baf-a9a9-a89d957586d2', 11);
INSERT INTO abstracts (abstractid, abstract, html) VALUES
(9000, '', '');
INSERT INTO modules
(module_ident, portal_type, uuid,
name, abstractid, licenseid, doctype, stateid,
submitter, submitlog,
authors)
VALUES
(9000, 'Module', 'a3f7c934-2a89-4baf-a9a9-a89d957586d2',
'missing resource', 9000, 11, '', null,
'cnxcap', 'tests derive-from with missing resource',
'{cnxcap}');
INSERT INTO files
(fileid, file)
VALUES
(9000, '<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><p>module with a missing resource</p><img src="/resources/aca93d69479e75244b01272902968d8349a548f4/python"/></body></html>');
INSERT INTO module_files
(module_ident, fileid, filename, mimetype)
VALUES
(9000, 9000, 'index.cnxml.html', 'text/html');""")


def _amend_publishing_data():
"""This contains data modifications of the publishing/archive data
that are specific to the authoring tests.
"""
# NOOP


def install_intercept():
"""Initializes both the archive and publishing applications.
Then this will register intercepts for both applications using
Expand All @@ -66,7 +117,6 @@ def install_intercept():
Therefore, it is a good idea to only initialize the applications
during testcase class setup (i.e. setUpClass).
"""
from cnxarchive import config
settings = publishing_settings()
authoring_settings = integration_test_settings()

Expand All @@ -77,11 +127,9 @@ def install_intercept():
cursor.execute("DROP SCHEMA public CASCADE; CREATE SCHEMA public")

# Initialize the archive database.
from cnxarchive.database import initdb
initdb(settings)
archive_initdb(settings)
# Initialize the publishing database.
from cnxpublishing.db import initdb
initdb(connection_string)
publishing_initdb(connection_string)
with psycopg2.connect(connection_string) as db_connection:
with db_connection.cursor() as cursor:
filepath = config.TEST_DATA_SQL_FILE
Expand All @@ -101,22 +149,24 @@ def install_intercept():
with open(filepath, 'r') as fb:
cursor.execute(fb.read())

# Make amendments to the data that are specific to the authoring tests.
_amend_archive_data()
_amend_publishing_data()

# Set up the intercept for archive
from cnxarchive import main
global _archive_app
if not _archive_app:
_archive_app = main({}, **publishing_settings())
_archive_app = archive_main({}, **publishing_settings())
make_app = lambda : _archive_app
# Grab the configured archive url from the authoring config.
host, port = _parse_url_from_settings(authoring_settings,
'archive.url')
add_wsgi_intercept(host, port, make_app)

# Set up the intercept for publishing
from cnxpublishing.main import main
global _publishing_app
if not _publishing_app:
_publishing_app = main({}, **publishing_settings())
_publishing_app = publishing_main({}, **publishing_settings())
make_app = lambda : _publishing_app
# Grab the configured publishing url from the authoring config.
host, port = _parse_url_from_settings(authoring_settings,
Expand Down
Loading

0 comments on commit 9f20214

Please sign in to comment.