from __future__ import print_function

# stdblib
import unittest

# pypi
from pyramid import testing
from pyramid.interfaces import IRoutesMapper

# local
# import pyramid_route_7

# ==============================================================================


class TestConfig(unittest.TestCase):
    def setUp(self):
        self.config = config = testing.setUp()
        config.include("pyramid_route_7")
        config.add_route_7_pattern("d4", r"\d\d\d\d")
        config.add_route_7_pattern("d2", r"\d\d")
        config.add_route_7_kvpattern("year", r"\d\d\d\d")
        config.add_route_7_kvpattern("month", r"\d\d")
        config.add_route_7_kvpattern("day", r"\d\d")
        config.add_route_7_kvpattern("user_id", r"\d\d\d")
        # define routes: TOTAL-5
        config.add_route_7("ymd-pattern", "/ymd-pattern/{year|d4}/{month|d2}/{day|d2}")
        config.add_route_7("ymd-kvpattern", "/ymd-kvpattern/{@year}/{@month}/{@day}")
        config.add_route_7("user_profile", "/path/to/user/{@user_id}")
        config.add_route_7(
            "user_profile-subfolder1", "/path/to/user/{@user_id}/subfolder-one"
        )
        config.add_route_7(
            "user_profile-subfolder2", "/path/to/user/{@user_id}/subfolder-two"
        )
        config.add_route_7(
            "user_profile-alt", "/path/to/user-alt/{@user_id}", jsonify=True
        )
        config.add_route_7("items", "/items", paginate=True)  # 2
        config.add_route_7("items_api", "/items/api", paginate=True, jsonify=True)

        # END define routes
        self.context = testing.DummyResource()
        self.request = testing.DummyRequest()

        # https://github.com/Pylons/pyramid/blob/master/tests/test_config/test_routes.py
        #    def _assertRoute(self, config, name, path, num_predicates=0)
        self.mapper = mapper = self.config.registry.getUtility(IRoutesMapper)
        self.routes = routes = mapper.get_routes()
        self.routes_dict = {r.name: r.pattern for r in routes}

    def tearDown(self):
        testing.tearDown()

    def test_setup(self):
        """
        if setup failed, the following would have raised exceptions
          in `_TestHarness.setUp` :
          * `config.add_route_7_pattern`
          * `config.add_route_7_kvpattern`
          * `config.add_route_7`
        just ensure we added 7 routes
          *  8 routes explcity
          * +1 route via jsonify
          * +1 route via paginate
          * +3 routes via jsonify+paginate
        """
        self.assertEqual(13, len(self.routes))

    def test_pattern(self):
        self.assertIn("ymd-pattern", self.routes_dict)
        _route_pattern = self.routes_dict["ymd-pattern"]
        self.assertEqual(
            _route_pattern, r"/ymd-pattern/{year:\d\d\d\d}/{month:\d\d}/{day:\d\d}"
        )

    def test_kvpattern(self):
        self.assertIn("ymd-kvpattern", self.routes_dict)
        _route_pattern = self.routes_dict["ymd-kvpattern"]
        self.assertEqual(
            _route_pattern, r"/ymd-kvpattern/{year:\d\d\d\d}/{month:\d\d}/{day:\d\d}"
        )

        self.assertIn("user_profile", self.routes_dict)
        _route_pattern = self.routes_dict["user_profile"]
        self.assertEqual(_route_pattern, r"/path/to/user/{user_id:\d\d\d}")

        self.assertIn("user_profile-subfolder1", self.routes_dict)
        _route_pattern = self.routes_dict["user_profile-subfolder1"]
        self.assertEqual(
            _route_pattern, r"/path/to/user/{user_id:\d\d\d}/subfolder-one"
        )

        self.assertIn("user_profile-subfolder2", self.routes_dict)
        _route_pattern = self.routes_dict["user_profile-subfolder2"]
        self.assertEqual(
            _route_pattern, r"/path/to/user/{user_id:\d\d\d}/subfolder-two"
        )

        self.assertIn("user_profile-alt", self.routes_dict)
        _route_pattern = self.routes_dict["user_profile-alt"]
        self.assertEqual(_route_pattern, r"/path/to/user-alt/{user_id:\d\d\d}")

        self.assertIn("user_profile-alt|json", self.routes_dict)
        _route_pattern = self.routes_dict["user_profile-alt|json"]
        self.assertEqual(_route_pattern, r"/path/to/user-alt/{user_id:\d\d\d}.json")

        self.assertIn("items", self.routes_dict)
        _route_pattern = self.routes_dict["items"]
        self.assertEqual(_route_pattern, r"/items")

        self.assertIn("items-paginated", self.routes_dict)
        _route_pattern = self.routes_dict["items-paginated"]
        self.assertEqual(_route_pattern, r"/items/{page:\d+}")

        self.assertIn("items_api", self.routes_dict)
        _route_pattern = self.routes_dict["items_api"]
        self.assertEqual(_route_pattern, r"/items/api")

        self.assertIn("items_api|json", self.routes_dict)
        _route_pattern = self.routes_dict["items_api|json"]
        self.assertEqual(_route_pattern, r"/items/api.json")

        self.assertIn("items_api-paginated", self.routes_dict)
        _route_pattern = self.routes_dict["items_api-paginated"]
        self.assertEqual(_route_pattern, r"/items/api/{page:\d+}")

        self.assertIn("items_api-paginated|json", self.routes_dict)
        _route_pattern = self.routes_dict["items_api-paginated|json"]
        self.assertEqual(_route_pattern, r"/items/api/{page:\d+}.json")
