Пример #1
0
    def test_round_trip(self):
        test = twc.CompoundWidget(id='a', children=[
            twc.DisplayOnlyWidget(child=
                twc.RepeatingWidget(id='q', child=twc.Widget)
            ),
            twc.CompoundWidget(id='cc', children=[
                twc.Widget(id='d'),
                twc.Widget(id='e'),
            ])
        ])

        widgets = [
            test.children[0].child.rwbc[0],
            test.children[0].child.rwbc[1],
            test.children.cc.children.d,
            test.children.cc.children.e,
        ]

        data = dict((w.compound_id, 'test%d' % i) for i,w in enumerate(widgets))
        testapi.request(1)
        vdata = test.validate(data)

        test = twc.core.request_local()['validated_widget']
        widgets = [
            test.children[0].child.children[0],
            test.children[0].child.children[1],
            test.children.cc.children.d,
            test.children.cc.children.e,
        ]

        for i,w in enumerate(widgets):
            eq_(w.value, 'test%d' % i)
Пример #2
0
 def test_rw_corrupt(self):
     testapi.request(1)
     try:
         repeating_widget.validate({"a": {"a": "b"}})
         assert False
     except twc.ValidationError:
         pass
Пример #3
0
    def testTGStyleController(self):
        """ Test turbogears style dispatch """
        from tw2.core.middleware import ControllersApp, TwMiddleware
        from tw2.core.compat import TGStyleController

        controller_response = Response("CONTROLLER")

        class WidgetMock(TGStyleController):
            id = "fake"

            class Controller(object):
                def foobar(self, request):
                    return controller_response

        mock = WidgetMock()
        mw = TwMiddleware(None, controller_prefix="goo")
        testapi.request(1, mw)
        ca = ControllersApp()

        ca.register(mock)
        res = ca(Request.blank("/%s/%s/foobar" % (mw.config.controller_prefix, mock.id)))
        self.assert_(res.status_int == 200, res.status_int)
        self.assert_(res.body == controller_response.body, res.body)

        res = ca(Request.blank("/%s/404" % mw.config.controller_prefix))
        self.assert_(res.status_int == 404, res.status_int)
        res = ca(Request.blank("%s/404" % mw.config.controller_prefix))
        self.assert_(res.status_int == 404, res.status_int)
Пример #4
0
 def test_rw_corrupt(self):
     testapi.request(1)
     try:
         repeating_widget.validate({'a':{'a':'b'}})
         assert(False)
     except twc.ValidationError:
         pass
Пример #5
0
 def test_compound_corrupt(self):
     testapi.request(1)
     try:
         compound_widget.validate({'a':[]})
         assert(False)
     except twc.ValidationError:
         pass
Пример #6
0
 def test_rw_child_fail(self):
     testapi.request(1)
     try:
         repeating_widget.validate({'a':['test', '']})
         assert(False)
     except twc.ValidationError, e:
         pass
Пример #7
0
    def testTGStyleController(self):
        """ Test turbogears style dispatch """
        from tw2.core.middleware import ControllersApp, TwMiddleware
        from tw2.core.compat import TGStyleController
        controller_response = Response("CONTROLLER")

        class WidgetMock(TGStyleController):
            id = 'fake'

            class Controller(object):
                def foobar(self, request):
                    return controller_response

        mock = WidgetMock()
        mw = TwMiddleware(None, controller_prefix="goo")
        testapi.request(1, mw)
        ca = ControllersApp()

        ca.register(mock)
        res = ca(
            Request.blank("/%s/%s/foobar" %
                          (mw.config.controller_prefix, mock.id)))
        self.assert_(res.status_int == 200, res.status_int)
        self.assert_(res.body == controller_response.body, res.body)

        res = ca(Request.blank("/%s/404" % mw.config.controller_prefix))
        self.assert_(res.status_int == 404, res.status_int)
        res = ca(Request.blank("%s/404" % mw.config.controller_prefix))
        self.assert_(res.status_int == 404, res.status_int)
Пример #8
0
 def test_mime_type(self):
     testapi.request(1, mw)
     wa = twc.JSLink(modname='tw2.core', filename='test_templates/simple_genshi.html').req()
     wa.prepare()
     resp = tst_mw.get(wa.link)
     assert(resp.content_type == 'text/html')
     assert(resp.charset == 'UTF-8')
