#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Unit tests."""
import os
import unittest
from copy import copy

from webui.app import create_app


class TestRoutes(unittest.TestCase):
    """Test routes."""

    ignore_routes = ('/static/<path:filename>',)
    ignore_end_patterns = ('>',)

    def setUp(self):
        """Set up: Put Flask app in test mode."""
        app = create_app()
        self.initial_app = copy(app)
        app.testing = True
        self.app = app.test_client()

    @staticmethod
    def valid_route(route):
        """Validate route.

        Args:
            route (str): Route url pattern.

        Returns:
            bool: True if valid, else False.
        """
        if route in TestRoutes.ignore_routes \
                or route.endswith(TestRoutes.ignore_end_patterns):
            return False
        return True

    def test_routes(self):
        """Smoke test routes to ensure no runtime errors.."""
        routes = [route.rule for route in self.initial_app.url_map.iter_rules()
                  if self.valid_route(route.rule)]
        for route in routes:
            self.app.get(route)


if __name__ == '__main__':
    from test.utils.doctest_unittest_runner import doctest_unittest_runner
    TEST_DIR = os.path.dirname(os.path.realpath(__file__)) + '/'
    doctest_unittest_runner(test_dir=TEST_DIR, relative_path_to_root='../',
                            package_names=['webui', 'test'])

[(50, 'test.utils.doctest_unittest_runner.doctest_unittest_runner', 'doctest_unittest_runner', False, 'from test.utils.doctest_unittest_runner import doctest_unittest_runner\n'), (19, 'webui.app.create_app', 'create_app', False, 'from webui.app import create_app\n'), (20, 'copy.copy', 'copy', False, 'from copy import copy\n'), (49, 'os.path.realpath', 'os.path.realpath', False, 'import os\n')]