Ejemplo n.º 1
0
    def test_mapper_session_data(self):
        """
            Test the mapper only deletes the expired session and
            doesn't create a new task on queue
        """

        s1 = Session(session_key='1',
                     expire_date=datetime.utcnow() + timedelta(seconds=3600))
        s1.put()
        s2 = Session(session_key='2', expire_date=datetime.utcnow())
        s2.put()

        mapper = DeleteMapper(
            Session,
            filters={'lt': ('expire_date', datetime.utcnow())},
            deferred_batch_size=1)
        mapper.transaction()
        taskqueue = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME)

        tasks = taskqueue.GetTasks(mapper.queue)

        # Should be no task in the queue as
        # no more sessions to delete.
        self.assertEquals(len(tasks), 0)

        # Should be 1 valid session left
        self.assertEquals(Session.query().count(), 1)
        self.assertEquals(Session.query().get().session_key, '1')
Ejemplo n.º 2
0
 def txn():
     s = Session(
         key_name='session-%s' % self.session_key,
         session_key='session-%s' % self.session_key,
         session_data=self.encode(session_data),
         expire_date=self.get_expiry_date()
     )
     s.put()
Ejemplo n.º 3
0
 def txn():
     s = Session(
         id=self._get_or_create_session_key(),
         session_key=self.session_key,
         session_data=self.encode(session_data),
         expire_date=self.get_expiry_date()
     )
     s.put()
Ejemplo n.º 4
0
 def txn():
     s = Session(
         key_name=self.session_key,
         session_key=self.session_key,
         session_data=self.encode(session_data),
         expire_date=self.get_expiry_date()
     )
     s.put()
Ejemplo n.º 5
0
    def test_sessionmanager_load(self):
        """
            Test calling the Load method creates a valid
            session object with a Key
        """

        s = self.session.load()
        
        # Load creates a session key
        self.assertTrue(self.session.session_key)
        
        # Session data is empty
        self.assertEquals(s,{})
        
        self.session['z'] = 1
        self.session.save()
        
        s = self.session.load()

        # Loading Session data is now not empty
        self.assertEquals(s,{'z':1})
        
        # Test the session object is in the datastore
        s = Session.get_by_key_name(self.session.session_key)
        self.assertEquals(s.session_key,self.session.session_key)
        self.assertEquals(s.get_decoded(), {'z': 1})
Ejemplo n.º 6
0
    def test_sessionmanager_save(self):
        """
        Test SessionManager.save method
        """
        # Create a session
        self.session['y'] = 1
        self.session.save()

        Session.query(Session.session_key == self.session.session_key).get()

        # Change it
        self.session['y'] = 2
        self.session.save()

        # Clear cache, so that it will be retrieved from DB
        del self.session._session_cache
        self.assertEqual(self.session['y'], 2)
Ejemplo n.º 7
0
    def test_sessionmanager_save(self):
        """
        Test SessionManager.save method
        """
        # Create a session
        self.session['y'] = 1
        self.session.save()

        Session.query(Session.session_key == self.session.session_key).get()

        # Change it
        self.session['y'] = 2
        self.session.save()

        # Clear cache, so that it will be retrieved from DB
        del self.session._session_cache
        self.assertEqual(self.session['y'], 2)
Ejemplo n.º 8
0
 def setUp(self):
     self.testbed = testbed.Testbed()
     self.testbed.activate()
     self.testbed.init_datastore_v3_stub()
     self.testbed.init_memcache_stub()
     self.session = self.backend()
     for s in Session.all():
         s.delete()
Ejemplo n.º 9
0
    def test_sessionmanager_save(self):
        """
        Test SessionManager.save method
        """
        # Create a session
        self.session['y'] = 1
        self.session.save()

        s = Session.get_by_key_name('session-%s' % self.session.session_key)
Ejemplo n.º 10
0
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        self.session = self.backend()
        for s in Session.query().fetch():
            s.delete()

        # Make sure the default Use Timezone setting is False
        settings.USE_TZ = False
Ejemplo n.º 11
0
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        self.session = self.backend()
        for s in Session.query().fetch():
            s.delete()

        # Make sure the default Use Timezone setting is False
        settings.USE_TZ = False
