You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.7 KiB
70 lines
1.7 KiB
7 years ago
|
import pytest
|
||
|
import transaction
|
||
|
|
||
|
from pyramid import testing
|
||
|
|
||
|
|
||
|
APP_SETTINGS = {
|
||
|
'sqlalchemy.url': 'sqlite:///:memory:',
|
||
|
}
|
||
7 years ago
|
|
||
|
EXAMPLE_USER_DATA = {
|
||
|
'UNVALIDATED': (1, 'Graham', 'Chapman'),
|
||
|
'NEW': (2, 'John', 'Cleese'),
|
||
|
'USER': (3, 'Terry', 'Gilliam'),
|
||
|
'PURCHASER': (4, 'Eric', 'Idle'),
|
||
|
'ADMIN': (5, 'Terry', 'Jones'),
|
||
|
'INACTIVE': (6, 'Michael', 'Palin'),
|
||
|
}
|
||
|
|
||
7 years ago
|
|
||
7 years ago
|
# fixtures
|
||
7 years ago
|
|
||
|
@pytest.fixture(scope='session')
|
||
|
def app_config():
|
||
|
''' fixture for tests requiring a pyramid.testing setup '''
|
||
|
with testing.testConfig(settings=APP_SETTINGS) as config:
|
||
|
config.include('pyramid_jinja2')
|
||
|
config.include('pyramid_listing')
|
||
|
yield config
|
||
|
|
||
|
|
||
7 years ago
|
@pytest.fixture(scope='function')
|
||
7 years ago
|
def dbsession(app_config):
|
||
|
''' fixture for testing with database connection '''
|
||
|
from ordr.models.meta import Base
|
||
|
from ordr.models import (
|
||
|
get_engine,
|
||
|
get_session_factory,
|
||
|
get_tm_session
|
||
|
)
|
||
|
|
||
|
settings = app_config.get_settings()
|
||
|
engine = get_engine(settings)
|
||
|
session_factory = get_session_factory(engine)
|
||
|
session = get_tm_session(session_factory, transaction.manager)
|
||
|
Base.metadata.create_all(engine)
|
||
|
|
||
|
yield session
|
||
|
|
||
|
transaction.abort()
|
||
|
Base.metadata.drop_all(engine)
|
||
7 years ago
|
|
||
|
|
||
|
# helpers
|
||
|
|
||
|
def get_example_user(role):
|
||
|
''' get the user model for one well known user '''
|
||
|
from ordr.models import User
|
||
|
id_, first_name, last_name = EXAMPLE_USER_DATA[role.name]
|
||
|
user = User(
|
||
|
id=id_,
|
||
|
username=first_name + last_name,
|
||
7 years ago
|
first_name=first_name,
|
||
|
last_name=last_name,
|
||
|
email=last_name.lower() + '@example.com',
|
||
7 years ago
|
role=role
|
||
|
)
|
||
|
user.set_password(first_name)
|
||
|
return user
|