<commit_before>from unittest import TestCase, skipIf
import mock

try:
    import jinja2
except ImportError:
    jinja2 = None

from minion import core, assets
from minion.compat import iteritems
from minion.request import Manager, Request, Response
from minion.routing import Router, SimpleMapper


class TestApplication(TestCase):
    def assertSubdict(self, subdict, of):
        """
        Assert that the given thing is a subdict of the given dict.

        """

        subdict = dict(subdict)
        matches = {k : v for k, v in iteritems(of) if k in subdict}
        self.assertEqual(subdict, matches)

    def test_it_delegates_routes_to_the_router(self):
        router = mock.Mock(spec=SimpleMapper())
        application = core.Application(router=router)

        fn = mock.Mock()
        application.route("/foo/bar", baz=2)(fn)

        router.add.assert_called_once_with("/foo/bar", fn, baz=2)

    def test_binds_to_its_bin(self):
        app = core.Application()
        self.assertSubdict(
            [
                ("app", app),
                ("config", app.config),
                ("manager", app.manager),
                ("router", app.router),
            ],
            app.bin.globals,
        )

    def test_it_can_bind_to_other_bins(self):
        app = core.Application()
        bin = assets.Bin(manager=app.manager)
        app.bind_bin(bin)
        self.assertSubdict(
            [
                ("app", app),
                ("config", app.config),
                ("manager", app.manager),
                ("router", app.router),
            ],
            bin.globals,
        )

    @skipIf(jinja2 is None, "jinja2 not found")
    def test_it_can_bind_to_jinja_environments(self):
        environment = jinja2.Environment()
        app = core.Application(jinja=environment)
        self.assertEqual(app.bin.globals["jinja"], environment)
        self.assertSubdict(
            [
                ("app", app),
                ("config", app.config),
                ("router", app.router),
            ],
            environment.globals,
        )


class TestApplicationIntegration(TestCase):
    def setUp(self):
        self.manager = mock.Mock(spec=Manager())
        self.router = Router(mapper=SimpleMapper())
        self.app = core.Application(manager=self.manager, router=self.router)
        self.request = Request(path="/")

    def test_it_starts_and_stops_the_request(self):
        self.router.add(self.request.path, lambda request : Response(b"Hello"))
        self.app.serve(self.request)
        self.assertEqual(
            self.manager.mock_calls, [
                mock.call.request_started(self.request),
                mock.call.request_served(self.request, Response(b"Hello")),
            ],
        )

    def test_it_serves_mapped_requests(self):
        self.router.add(self.request.path, lambda request : Response(b"Hello"))
        self.assertEqual(self.app.serve(self.request), Response(b"Hello"))

    def test_it_serves_404s_for_unmapped_requests_by_default(self):
        self.assertEqual(self.app.serve(self.request).code, 404)<commit_msg>Remove mocks in test_core<commit_after>from unittest import TestCase, skipIf

try:
    import jinja2
except ImportError:
    jinja2 = None

from minion import core, assets
from minion.compat import iteritems
from minion.request import Manager, Request, Response
from minion.routing import Router, SimpleMapper


class TestApplication(TestCase):
    def assertSubdict(self, subdict, of):
        """
        Assert that the given thing is a subdict of the given dict.

        """

        subdict = dict(subdict)
        matches = {k : v for k, v in iteritems(of) if k in subdict}
        self.assertEqual(subdict, matches)

    def test_it_delegates_routes_to_the_router(self):
        class Router(object):
            def add(this, route, fn, baz):
                self.added = route, fn, baz

        application = core.Application(router=Router())
        @application.route("/foo", baz=2)
        def fn(request):
            pass

        self.assertEqual(getattr(self, "added", None), ("/foo", fn, 2))

    def test_binds_to_its_bin(self):
        app = core.Application()
        self.assertSubdict(
            [
                ("app", app),
                ("config", app.config),
                ("manager", app.manager),
                ("router", app.router),
            ],
            app.bin.globals,
        )

    def test_it_can_bind_to_other_bins(self):
        app = core.Application()
        bin = assets.Bin(manager=app.manager)
        app.bind_bin(bin)
        self.assertSubdict(
            [
                ("app", app),
                ("config", app.config),
                ("manager", app.manager),
                ("router", app.router),
            ],
            bin.globals,
        )

    @skipIf(jinja2 is None, "jinja2 not found")
    def test_it_can_bind_to_jinja_environments(self):
        environment = jinja2.Environment()
        app = core.Application(jinja=environment)
        self.assertEqual(app.bin.globals["jinja"], environment)
        self.assertSubdict(
            [
                ("app", app),
                ("config", app.config),
                ("router", app.router),
            ],
            environment.globals,
        )


class TestApplicationIntegration(TestCase):
    def setUp(self):
        self.manager = Manager()
        self.router = Router(mapper=SimpleMapper())
        self.app = core.Application(manager=self.manager, router=self.router)
        self.request = Request(path="/")

    def test_it_serves_mapped_requests(self):
        self.router.add(self.request.path, lambda request : Response(b"Hello"))
        self.assertEqual(self.app.serve(self.request), Response(b"Hello"))

    def test_it_serves_404s_for_unmapped_requests_by_default(self):
        self.assertEqual(self.app.serve(self.request).code, 404)
{'fraction_non_alphanumeric': 0.1149561107359892, 'fraction_numerical': 0.004388926401080351, 'mean_word_length': 2.1957928802588995, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7428671', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1697, 'n_words': 343}