Ejemplo n.º 12
0
    def test_session_get_decoded(self):
        """
        Test we can use Session.get_decoded to retrieve data stored
        in normal way
        """
        self.session['x'] = 1
        self.session.save()

        s = Session.get_by_key_name(self.session.session_key)

        self.assertEqual(s.get_decoded(), {'x': 1})
Ejemplo n.º 13
0
    def test_mapper_batch_size(self):
        """
        Test the amount of sessions gets deleted is
        the same as the deferred_batch_size
        """
        for i in range(0, 20):
            s = Session(session_key='%s' % i, expire_date=datetime.utcnow())
            s.put()

        mapper = DeleteMapper(Session, filters={'lt': ('expire_date', datetime.utcnow())}, deferred_batch_size=10)
        mapper.transaction()

        self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME)

        # Should be 10 left
        self.assertEquals(Session.query().count(), 10)

        mapper.transaction()

        # Should be 0 session left
        self.assertEquals(Session.query().count(), 0)
Ejemplo n.º 14
0
    def test_mapper(self):
        """
        Test a session gets deleted and another
        one has been put on as a task to be deleted
        """
        for i in range(0, 2):
            s = Session(session_key='%s' % i, expire_date=datetime.utcnow())
            s.put()

        mapper = DeleteMapper(Session, filters={'lt': ('expire_date', datetime.utcnow())}, deferred_batch_size=1)
        mapper.transaction()

        taskqueue = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME)

        # One expired session left
        self.assertEquals(Session.query().count(), 1)

        tasks = taskqueue.GetTasks(mapper.queue)

        # Another deferred task in the queue to remove the next expired session
        self.assertEquals(len(tasks), 1)
Ejemplo n.º 15
0
    def test_mapper(self):
        """
            Test a session gets deleted and another
            one has been put on as a task to be deleted
        """
        for i in range(0, 2):
            s = Session(session_key='%s' % i, expire_date=datetime.utcnow())
            s.put()

        mapper = DeleteMapper(
            Session,
            filters={'lt': ('expire_date', datetime.utcnow())},
            deferred_batch_size=1)
        mapper.transaction()

        taskqueue = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME)

        # One expired session left
        self.assertEquals(Session.query().count(), 1)

        tasks = taskqueue.GetTasks(mapper.queue)

        # Another deferred task in the queue to remove the next expired session
        self.assertEquals(len(tasks), 1)
Ejemplo n.º 16
0
    def test_mapper_session_data(self):
        """
        Test the mapper only deletes the expired session and
        doesn't create a new task on queue
        """

        s1 = Session(session_key='1', expire_date=datetime.utcnow() + timedelta(seconds=3600))
        s1.put()
        s2 = Session(session_key='2', expire_date=datetime.utcnow())
        s2.put()

        mapper = DeleteMapper(Session, filters={'lt': ('expire_date', datetime.utcnow())}, deferred_batch_size=1)
        mapper.transaction()
        taskqueue = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME)

        tasks = taskqueue.GetTasks(mapper.queue)

        # Should be no task in the queue as
        # no more sessions to delete.
        self.assertEquals(len(tasks), 0)

        # Should be 1 valid session left
        self.assertEquals(Session.query().count(), 1)
        self.assertEquals(Session.query().get().session_key, '1')
Ejemplo n.º 17
0
    def test_mapper_batch_size(self):
        """
            Test the amount of sessions gets deleted is
            the same as the deferred_batch_size
        """
        for i in range(0, 20):
            s = Session(session_key='%s' % i, expire_date=datetime.utcnow())
            s.put()

        mapper = DeleteMapper(
            Session,
            filters={'lt': ('expire_date', datetime.utcnow())},
            deferred_batch_size=10)
        mapper.transaction()

        taskqueue = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME)

        # Should be 10 left
        self.assertEquals(Session.query().count(), 10)

        mapper.transaction()

        # Should be 0 session left
        self.assertEquals(Session.query().count(), 0)
Ejemplo n.º 18
0
 def txn():
     s = Session(id=self._get_or_create_session_key(),
                 session_key=self.session_key,
                 session_data=self.encode(session_data),
                 expire_date=self.get_expiry_date())
     s.put()