Пример #9
0
    def testControllerAppWithId(self):
        """
        controllerapp should dispatch to an object having id, and a
        request method taking a webob request based on path_info of
        request.
        """
        from tw2.core.middleware import ControllersApp, TwMiddleware
        controller_response = Response("CONTROLLER")

        class WidgetMock(object):
            def __init__(self):
                self.id = "fake"

            def request(self, request):
                return controller_response

        mock = WidgetMock()
        mw = TwMiddleware(None, controller_prefix="goo")
        testapi.request(1, mw)
        ca = ControllersApp()

        ca.register(mock)
        res = ca(
            Request.blank("/%s/%s" % (mw.config.controller_prefix, mock.id)))
        self.assert_(res.status_int == 200, res.status_int)
        self.assert_(res.body == controller_response.body, res.body)

        res = ca(Request.blank("/%s/404" % mw.config.controller_prefix))
        self.assert_(res.status_int == 404, res.status_int)
        res = ca(Request.blank("%s/404" % mw.config.controller_prefix))
        self.assert_(res.status_int == 404, res.status_int)
Пример #10
0
 def test_compound_corrupt(self):
     testapi.request(1)
     try:
         compound_widget.validate({'a': []})
         assert (False)
     except twc.ValidationError:
         pass
Пример #11
0
 def test_rw_propagate(self):
     test = twc.RepeatingWidget(child=Child).req()
     testapi.request(1)
     test.value = ['a', 'b', 'c']
     test.prepare()
     assert(len(test.children) == 3)
     assert([w.value for w in test.children] == ['a', 'b', 'c'])
Пример #12
0
 def test_rw_child_fail(self):
     testapi.request(1)
     try:
         repeating_widget.validate({'a': ['test', '']})
         assert (False)
     except twc.ValidationError, e:
         pass
Пример #13
0
 def test_rw_corrupt(self):
     testapi.request(1)
     try:
         repeating_widget.validate({'a': {'a': 'b'}})
         assert (False)
     except twc.ValidationError:
         pass
Пример #14
0
 def _check_render(self, template, data, expected, engine=None):
     if engine:
         mw = twc.make_middleware(None, preferred_rendering_engines=[engine])
         testapi.request(1, mw)
     out = twc.template.EngineManager().render(template, 'string', data)
     assert(isinstance(out, unicode))
     assert out == expected, out
Пример #15
0
    def test_round_trip(self):
        test = twc.CompoundWidget(
            id='a',
            children=[
                twc.DisplayOnlyWidget(
                    child=twc.RepeatingWidget(id='q', child=twc.Widget)),
                twc.CompoundWidget(id='cc',
                                   children=[
                                       twc.Widget(id='d'),
                                       twc.Widget(id='e'),
                                   ])
            ])

        widgets = [
            test.children[0].child.rwbc[0],
            test.children[0].child.rwbc[1],
            test.children.cc.children.d,
            test.children.cc.children.e,
        ]

        data = dict(
            (w.compound_id, 'test%d' % i) for i, w in enumerate(widgets))
        testapi.request(1)
        vdata = test.validate(data)

        test = twc.core.request_local()['validated_widget']
        widgets = [
            test.children[0].child.children[0],
            test.children[0].child.children[1],
            test.children.cc.children.d,
            test.children.cc.children.e,
        ]

        for i, w in enumerate(widgets):
            eq_(w.value, 'test%d' % i)
Пример #16
0
    def testControllerAppWithId(self):
        """
        controllerapp should dispatch to an object having id, and a
        request method taking a webob request based on path_info of
        request.
        """
        from tw2.core.middleware import ControllersApp, TwMiddleware

        controller_response = Response("CONTROLLER")

        class WidgetMock(object):
            def __init__(self):
                self.id = "fake"

            def request(self, request):
                return controller_response

        mock = WidgetMock()
        mw = TwMiddleware(None, controller_prefix="goo")
        testapi.request(1, mw)
        ca = ControllersApp()

        ca.register(mock)
        res = ca(Request.blank("/%s/%s" % (mw.config.controller_prefix, mock.id)))
        self.assert_(res.status_int == 200, res.status_int)
        self.assert_(res.body == controller_response.body, res.body)

        res = ca(Request.blank("/%s/404" % mw.config.controller_prefix))
        self.assert_(res.status_int == 404, res.status_int)
        res = ca(Request.blank("%s/404" % mw.config.controller_prefix))
        self.assert_(res.status_int == 404, res.status_int)
