Esempio n. 1
0
 def setUp(self):
     self.rf = RequestFactory()
     self.mw = RequestTreeMiddleware()
     MenuItem.load_bulk(get_bulk_data())
Esempio n. 2
0
class RequestTreeMiddlewareTestCase(TestCaseWithDB):
    def setUp(self):
        self.rf = RequestFactory()
        self.mw = RequestTreeMiddleware()
        MenuItem.load_bulk(get_bulk_data())

    @override_settings(MEDIA_URL='/a/b/c/', STATIC_URL='/b/')
    def test_is_ignored(self):
        req = self.rf.get('/a/b/c/d.jpg')
        self.assertIsNone(self.mw.process_request(req))

    def test_menuitem_is_published(self):
        MenuItem.add_root(uri='/a/b/', title='found',
                          site=Site.objects.get_current(),
                          is_published=True)
        req = self.rf.get('/a/b/')
        self.mw.process_request(req)
        with self.assertNumQueries(1):
            self.assertEqual(req.menuitem.uri, '/a/b/')
            self.assertEqual(req.menuitem.title, 'found')

    def test_menuitem_is_none_because_not_published(self):
        MenuItem.add_root(uri='/a/b/', title='found',
                          site=Site.objects.get_current(),
                          is_published=False)
        req = self.rf.get('/a/b/')
        self.mw.process_request(req)
        with self.assertNumQueries(1):
            with self.assertRaises(AttributeError):
                self.assertIsNone(req.menuitem.uri)

    def test_descendants(self):
        req = self.rf.get('/a/')
        self.mw.process_request(req)
        descendants = [x for x in req.descendants]
        urls = [x.uri for x in descendants]
        self.assertEqual(urls, ['/a/b/c/', '/d/', '/e', '/HI', '/x/'])

    def test_ancestors(self):
        req = self.rf.get('/a/b/c/')
        self.mw.process_request(req)
        ancestors = [x for x in req.ancestors]
        urls = [x.uri for x in ancestors]
        self.assertEqual(urls, ['/a/'])

    def test_siblings(self):
        req = self.rf.get('/a/')
        self.mw.process_request(req)
        siblings = [x for x in req.siblings]
        urls = [x.uri for x in siblings]
        self.assertEqual(urls, ['/', '/a/', '/sup', '/yo'])

    def test_children(self):
        req = self.rf.get('/a/')
        self.mw.process_request(req)
        children = [x for x in req.children]
        urls = [x.uri for x in children]
        self.assertEqual(urls, ['/a/b/c/', '/d/', '/e', '/x/'])