You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.5 KiB
Python
65 lines
1.5 KiB
Python
from unittest import mock
|
|
|
|
from apistar.frameworks.wsgi import WSGIApp
|
|
from apistar.backends.sqlalchemy_backend import SQLAlchemyBackend, get_session
|
|
|
|
import pytest
|
|
|
|
from news.models import Base
|
|
from news.renders import JSONRenderer
|
|
from news.app import commands, routes, components
|
|
|
|
settings = {
|
|
'DATABASE': {
|
|
'URL': 'postgresql://apistar:local@localhost/testnews',
|
|
'METADATA': Base.metadata
|
|
},
|
|
'RENDERERS': [JSONRenderer()],
|
|
}
|
|
|
|
backend = SQLAlchemyBackend(settings)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def create_db():
|
|
"""Creates a test database with session scope"""
|
|
Base.metadata.create_all(backend.engine)
|
|
|
|
yield
|
|
|
|
Base.metadata.drop_all(backend.engine)
|
|
|
|
|
|
@pytest.fixture(name='rb_session')
|
|
def db_session_fixure():
|
|
"""Returns a SQLAlchemy session with automatic rollback"""
|
|
session = backend.Session()
|
|
try:
|
|
yield session
|
|
session.rollback()
|
|
except:
|
|
session.rollback()
|
|
raise
|
|
finally:
|
|
session.close()
|
|
|
|
|
|
@pytest.fixture(name='app', scope='session')
|
|
def apistar_app_fixture():
|
|
"""Returns a session scoped WSGIApp instance"""
|
|
return WSGIApp(settings=settings,
|
|
commands=commands,
|
|
components=components,
|
|
routes=routes)
|
|
|
|
|
|
@pytest.fixture(name='router', scope="session")
|
|
def apistar_router_fixture():
|
|
"""Returns a session scoped mock Router"""
|
|
mock_router = mock.Mock()
|
|
mock_router.configure_mock(
|
|
**{"reverse_url.return_value": "/mylocation"}
|
|
)
|
|
|
|
return mock_router
|