Пример #17
0
 def test_rw_propagate(self):
     test = twc.RepeatingWidget(child=twc.Widget).req()
     testapi.request(1)
     test.value = ['a', 'b', 'c']
     test.prepare()
     assert(len(test.children) == 3)
     assert([w.value for w in test.children] == ['a', 'b', 'c'])
Пример #18
0
    def test_round_trip(self):
        test = twc.CompoundWidget(
            id="a",
            children=[
                twc.DisplayOnlyWidget(child=twc.RepeatingWidget(id="q", child=twc.Widget)),
                twc.CompoundWidget(id="cc", children=[twc.Widget(id="d"), twc.Widget(id="e")]),
            ],
        )

        widgets = [
            test.children[0].child.rwbc[0],
            test.children[0].child.rwbc[1],
            test.children.cc.children.d,
            test.children.cc.children.e,
        ]

        data = dict((w.compound_id, "test%d" % i) for i, w in enumerate(widgets))
        testapi.request(1)
        vdata = test.validate(data)

        test = twc.core.request_local()["validated_widget"]
        widgets = [
            test.children[0].child.children[0],
            test.children[0].child.children[1],
            test.children.cc.children.d,
            test.children.cc.children.e,
        ]

        for i, w in enumerate(widgets):
            eq_(w.value, "test%d" % i)
Пример #19
0
 def test_rw_propagate(self):
     test = twc.RepeatingWidget(child=Child).req()
     testapi.request(1)
     test.value = ["a", "b", "c"]
     test.prepare()
     assert len(test.children) == 3
     assert [w.value for w in test.children] == ["a", "b", "c"]
Пример #20
0
 def test_link_reg(self):
     testapi.request(1, mw)
     wa = twc.JSLink(modname='tw2.core',
                     filename='test_templates/simple_mako.mak').req()
     wa.prepare()
     assert (
         wa.link == '/resources/tw2.core/test_templates/simple_mako.mak')
     tst_mw.get(wa.link)
Пример #21
0
 def test_rwb(self):
     test = twc.RepeatingWidget(child=Child(template=None)).req()
     testapi.request(1)
     test.value = ["a", "b", "c"]
     test.prepare()
     for i in range(4):
         assert test.children[i].repetition == i
     assert test.children[0] is not test.children[1]
Пример #22
0
 def test_mime_type(self):
     testapi.request(1, mw)
     wa = twc.JSLink(modname='tw2.core',
                     filename='test_templates/simple_genshi.html').req()
     wa.prepare()
     resp = tst_mw.get(wa.link)
     assert (resp.content_type == 'text/html')
     assert (resp.charset == 'UTF-8')
Пример #23
0
 def test_rwb(self):
     test = twc.RepeatingWidget(child=Child(template=None)).req()
     testapi.request(1)
     test.value = ['a', 'b', 'c']
     test.prepare()
     for i in range(4):
         assert(test.children[i].repetition == i)
     assert(test.children[0] is not test.children[1])
Пример #24
0
 def test_auto_unflatten(self):
     test = twc.CompoundWidget(
         id='a',
         children=[
             twc.Widget(id='b', validator=twc.Validator(required=True)),
         ])
     testapi.request(1)
     eq_(test.validate({'a:b': '10'}), {'b': '10'})
Пример #25
0
 def test_rw_pass(self):
     testapi.request(1)
     rep = repeating_widget.req()
     inp = ['test', 'test2']
     out = rep._validate(inp)
     assert (inp == out)
     assert (rep.children[0].value == 'test')
     assert (rep.children[1].value == 'test2')
Пример #26
0
 def test_compound_pass(self):
     testapi.request(1)
     inp = {'a': {'b': 'test', 'c': 'test2'}}
     out = compound_widget.validate(inp)
     eq_(out, inp['a'])
     cw = twc.core.request_local()['validated_widget']
     assert (cw.children.b.value == 'test')
     assert (cw.children.c.value == 'test2')
