user_messages.py :  » Content-Management-Systems » PyLucid » PyLucid_standalone » django » contrib » messages » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Content Management Systems » PyLucid 
PyLucid » PyLucid_standalone » django » contrib » messages » tests » user_messages.py
from django import http
from django.contrib.auth.models import User
from django.contrib.messages.storage.user_messages import UserMessagesStorage,\
    LegacyFallbackStorage
from django.contrib.messages.tests.cookie import set_cookie_data
from django.contrib.messages.tests.fallback import FallbackTest
from django.test import TestCase


class UserMessagesTest(TestCase):

    def setUp(self):
        self.user = User.objects.create(username='tester')

    def test_add(self):
        storage = UserMessagesStorage(http.HttpRequest())
        self.assertRaises(NotImplementedError, storage.add, 'Test message 1')

    def test_get_anonymous(self):
        # Ensure that the storage still works if no user is attached to the
        # request.
        storage = UserMessagesStorage(http.HttpRequest())
        self.assertEqual(len(storage), 0)

    def test_get(self):
        storage = UserMessagesStorage(http.HttpRequest())
        storage.request.user = self.user
        self.user.message_set.create(message='test message')

        self.assertEqual(len(storage), 1)
        self.assertEqual(list(storage)[0].message, 'test message')


class LegacyFallbackTest(FallbackTest, TestCase):
    storage_class = LegacyFallbackStorage

    def setUp(self):
        super(LegacyFallbackTest, self).setUp()
        self.user = User.objects.create(username='tester')

    def get_request(self, *args, **kwargs):
        request = super(LegacyFallbackTest, self).get_request(*args, **kwargs)
        request.user = self.user
        return request

    def test_get_legacy_only(self):
        request = self.get_request()
        storage = self.storage_class(request)
        self.user.message_set.create(message='user message')

        # Test that the message actually contains what we expect.
        self.assertEqual(len(storage), 1)
        self.assertEqual(list(storage)[0].message, 'user message')

    def test_get_legacy(self):
        request = self.get_request()
        storage = self.storage_class(request)
        cookie_storage = self.get_cookie_storage(storage)
        self.user.message_set.create(message='user message')
        set_cookie_data(cookie_storage, ['cookie'])

        # Test that the message actually contains what we expect.
        self.assertEqual(len(storage), 2)
        self.assertEqual(list(storage)[0].message, 'user message')
        self.assertEqual(list(storage)[1], 'cookie')
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.