Add tests back.

This commit is contained in:
Dorian 2016-09-06 08:38:43 -04:00
parent 24b35834e3
commit a06ee69cf1
8 changed files with 249 additions and 0 deletions

0
api/tests/fixtures/__init__.py vendored Normal file
View File

24
api/tests/fixtures/sample_site.py vendored Normal file
View File

@ -0,0 +1,24 @@
"""
Sample database test fixtures for database related tests.
:copyright: Copyright 2013-2015, Dorian Pula <dorian.pula@amber-penguin-software.ca>
:license: AGPL v3+
"""
import pathlib
def create_sample_page(page_id, title, content_file):
sample_content_path = pathlib.Path('docs', content_file)
with sample_content_path.open(encoding='utf-8') as markdown_file:
content = markdown_file.read()
return {'_id': page_id, 'title': title, 'content': content, 'doc_type': 'journal'}
def bulk_docs_export():
return [
create_sample_page('about', title='About Rookeries', content_file='about.md'),
create_sample_page('develop', title='Develop', content_file='develop.md'),
create_sample_page('license', title='License (AGPL)', content_file='license.md'),
]

View File

View File

View File

@ -0,0 +1,52 @@
"""
Test database code for handling the retrieval and storage of journal entries.
:copyright: Copyright 2015, Dorian Pula <dorian.pula@amber-penguin-software.ca>
:license: AGPL v3+
"""
import pytest
from pycouchdb import exceptions
from api.rookeries import database
@pytest.fixture
def test_config():
return {
database.COUCHDB_SERVER_CONNECTION: 'http://test.server.net:1234/',
database.COUCHDB_DATABASE: 'test_db',
}
def test_gets_database_connection_if_database_exists(mocker, test_config):
mock_server_cls = mocker.patch('pycouchdb.Server', autospec=True)
mock_server = mock_server_cls.return_value
actual_db = database.create_or_get_database(test_config)
mock_server_cls.assert_called_with(test_config[database.COUCHDB_SERVER_CONNECTION])
mock_server.database.assert_called_with(test_config[database.COUCHDB_DATABASE])
assert actual_db == mock_server.database.return_value
def test_creates_database_connection_if_database_does_not_exist(mocker, test_config):
mock_server_cls = mocker.patch('pycouchdb.Server', autospec=True)
mock_server = mock_server_cls.return_value
mock_server.database.side_effect = exceptions.NotFound
actual_db = database.create_or_get_database(test_config)
mock_server_cls.assert_called_with(test_config[database.COUCHDB_SERVER_CONNECTION])
mock_server.create.assert_called_with(test_config[database.COUCHDB_DATABASE])
assert actual_db == mock_server.create.return_value
def test_creates_database_connection_fails_if_database_server_not_found(mocker, test_config):
mock_server_cls = mocker.patch('pycouchdb.Server', autospec=True)
mock_server_cls.side_effect = RuntimeError
with pytest.raises(RuntimeError):
database.create_or_get_database(test_config)
mock_server_cls.assert_called_with(test_config[database.COUCHDB_SERVER_CONNECTION])

View File

@ -0,0 +1,31 @@
"""
Unit tests for the app status view.
:copyright: Copyright 2013-2015, Dorian Pula <dorian.pula@amber-penguin-software.ca>
:license: AGPL v3+
"""
import httplib
import json
import pytest
from api import rookeries
@pytest.fixture
def flask_test_app():
return rookeries.make_rookeries_app().test_client()
def test_app_status_view_returns_app_info_as_a_json(flask_test_app):
expected_info = {
'app': 'rookeries',
'version': rookeries.__version__,
}
actual = flask_test_app.get('/status')
assert actual.status_code == httplib.OK
actual_json = json.loads(actual.data)
assert actual_json == expected_info

View File