Пример #27
0
 def test_rw_pass(self):
     testapi.request(1)
     rep = repeating_widget.req()
     inp = ["test", "test2"]
     out = rep._validate(inp)
     assert inp == out
     assert rep.children[0].value == "test"
     assert rep.children[1].value == "test2"
Пример #28
0
 def test_compound_keyed_children(self):
     testapi.request(1)
     inp = {'a': {'x':'test', 'y':'test2'}}
     try:
         compound_keyed_widget.validate(inp)
         assert False
     except twc.ValidationError, e:
         pass
Пример #29
0
 def test_rw_pass(self):
     testapi.request(1)
     rep = repeating_widget.req()
     inp = ['test', 'test2']
     out = rep._validate(inp)
     assert(inp == out)
     assert(rep.children[0].value == 'test')
     assert(rep.children[1].value == 'test2')
Пример #30
0
 def test_rwb(self):
     test = twc.RepeatingWidget(child=twc.Widget).req()
     testapi.request(1)
     test.value = ['a', 'b', 'c']
     test.prepare()
     for i in range(4):
         assert(test.children[i].repetition == i)
     assert(test.children[0] is not test.children[1])
Пример #31
0
 def test_compound_pass(self):
     testapi.request(1)
     inp = {'a': {'b':'test', 'c':'test2'}}
     out = compound_widget.validate(inp)
     eq_(out, inp['a'])
     cw = twc.core.request_local()['validated_widget']
     assert(cw.children.b.value == 'test')
     assert(cw.children.c.value == 'test2')
Пример #32
0
 def test_compound_keyed_children(self):
     testapi.request(1)
     inp = {'a': {'x': 'test', 'y': 'test2'}}
     try:
         compound_keyed_widget.validate(inp)
         assert False
     except twc.ValidationError, e:
         pass
Пример #33
0
 def test_compound_pass(self):
     testapi.request(1)
     inp = {"a": {"b": "test", "c": "test2"}}
     out = compound_widget.validate(inp)
     eq_(out, inp["a"])
     cw = twc.core.request_local()["validated_widget"]
     assert cw.children.b.value == "test"
     assert cw.children.c.value == "test2"
Пример #34
0
 def test_mw_resourcesapp(self):
     testapi.request(1)
     mw.resources.register('tw2.core', 'test_templates/simple_genshi.html')
     fcont = open(
         os.path.join(os.path.dirname(twc.__file__),
                      'test_templates/simple_genshi.html')).read()
     assert (
         tst_mw.get('/resources/tw2.core/test_templates/simple_genshi.html'
                    ).body == fcont)
Пример #35
0
 def test_display_only_widget(self):
     test = twc.DisplayOnlyWidget(child=compound_widget)
     testapi.request(1)
     inp = {'a': {'b':'test', 'c':'test2'}}
     out = test.validate(inp)
     assert(out == inp['a'])
     test = twc.core.request_local()['validated_widget']
     assert(test.child.children.b.value == 'test')
     assert(test.child.children.c.value == 'test2')
Пример #36
0
    def _check_render(self, template, data, expected, engine=None):
        if engine:
            mw = twc.make_middleware(None, preferred_rendering_engines=[engine])
            testapi.request(1, mw)
            twc.util.flush_memoization()

        out = twc.templating.render(template, "string", data)
        assert isinstance(out, six.text_type)
        eq_(out, expected)
Пример #37
0
 def test_display_only_widget(self):
     test = twc.DisplayOnlyWidget(child=compound_widget)
     testapi.request(1)
     inp = {'a': {'b': 'test', 'c': 'test2'}}
     out = test.validate(inp)
     assert (out == inp['a'])
     test = twc.core.request_local()['validated_widget']
     assert (test.child.children.b.value == 'test')
     assert (test.child.children.c.value == 'test2')
Пример #38
0
 def test_prepare_validate(self):
     class MyValidator(twc.Validator):
         def from_python(self, value, state=None):
             return value.upper()
     test = twc.Widget(id='a', template='b', validator=MyValidator()).req()
     testapi.request(1)
     test.value = 'fred'
     test.prepare()
     assert(test.value == 'FRED')
