当前位置: 首页>>代码示例>>Python>>正文


Python TestApp.session_transaction方法代码示例

本文整理汇总了Python中flask.ext.webtest.TestApp.session_transaction方法的典型用法代码示例。如果您正苦于以下问题:Python TestApp.session_transaction方法的具体用法?Python TestApp.session_transaction怎么用?Python TestApp.session_transaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在flask.ext.webtest.TestApp的用法示例。


在下文中一共展示了TestApp.session_transaction方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: RouteTestCase

# 需要导入模块: from flask.ext.webtest import TestApp [as 别名]
# 或者: from flask.ext.webtest.TestApp import session_transaction [as 别名]
class RouteTestCase(BaseTestCase):
    """
    Base test class for dealing with HTTP route handlers.
    """
    def setUp(self):
        super(RouteTestCase, self).setUp()

        # Setup app for testing
        self.app = TestApp(app, db=db, use_session_scopes=True)
        self.app_context = app.app_context()
        self.app_context.push()

        # Setup database for testing
        self.db = db
        self.db.create_all()

        # Create a new user
        user = User(
            email='[email protected]',
            password='secret')

        self.db.session.add(user)
        self.db.session.commit()

        # Assign the user to the session
        with self.app.session_transaction() as sess:
            sess['user_id'] = user.id

    def tearDown(self):
        # Clean up the test app and database
        db.drop_all()
        self.app_context.pop()
开发者ID:CivBase,项目名称:testing-parse,代码行数:34,代码来源:utils.py

示例2: WebTestMixin

# 需要导入模块: from flask.ext.webtest import TestApp [as 别名]
# 或者: from flask.ext.webtest.TestApp import session_transaction [as 别名]
class WebTestMixin(object):
    def create_app(self):
        config = os.environ.get('KOZMIC_CONFIG', 'kozmic.config.TestingConfig')
        return create_app(config)

    def setup_app_and_ctx(self):
        self.app = self.create_app()
        self.ctx = self.app.app_context()
        self.ctx.push()
        self.w = TestApp(self.app)

    def teardown_app_and_ctx(self):
        self.ctx.pop()

    def login(self, user_id):
        with self.w.session_transaction() as sess:
            sess['user_id'] = user_id
开发者ID:j-u-p-iter,项目名称:kozmic-ci,代码行数:19,代码来源:__init__.py

示例3: TestMainFeatures

# 需要导入模块: from flask.ext.webtest import TestApp [as 别名]
# 或者: from flask.ext.webtest.TestApp import session_transaction [as 别名]
class TestMainFeatures(unittest.TestCase):
    def setUp(self):
        self.app = create_app()
        self.w = TestApp(self.app)

    def test_single_template(self):
        r = self.w.get('/')
        self.assertFalse(r.flashes)
        self.assertEqual(len(r.contexts), 1)

        self.assertEqual(r.context['text'], 'Hello!')
        self.assertEqual(r.template, 'template.html')
        self.assertNotIn('qwerty', r.session)

    def test_two_templates_and_flash_messages(self):
        r = self.w.get('/').form.submit()
        self.assertEqual(len(r.contexts), 2)

        if flask_gte_0_10:
            self.assertEqual(len(r.flashes), 2)
            category, message = r.flashes[0]
            self.assertEqual(message, 'You have pressed "Quit"...')

            category, message = r.flashes[1]
            self.assertEqual(message, 'Flash message that will never be shown')
        else:
            self.assertEqual(len(r.flashes), 1)
            category, message = r.flashes[0]
            self.assertEqual(message, 'You have pressed "Quit"...')

        with self.assertRaises(AssertionError):
            r.context  # Because there are more than one used templates
        self.assertEqual(
            r.contexts['template.html']['text'],
            'Goodbye!')
        self.assertEqual(
            r.contexts['extra-template.html']['extra_text'],
            'Some text.')

    def test_session_transaction(self):
        r = self.w.get('/whoami/')
        self.assertEqual(r.body, 'nobody')

        with self.w.session_transaction() as sess:
            sess['username'] = 'aromanovich'

        r = self.w.get('/whoami/')

        self.assertEqual(r.session['username'], 'aromanovich')
        self.assertEqual(r.body, 'aromanovich')

    def test_init(self):
        w = TestApp(self.app)
        self.assertEqual(w.get('/').status_code, 200)

        original_server_name = self.app.config['SERVER_NAME']
        try:
            self.app.config['SERVER_NAME'] = 'webtest-app.local'
            w = TestApp(self.app)
            self.assertEqual(w.get('/').status_code, 200)
        finally:
            self.app.config['SERVER_NAME'] = original_server_name
开发者ID:liushaochan,项目名称:cn486,代码行数:64,代码来源:test.py


注:本文中的flask.ext.webtest.TestApp.session_transaction方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。