test_accessors.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » nevow » test » 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 » Web Frameworks » Nevow 
Nevow » Nevow 0.10.0 » nevow » test » test_accessors.py
# Copyright (c) 2004 Divmod.
# See LICENSE for details.


from nevow.accessors import convertToData,NoAccessor
from nevow import context
from nevow import inevow
from nevow.stan import directive
from nevow.tags import invisible,slot
from nevow.flat import precompile
from nevow import rend
from nevow.testutil import TestCase


class Base(TestCase):
    def makeContext(self, *args):
        ctx = context.WovenContext()
        ctx.remember(convertToData(self.remember, ctx), inevow.IData)
        for a in args:
            ctx = context.WovenContext(ctx, invisible())
            ctx.remember(convertToData(a, ctx), inevow.IData)
        return ctx


class TestBasics(Base):
    remember = None

    def test_dict_directive(self):
        d = directive('one')
        ctx = self.makeContext({'one': 1, 'two': 2})
        self.assertEquals(1, convertToData(d, ctx))
        self.assertRaises(KeyError, convertToData, directive('asdfasdf'), ctx)

    def test_list_directive(self):
        d= directive('2')
        ctx = self.makeContext([0, 1, 42, 3, 4])
        self.assertEquals(42, convertToData(d, ctx))
        self.assertRaises(IndexError, convertToData, directive('9999'), ctx)
        self.assertRaises(ValueError, convertToData, directive('HAHAHAHA'), ctx)

    def test_function_accessor(self):
        def foo(context, data):
            return 42
        ctx = self.makeContext()
        self.assertEquals(42, convertToData(foo, ctx))
        d = directive('this wont work')
        ctx2 = self.makeContext(foo)
        self.assertRaises(NoAccessor, convertToData, d, ctx2)


thefoo = "foo"

class Factory(rend.DataFactory):
    original = None
    
    def data_foo(self, context, data):
        return thefoo

    def data_dict(self, context, data):
        return {"one": 1, "two": 2}

    def data_list(self, context, data):
        return [1, 99, 43]

    def data_factory(self, context, data):
        return [self]


f = Factory()


class TestThroughDirective(Base):
    remember = f

    def test_simple(self):
        d = directive('foo')
        ctx = self.makeContext()
        self.assertEquals(thefoo, convertToData(d, ctx))

    def test_dict_through_directive(self):
        d1, d2 = directive('dict'), directive('one')
        ctx = self.makeContext(d1)
        self.assertEquals(1, convertToData(d2, ctx))

    def test_list_through_directive(self):
        d1, d2 = directive('list'), directive('1')
        ctx = self.makeContext(d1)
        self.assertEquals(99, convertToData(d2, ctx))

    def test_roundAndRound(self):
        ctx = self.makeContext(
            directive('factory'), directive('0'), directive('factory')
        )
        self.assertEquals(f, convertToData(directive('0'), ctx))
        
    def test_missing(self):
        self.assertRaises(rend.DataNotFoundError, self.makeContext, directive('missing'))


class APage(rend.Page):
    def data_foo(self, ctx, data):
        return "foo"


class TestPageData(Base):
    def test_1_noneOriginal(self):
        data = None
        ctx = context.WovenContext()
        ctx.remember(APage(data), inevow.IData)
        self.assertEquals(data, convertToData(ctx.locate(inevow.IData), ctx))
        self.assertEquals('foo', convertToData(directive('foo'), ctx))

    def test_2_dictOriginal(self):
        data = {'hello': 'world'}
        ctx = context.WovenContext()
        ctx.remember(APage(data), inevow.IData)
        # IGettable should return our dictionary
        self.assertEquals(data, convertToData(ctx.locate(inevow.IData), ctx))
        # IContainer on the *Page*, not the dictionary, should work
        self.assertEquals('foo', convertToData(directive('foo'), ctx))
        # IContainer on the Page should delegate to IContainer(self.original) if no data_ method
        self.assertEquals('world', convertToData(directive('hello'), ctx))



class SlotAccessorTestCase(TestCase):
    def _accessorTest(self, obj):
        ctx = context.WovenContext()
        ctx.fillSlots('slotname', 'foo')
        data = inevow.IGettable(obj).get(ctx)
        self.assertEqual(data, 'foo')


    def test_slotAccessor(self):
        return self._accessorTest(slot('slotname'))


    def test_precompiledSlotAccessor(self):
        return self._accessorTest(precompile(slot('slotname'))[0])
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.