'Test clearsessions command for clearing expired sessions.'

@override_settings(SESSION_ENGINE='django.contrib.sessions.backends.file', SESSION_COOKIE_AGE=0)
def test_clearsessions_command(self):

    storage_path = self.backend._get_storage_path()
    file_prefix = settings.SESSION_COOKIE_NAME
    def count_sessions():
        return len([session_file for session_file in os.listdir(storage_path) if session_file.startswith(file_prefix)])
    self.assertEqual(0, count_sessions())
    self.session['foo'] = 'bar'
    self.session.set_expiry(3600)
    self.session.save()
    other_session = self.backend()
    other_session['foo'] = 'bar'
    other_session.set_expiry((-3600))
    other_session.save()
    other_session2 = self.backend()
    other_session2['foo'] = 'bar'
    other_session2.save()
    self.assertEqual(3, count_sessions())
    management.call_command('clearsessions')
    self.assertEqual(1, count_sessions())