@ -0,0 +1,73 @@
"""
Unit tests for app configuration.
:copyright: Copyright 2013-2015, Dorian Pula <dorian.pula@amber-penguin-software.ca>
:license: AGPL v3+
"""
import contextlib
import copy
import os
import pytest
import rookeries
from api.rookeries import config
@pytest.fixture
def flask_test_app():
return rookeries.make_rookeries_app().test_client().application
def test_configuration_will_use_default_configuration_class(flask_test_app):
config.configure_web_app(flask_test_app)
expected_config = config.DefaultConfig()
for key, value in expected_config.__dict__.iteritems():
assert key in flask_test_app.config
assert value == flask_test_app.config[key]
def test_configuration_will_use_envs_with_appropriate_prefix(flask_test_app):
test_config_key = 'CONFIG_KEY'
test_config_value = 'ROOKERIES_CONFIG_VALUE'
with set_config_in_environ(test_config_key, test_config_value):
config.configure_web_app(flask_test_app)
assert test_config_key in flask_test_app.config
assert test_config_value == flask_test_app.config[test_config_key]
def test_configuration_will_skip_envs_with_wrong_prefix(flask_test_app):
test_config_key = 'CONFIG_KEY'
test_config_value = 'BAD_CONFIG_VALUE'
with set_config_in_environ(test_config_key, test_config_value, prefix='RANDOM_'):
config.configure_web_app(flask_test_app)
assert test_config_key not in flask_test_app.config
def test_configuration_from_environment_variable_will_override_default(flask_test_app):
test_config_key = 'COUCHDB_DATABASE'
test_config_value = 'test_environ'
with set_config_in_environ(test_config_key, test_config_value):
config.configure_web_app(flask_test_app)
assert test_config_key in flask_test_app.config
assert config.DefaultConfig.ADMIN_USERNAME != flask_test_app.config[test_config_key]
assert test_config_value == flask_test_app.config[test_config_key]
@contextlib.contextmanager
def set_config_in_environ(environ_variable, environ_value, prefix=config.ROOKERIES_ENV_CONFIG_PREFIX):
"""Context manager to change an environment variable."""
environ_name = ''.join((prefix, environ_variable))
original_environ_value = copy.deepcopy(os.environ.get(environ_name))
os.environ[environ_name] = environ_value
yield
os.environ.pop(environ_name)
if original_environ_value:
os.environ[environ_name] = original_environ_value

View File

@ -0,0 +1,69 @@
"""
Unit tests for testing serving of page content.
:copyright: Copyright 2013-2015, Dorian Pula <dorian.pula@amber-penguin-software.ca>
:license: AGPL v3+
"""
import httplib
import json
import mock
import pathlib
import pytest
from api import rookeries
@pytest.fixture
def flask_test_app():
return rookeries.make_rookeries_app().test_client()
@pytest.fixture
def expected_json_content():
with pathlib.Path('docs/about.md').open('r', encoding='UTF-8') as markdown_data:
content = markdown_data.read()
return content
def test_serve_journal_entry_view_returns_journal_entry_as_json(flask_test_app, expected_json_content):
expected_info = {
'doc_id': 'about',
'title': 'About Rookeries',
'content': expected_json_content,
'revision': mock.ANY,
}
actual = flask_test_app.get('/app/pages/about')
assert actual.status_code == httplib.OK
assert json.loads(actual.data) == expected_info
def test_serve_journal_entry_view_serving_landing_doc_returns_about_page(flask_test_app, expected_json_content):
expected_info = {
'doc_id': 'about',
'title': 'About Rookeries',
'content': expected_json_content,
'revision': mock.ANY,
}
actual = flask_test_app.get('/app/pages/landing')
assert actual.status_code == httplib.OK
assert json.loads(actual.data) == expected_info
def test_serve_journal_entry_view_returns_404_when_no_content_found(flask_test_app):
expect_json = {
'error': {
'code': 404,
'message': 'Not Found'
}
}
actual = flask_test_app.get('/app/pages/missing')
assert actual.status_code == httplib.NOT_FOUND
assert json.loads(actual.data) == expect_json