2024-08-05 21:49:18 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
|
|
|
|
from unittest import TestCase
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
|
|
from pyramid import testing
|
|
|
|
|
|
|
|
from wuttjamaican.conf import WuttaConfig
|
|
|
|
from wuttaweb import subscribers
|
2024-08-12 21:17:08 -05:00
|
|
|
from wuttaweb.menus import MenuHandler
|
2024-08-05 21:49:18 -05:00
|
|
|
|
|
|
|
|
2024-08-12 21:17:08 -05:00
|
|
|
class DataTestCase(TestCase):
|
2024-08-05 21:49:18 -05:00
|
|
|
"""
|
2024-08-12 21:17:08 -05:00
|
|
|
Base class for test suites requiring a full (typical) database.
|
2024-08-05 21:49:18 -05:00
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
2024-08-12 21:17:08 -05:00
|
|
|
self.setup_db()
|
2024-08-05 21:49:18 -05:00
|
|
|
|
2024-08-12 21:17:08 -05:00
|
|
|
def setup_db(self):
|
2024-08-05 21:49:18 -05:00
|
|
|
self.config = WuttaConfig(defaults={
|
|
|
|
'wutta.db.default.url': 'sqlite://',
|
|
|
|
})
|
2024-08-12 21:17:08 -05:00
|
|
|
self.app = self.config.get_app()
|
2024-08-05 21:49:18 -05:00
|
|
|
|
2024-08-12 21:17:08 -05:00
|
|
|
# init db
|
|
|
|
model = self.app.model
|
|
|
|
model.Base.metadata.create_all(bind=self.config.appdb_engine)
|
|
|
|
self.session = self.app.make_session()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.teardown_db()
|
|
|
|
|
|
|
|
def teardown_db(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class WebTestCase(DataTestCase):
|
|
|
|
"""
|
|
|
|
Base class for test suites requiring a full (typical) web app.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.setup_web()
|
2024-08-05 21:49:18 -05:00
|
|
|
|
2024-08-12 21:17:08 -05:00
|
|
|
def setup_web(self):
|
|
|
|
self.setup_db()
|
|
|
|
self.request = testing.DummyRequest()
|
2024-08-05 21:49:18 -05:00
|
|
|
self.pyramid_config = testing.setUp(request=self.request, settings={
|
|
|
|
'wutta_config': self.config,
|
|
|
|
'mako.directories': ['wuttaweb:templates'],
|
2024-08-12 21:17:08 -05:00
|
|
|
# TODO: have not need this yet, but will?
|
|
|
|
# 'pyramid_deform.template_search_path': 'wuttaweb:templates/deform',
|
2024-08-05 21:49:18 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
# init web
|
|
|
|
self.pyramid_config.include('pyramid_mako')
|
|
|
|
self.pyramid_config.include('wuttaweb.static')
|
|
|
|
self.pyramid_config.include('wuttaweb.views.essential')
|
|
|
|
self.pyramid_config.add_subscriber('wuttaweb.subscribers.before_render',
|
|
|
|
'pyramid.events.BeforeRender')
|
|
|
|
|
|
|
|
# setup new request w/ anonymous user
|
|
|
|
event = MagicMock(request=self.request)
|
|
|
|
subscribers.new_request(event)
|
|
|
|
def user_getter(request, **kwargs): pass
|
|
|
|
subscribers.new_request_set_user(event, db_session=self.session,
|
|
|
|
user_getter=user_getter)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.teardown_web()
|
|
|
|
|
|
|
|
def teardown_web(self):
|
|
|
|
testing.tearDown()
|
2024-08-12 21:17:08 -05:00
|
|
|
self.teardown_db()
|
|
|
|
|
|
|
|
|
|
|
|
class NullMenuHandler(MenuHandler):
|
|
|
|
"""
|
|
|
|
Dummy menu handler for testing.
|
|
|
|
"""
|
|
|
|
def make_menus(self, request, **kwargs):
|
|
|
|
return []
|