Пример #39
0
 def test_display_only_widget(self):
     test = twc.DisplayOnlyWidget(child=compound_widget)
     testapi.request(1)
     inp = {"a": {"b": "test", "c": "test2"}}
     out = test.validate(inp)
     assert out == inp["a"]
     test = twc.core.request_local()["validated_widget"]
     assert test.child.children.b.value == "test"
     assert test.child.children.c.value == "test2"
Пример #40
0
 def test_rw_child_fail(self):
     testapi.request(1)
     try:
         repeating_widget.validate({"a": ["test", ""]})
         assert False
     except twc.ValidationError as e:
         pass
     rw = twc.core.request_local()["validated_widget"]
     assert rw.children[0].value == "test"
     assert "Enter a value" == rw.children[1].error_msg
Пример #41
0
    def _check_render(self, template, data, expected, engine=None):
        if engine:
            mw = twc.make_middleware(None,
                                     preferred_rendering_engines=[engine])
            testapi.request(1, mw)
            twc.util.flush_memoization()

        out = twc.templating.render(template, 'string', data)
        assert (isinstance(out, six.text_type))
        eq_(out, expected)
Пример #42
0
    def test_prepare_validate(self):
        class MyValidator(twc.Validator):
            def from_python(self, value, state=None):
                return value.upper()

        test = twc.Widget(id='a', template='b', validator=MyValidator()).req()
        testapi.request(1)
        test.value = 'fred'
        test.prepare()
        assert (test.value == 'FRED')
Пример #43
0
 def test_compound_child_fail(self):
     testapi.request(1)
     try:
         compound_widget.validate({"a": {"b": "test"}})
         assert False
     except twc.ValidationError:
         pass
     cw = twc.core.request_local()["validated_widget"]
     assert cw.children.b.value == "test"
     assert "Enter a value" == cw.children.c.error_msg
Пример #44
0
    def test_prepare_validate(self):
        class MyValidator(twc.Validator):
            def from_python(self, value, state=None):
                return value.upper()

        test = twc.Widget(id="a", template="b", validator=MyValidator()).req()
        testapi.request(1)
        test.value = "fred"
        test.prepare()
        assert test.value == "FRED"
Пример #45
0
 def test_rw_child_fail(self):
     testapi.request(1)
     try:
         repeating_widget.validate({'a': ['test', '']})
         assert (False)
     except twc.ValidationError as e:
         pass
     rw = twc.core.request_local()['validated_widget']
     assert (rw.children[0].value == 'test')
     assert ('Enter a value' == rw.children[1].error_msg)
Пример #46
0
 def test_display_only(self):
     a = Child(id='a')
     test = twc.DisplayOnlyWidget(child=a, template='xyz', id_suffix='test')
     assert(a.parent is None)
     assert(test.child.parent.template == test.template)
     testapi.request(1)
     test = test.req()
     test.value = 10
     test.prepare()
     assert(test.child.value == 10)
Пример #47
0
 def test_rw_child_fail(self):
     testapi.request(1)
     try:
         repeating_widget.validate({'a':['test', '']})
         assert(False)
     except twc.ValidationError as e:
         pass
     rw = twc.core.request_local()['validated_widget']
     assert(rw.children[0].value == 'test')
     assert('Enter a value' == rw.children[1].error_msg)
Пример #48
0
 def test_compound_child_fail(self):
     testapi.request(1)
     try:
         compound_widget.validate({'a': {'b':'test'}})
         assert(False)
     except twc.ValidationError:
         pass
     cw = twc.core.request_local()['validated_widget']
     assert(cw.children.b.value == 'test')
     assert('Enter a value' == cw.children.c.error_msg)
Пример #49
0
 def test_compound_child_fail(self):
     testapi.request(1)
     try:
         compound_widget.validate({'a': {'b': 'test'}})
         assert (False)
     except twc.ValidationError:
         pass
     cw = twc.core.request_local()['validated_widget']
     assert (cw.children.b.value == 'test')
     assert ('Enter a value' == cw.children.c.error_msg)
Пример #50
0
 def test_display_only(self):
     a = Child(id="a")
     test = twc.DisplayOnlyWidget(child=a, template="xyz", id_suffix="test")
     assert a.parent is None
     assert test.child.parent.template == test.template
     testapi.request(1)
     test = test.req()
     test.value = 10
     test.prepare()
     assert test.child.value == 10
