Example #1
0
def test_mock_object():
    m1 = utils.MockObject(foo=1, bar='flam')
    assert m1.foo == 1
    assert m1.bar == 'flam'
    m2 = utils.MockObject(foo=1, bar='flam')
    assert m1 == m2
    assert repr(m1) == "<MockObject foo=1, bar='flam'>"
Example #2
0
def test_create_require():
    require_mod_2 = wu.create_require(lambda r: bool(r.value),
                                      response_builder=lambda r, f, *a, **k:
                                      (r, f, a, k))

    def foo(request, *args, **kwargs):
        return utils.MockObject(args=args, kwargs=kwargs)

    decorated_foo = require_mod_2(foo)

    passing_request = utils.MockObject(value=True)
    response = decorated_foo(passing_request, 1, 2, a=1, b=2)
    assert isinstance(response, utils.MockObject)
    assert response.args == (1, 2)
    assert response.kwargs == {'a': 1, 'b': 2}

    failing_request = utils.MockObject(value=False)
    func, request, args, kwargs = decorated_foo(failing_request,
                                                1,
                                                2,
                                                a=1,
                                                b=2)
    assert func == foo
    assert request == failing_request
    assert args == (1, 2)
    assert kwargs == {'a': 1, 'b': 2}
Example #3
0
def test_do_dispatch():
    url_map = wu.UrlMap()
    url_map.expose('/foo/bar')(lambda r: ("Foo Bar", r))
    url_map.expose('/foo/<path:path_info>')(lambda r: ("Foo Bar Baz", r))
    url_map.expose('/foo/flibble/<path:path_info>')(
        lambda r: ("Foo Flibble Wibble Dibble", r))
    app = utils.MockObject(url_map=url_map)

    req = wz.Request.from_values(path="/foo/bar")
    req.map_adapter = url_map.bind_to_environ(req.environ)
    (res, r) = wu._do_dispatch(app, req)
    assert res == "Foo Bar"
    assert r.environ["SCRIPT_NAME"] == "/foo/bar"
    assert r.environ["PATH_INFO"] == ""

    req = wz.Request.from_values(path="/foo/bar/baz")
    req.map_adapter = url_map.bind_to_environ(req.environ)
    (res, r) = wu._do_dispatch(app, req)
    assert res == "Foo Bar Baz"
    assert r.environ["SCRIPT_NAME"] == "/foo"
    assert r.environ["PATH_INFO"] == "/bar/baz"

    req = wz.Request.from_values(path="/foo/flibble/wibble/dibble")
    req.map_adapter = url_map.bind_to_environ(req.environ)
    (res, r) = wu._do_dispatch(app, req)
    assert res == "Foo Flibble Wibble Dibble"
    assert r.environ["SCRIPT_NAME"] == "/foo/flibble"
    assert r.environ["PATH_INFO"] == "/wibble/dibble"
Example #4
0
def test_Prototype():
    s = utils.MockObject(foo="slanget", bar="sloople")
    f = utils.MockObject(foo="flanget", baz="floople")
    cg = utils.make_prototype(s, f)
    cg.flamble = "wotsit"
    assert cg.foo == s.foo
    assert cg.bar == s.bar
    assert cg.baz == f.baz
    assert not callable(cg)

    c = lambda x: "Beeble Boople Lula"
    cg = utils.make_prototype(s, f, c)
    cg.flamble = "wotsit"
    assert cg.foo == s.foo
    assert cg.bar == s.bar
    assert cg.baz == f.baz
    assert callable(cg)
    assert cg("foo") == "Beeble Boople Lula"
    assert cg.flamble == "wotsit"
Example #5
0
def test_storage():
    def do_test(data):
        s = fixture.EnvWrapper(data)
        assert s.FooData == 'Bar'
        assert s['FooData'] == 'Bar'
        with py.test.raises(fixture.KeyAndAttributeError):
            s.plop
        with py.test.raises(fixture.KeyAndAttributeError):
            s['plop']

    datas = [dict(FooData='Bar'), utils.MockObject(Foo='Bar')]

    for data in datas:
        yield do_test, data
Example #6
0
def test_CascadingEnv():
    with path.create_temp_dir() as d:
        d.child('bar3.jade').write_text("""div Jade Page\n  !=" "+foo_var""")

        env = wu.CascadingEnv(
            wu.LxmlEnv(
                utils.MockObject(bar1=lambda context: wiseguy.html.jade(
                    "div Lxml Page %s" % context['foo_var']))),
            wu.JinjaEnv(
                j2.Environment(loader=j2.DictLoader(
                    dict(bar2="<div>Jinja Page {{foo_var}}</div>",
                         flam="<div>Flam Page {{wangle}}</div>")))),
            wu.JadeEnv(d, dict()))

        expected = "<div>Lxml Page flam</div>"
        result = env.render("bar1", dict(foo_var="flam")).strip()
        assert result == expected
        result = env.get_response("bar1", dict(foo_var="flam"))
        assert result.data.strip() == expected

        expected = "<div>Jinja Page flom</div>"
        result = env.render("bar2", dict(foo_var="flom")).strip()
        assert result == expected
        result = env.get_response("bar2",
                                  dict(foo_var="flom"),
                                  mimetype="blah")
        assert result.data.strip() == expected
        assert result.mimetype == "blah"

        expected = "<div>Jade Page flim</div>"
        result = env.render("bar3", dict(foo_var="flim")).strip()
        assert result == expected
        result = env.get_response("bar3", dict(foo_var="flim"))
        assert result.data.strip() == expected

        with raises(wu.TemplateNotFound):
            result = env.render("flib", dict())

        with raises(wu.TemplateNotFound):
            result = env.get_response("flib", dict())

        env.update_globals(dict(wangle="wotsit"))
        html = env.render("flam", dict()).strip()
        assert html == "<div>Flam Page wotsit</div>"
Example #7
0
def test_LxmlEnv():
    env = wu.LxmlEnv(
        utils.MockObject(
            bar=lambda context: wiseguy.html.jade("div Foo Page %s" % context[
                'foo_var']),
            flam=lambda context: wiseguy.html.jade("div Flam Page %s" %
                                                   context['wangle']),
        ))

    html = env.render("bar", dict(foo_var="flangit")).strip()
    assert html == "<div>Foo Page flangit</div>"

    response = env.get_response("bar", dict(foo_var="flibble"), "text/html")
    assert response.data.strip() == "<div>Foo Page flibble</div>"

    with raises(wu.TemplateNotFound):
        html = env.render("foo", dict(blim="blam")).strip()

    env.update_globals(dict(wangle="wotsit"))
    html = env.render("flam", dict()).strip()
    assert html == "<div>Flam Page wotsit</div>"
Example #8
0
 def foo(request, *args, **kwargs):
     return utils.MockObject(args=args, kwargs=kwargs)
Example #9
0
 class MockRequest(object):
     method = "POST"
     form = utils.MockObject(to_dict=lambda flat: dict(
         flamble=[1, 2, 3], flooble=["flooble"]))
Example #10
0
 class MockRequest(object):
     method = "POST"
     form = utils.MockObject(to_dict=lambda flat: dict(do_raise=True))
Example #11
0
 class MockRequest(object):
     method = "POST"
     form = utils.MockObject(to_dict=lambda flat: dict(foo="blam"))