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


Python SessionStore.create方法代码示例

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


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

示例1: test_duplicate_create

# 需要导入模块: from user_sessions.backends.db import SessionStore [as 别名]
# 或者: from user_sessions.backends.db.SessionStore import create [as 别名]
    def test_duplicate_create(self):
        s1 = SessionStore('Python/2.7', '127.0.0.1', 'DUPLICATE')
        s1.create()
        s2 = SessionStore('Python/2.7', '127.0.0.1', 'DUPLICATE')
        s2.create()
        self.assertNotEqual(s1.session_key, s2.session_key)

        s3 = SessionStore('Python/2.7', '127.0.0.1', s1.session_key)
        with self.assertRaises(CreateError):
            s3.save(must_create=True)
开发者ID:doselect,项目名称:django-user-sessions,代码行数:12,代码来源:tests.py

示例2: SessionStoreTest

# 需要导入模块: from user_sessions.backends.db import SessionStore [as 别名]
# 或者: from user_sessions.backends.db.SessionStore import create [as 别名]
class SessionStoreTest(TestCase):
    def setUp(self):
        self.store = SessionStore('Python/2.7', '127.0.0.1', None)

    def test_untouched_init(self):
        self.assertFalse(self.store.modified)
        self.assertFalse(self.store.accessed)

    def test_auth_session_key(self):
        self.assertFalse(auth.SESSION_KEY in self.store)
        self.assertFalse(self.store.modified)
        self.assertTrue(self.store.accessed)

        self.store.get(auth.SESSION_KEY)
        self.assertFalse(self.store.modified)

        self.store[auth.SESSION_KEY] = 1
        self.assertTrue(self.store.modified)

    def test_save(self):
        self.store[auth.SESSION_KEY] = 1
        self.store.save()

        session = Session.objects.get(pk=self.store.session_key)
        self.assertEqual(session.user_agent, 'Python/2.7')
        self.assertEqual(session.ip, '127.0.0.1')
        self.assertEqual(session.user_id, 1)
        self.assertAlmostEqual(now(), session.last_activity,
                               delta=timedelta(seconds=5))

    def test_load_unmodified(self):
        self.store[auth.SESSION_KEY] = 1
        self.store.save()
        store2 = SessionStore('Python/2.7', '127.0.0.1',
                              self.store.session_key)
        store2.load()
        self.assertEqual(store2.user_agent, 'Python/2.7')
        self.assertEqual(store2.ip, '127.0.0.1')
        self.assertEqual(store2.user_id, 1)
        self.assertEqual(store2.modified, False)

    def test_load_modified(self):
        self.store[auth.SESSION_KEY] = 1
        self.store.save()
        store2 = SessionStore('Python/3.3', '8.8.8.8', self.store.session_key)
        store2.load()
        self.assertEqual(store2.user_agent, 'Python/3.3')
        self.assertEqual(store2.ip, '8.8.8.8')
        self.assertEqual(store2.user_id, 1)
        self.assertEqual(store2.modified, True)

    def test_duplicate_create(self):
        s1 = SessionStore('Python/2.7', '127.0.0.1', 'DUPLICATE')
        s1.create()
        s2 = SessionStore('Python/2.7', '127.0.0.1', 'DUPLICATE')
        s2.create()
        self.assertNotEqual(s1.session_key, s2.session_key)

        s3 = SessionStore('Python/2.7', '127.0.0.1', s1.session_key)
        with self.assertRaises(CreateError):
            s3.save(must_create=True)

    def test_integrity(self):
        self.store.user_agent = None
        with self.assertRaisesRegexp(
                IntegrityError,
                '(user_sessions_session.user_agent may not be NULL|'
                'NOT NULL constraint failed: user_sessions_session.user_agent)'
        ):
            self.store.save()

    def test_delete(self):
        # not persisted, should just return
        self.store.delete()

        # create, then delete
        self.store.create()
        session_key = self.store.session_key
        self.store.delete()

        # non-existing sessions, should not raise
        self.store.delete()
        self.store.delete(session_key)

    def test_clear(self):
        """
        Clearing the session should clear all non-browser information
        """
        self.store[auth.SESSION_KEY] = 1
        self.store.clear()
        self.store.save()

        session = Session.objects.get(pk=self.store.session_key)
        self.assertEqual(session.user_id, None)
开发者ID:doselect,项目名称:django-user-sessions,代码行数:96,代码来源:tests.py


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