wheezy.routing / src / wheezy / routing / tests / test_builders.py

""" Unit tests for ``wheezy.routing.builders``.
"""

import unittest

from mocker import Mocker, expect


class TryBuildPlainRouteTestCase(unittest.TestCase):
    """ Test the ``builders.try_build_plain_route``
        builder strategy.
    """

    def test_match(self):
        """ Match plain route strategy.
        """
        from wheezy.routing.builders import try_build_plain_route
        from wheezy.routing.route import PlainRoute

        r = try_build_plain_route(r'abc', False)
        assert isinstance(r, PlainRoute)
        assert not r.kwargs

    def test_match_empty(self):
        """ Match plain route strategy.
        """
        from wheezy.routing.builders import try_build_plain_route
        from wheezy.routing.route import PlainRoute

        r = try_build_plain_route(r'', False)
        assert isinstance(r, PlainRoute)
        assert not r.kwargs

    def test_kwards(self):
        """ Test whenever route is initialized with
            ``kwargs``.
        """
        from wheezy.routing.builders import try_build_plain_route

        r = try_build_plain_route(r'abc', False, {'a': 1})

        self.assertEqual({'a': 1}, r.kwargs)

    def test_no_match(self):
        """ No match for plain route strategy.
        """
        from wheezy.routing.builders import try_build_plain_route

        r = try_build_plain_route(r'abc/{name}', False)

        assert r is None


class TryBuildCurlyRouteTestCase(unittest.TestCase):
    """ Test the ``builders.try_build_curly_route``.
    """

    def test_no_match(self):
        """ Always return an instance of RegexRoute.
        """
        from wheezy.routing.builders import try_build_curly_route

        r = try_build_curly_route(r'abc', False)
        assert r is None

    def test_match(self):
        """ Always return an instance of RegexRoute.
        """
        from wheezy.routing.builders import try_build_curly_route
        from wheezy.routing.route import RegexRoute

        r = try_build_curly_route(r'abc/{n}', False)
        assert isinstance(r, RegexRoute)

    def test_pattern_name(self):
        """ Always return an instance of RegexRoute.
        """
        from wheezy.routing.builders import try_build_curly_route
        from wheezy.routing.route import RegexRoute

        r = try_build_curly_route(r'abc/{n:i}', False)
        assert isinstance(r, RegexRoute)


class TryBuildRegexRouteTestCase(unittest.TestCase):
    """ Test the ``builders.try_build_regex_route``.
    """

    def test_instance(self):
        """ Always return an instance of RegexRoute.
        """
        from wheezy.routing.builders import try_build_regex_route
        from wheezy.routing.route import RegexRoute

        r = try_build_regex_route(r'abc', False)
        assert isinstance(r, RegexRoute)


class BuildRouteTestCase(unittest.TestCase):
    """ Test the ``builders.build_route``
        strategy selection chain of responsibility.
    """

    def test_pattern_is_route(self):
        """ ``pattern`` is an object drived from Route.
        """
        from wheezy.routing.builders import build_route
        from wheezy.routing.route import PlainRoute

        r = PlainRoute('', True)

        self.assertRaises(LookupError,
                          lambda: build_route(r, False, None, None, []))

    def test_found(self):
        """ Sutable route strategy has been found.
        """
        from wheezy.routing.builders import build_route
        from wheezy.routing import config

        r = build_route(r'abc', False, {'a': 1}, None, config.route_builders)

        assert r
        self.assertEqual({'a': 1}, r.kwargs)

    def test_first_matched(self):
        """ First matched strategy is selected.
        """
        from wheezy.routing.builders import build_route

        m = Mocker()
        mock = m.mock
        builders = mock(), mock(), mock(), mock(), mock()
        b1, b2, b3, b4, b5 = builders
        expect(b1(r'abc', False, None, None)).result(None)
        expect(b2(r'abc', False, None, None)).result(None)
        expect(b3(r'abc', False, None, None)).result('x')
        m.replay()

        r = build_route(r'abc', False, None, None, builders)

        self.assertEqual('x', r)
        m.verify()

    def test_not_found(self):
        """ None of available route builders matched
            pattern.
        """
        from wheezy.routing.builders import build_route

        m = Mocker()
        mock = m.mock
        builders = mock(), mock()
        b1, b2 = builders
        expect(b1(r'abc', False, None, None)).result(None)
        expect(b2(r'abc', False, None, None)).result(None)
        m.replay()

        self.assertRaises(LookupError, lambda: build_route(
            r'abc', False, None, None, builders))
        m.verify()


class BuildRouteIntegrationTestCase(unittest.TestCase):
    """ Test integration with ``config.route_builders``.
    """

    def test_match_plain_route(self):
        """ the chain of strategies match plain route.
        """
        from wheezy.routing import config
        from wheezy.routing.builders import build_route
        from wheezy.routing.route import PlainRoute

        r = build_route(r'abc', False, None, None, config.route_builders)

        assert isinstance(r, PlainRoute)

    def test_match_curly_route_intermediate(self):
        """ the chain of strategies match regex route.
        """
        from wheezy.routing import config
        from wheezy.routing.builders import build_route
        from wheezy.routing.route import RegexRoute

        r = build_route(
            r'abc/{id}',
            False,
            None,
            None,
            config.route_builders
        )

        assert isinstance(r, RegexRoute)
        self.assertEquals('^abc/(?P<id>[^/]+)', r.regex.pattern)

    def test_match_curly_route_finishing(self):
        """ the chain of strategies match regex route.
        """
        from wheezy.routing import config
        from wheezy.routing.builders import build_route
        from wheezy.routing.route import RegexRoute

        r = build_route(
            r'abc/{id}',
            True,
            None,
            None,
            config.route_builders
        )

        assert isinstance(r, RegexRoute)
        self.assertEquals('^abc/(?P<id>[^/]+)$', r.regex.pattern)

    def test_match_regex_route(self):
        """ the chain of strategies match regex route.
        """
        from wheezy.routing import config
        from wheezy.routing.builders import build_route
        from wheezy.routing.route import RegexRoute

        r = build_route(
            r'abc/(?P<id>\d+)',
            False,
            None,
            None,
            config.route_builders
        )

        assert isinstance(r, RegexRoute)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.