當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。