Пример #51
0
 def test_display_only(self):
     a = twc.Widget(id='a')
     test = twc.DisplayOnlyWidget(child=a, template='xyz')
     assert(a.parent is None)
     assert(test.child.parent.template == test.template)
     testapi.request(1)
     test = test.req()
     test.value = 10
     test.prepare()
     assert(test.child.value == 10)
Пример #52
0
 def test_res_collection(self):
     rl = testapi.request(1, mw)
     wa = TestWidget(id='a')
     wb = TestWidget(id='b', resources=[js, css])
     wa.display()
     rl = twc.core.request_local()
     assert (len(rl.get('resources', [])) == 0)
     wb.display()
     for r in rl['resources']:
         assert (any(isinstance(r, b) for b in [js, css]))
     rl = testapi.request(2)
     r = rl.get('resources', [])
     assert len(r) == 0, r
Пример #53
0
    def testGetLinkID(self):
        from tw2.core.middleware import TwMiddleware
        mw = TwMiddleware(None)
        testapi.request(1, mw)

        class CWidget(wd.Widget):
            id = "foo"

            @classmethod
            def request(cls, req):
                pass

        self.assert_(CWidget.get_link())
Пример #54
0
 def test_auto_select_unavailable_engine_not_strict(self):
     engine = 'mako'
     mw = twc.make_middleware(
         None,
         preferred_rendering_engines=[engine],
         strict_engine_selection=False,
     )
     testapi.request(501, mw)
     self._check_render(
         'tw2.core.test_templates.simple_genshi',
         {'test': 'blah!'},
         '<p>TEST blah!</p>',
     )
Пример #55
0
    def test_compound_keyed_children(self):
        testapi.request(1)
        inp = {'a': {'x': 'test', 'y': 'test2'}}
        try:
            compound_keyed_widget.validate(inp)
            assert False
        except twc.ValidationError as e:
            pass

        cw = twc.core.request_local()['validated_widget']
        error_msg = ("is not a valid OpenId"
                     if formencode else "Must be a valid email address")
        assert error_msg in cw.children.c.error_msg
Пример #56
0
    def testAddCall(self):
        class T(wd.Widget):
            test = twc.Param('blah', default='hello')
            template = 'mako:tw2.core.test_templates.simple_mako'

        i = T(id="foo").req()
        jscall = ["somefunc", "bodybottom"]
        i.add_call(jscall[0], jscall[1])
        self.assert_(jscall in i._js_calls)
        testapi.request(1)
        twc.core.request_local()['middleware'] = twc.make_middleware(
            None, params_as_vars=True)
        res = i.display(displays_on="string")
        self.assert_(res)
        self.assert_(i.resources)
Пример #57
0
 def test_params_as_vars(self):
     import mako
     class MyTest(twc.Widget):
         template = 'mako:tw2.core.test_templates.simple_mako'
         test= twc.Param('blah', default='hello')
     testapi.request(1)
     twc.core.request_local()['middleware'] = twc.make_middleware(None, params_as_vars=True)
     MyTest.display()
     twc.core.request_local()['middleware'] = twc.make_middleware(None, params_as_vars=False)
     try:
         MyTest.display()
         assert(False)
     except NameError:
         # this will raise a name error because "Undefined"
         # is found (not a string)
         pass
Пример #58
0
 def test_no_inject_head(self):
     rl = testapi.request(1, mw)
     js.req(no_inject=True).prepare()
     out = twc.inject_resources(html)
     assert eq_xhtml(
         out,
         '<html><head><title>a</title></head><body>hello</body></html>')
Пример #59
0
 def test_inject_css(self):
     rl = testapi.request(1, mw)
     csssrc.inject()
     out = twc.inject_resources(html)
     assert eq_xhtml(
         out,
         '<html><head><style type="text/css">.bob { font-weight: bold; }</style>\
         <title>a</title></head><body>hello</body></html>')
Пример #60
0
 def test_inject_body(self):
     rl = testapi.request(1, mw)
     jssrc.inject()
     out = twc.inject_resources(html)
     assert eq_xhtml(
         out,
         '<html><head><title>a</title></head><body>hello<script type="text/javascript">bob</script></body></html>'
     )