<commit_before>from unittest import TestCase
from cStringIO import StringIO

from webtest import TestApp
from pyramid.mako_templating import MakoRenderingException

from PIL import Image

from ..images import files
from ..images.view import MissingOriginal

from . import utils


def setup():
    utils.load_images()


class Functional(TestCase):
    settings = None

    def setUp(self):
        self.app = TestApp(utils.make_app(self.settings))


class TestTemplatingFunctional(Functional):
    def test_render_index(self):
        resp = self.app.get('/')
        resp.mustcontain('base theme base.html')
        resp.mustcontain('foo theme base.html')
        resp.mustcontain('base theme index.html')

    def test_render_bad_return(self):
        with self.assertRaises(ValueError):
            self.app.get('/bad-return')

    def test_render_bad_template(self):
        with self.assertRaises(MakoRenderingException):
            self.app.get('/bad-template')


class TestThemeStrategy(Functional):
    def setUp(self):
        def theme_strategy(req):
            return req.params.get('theme', 'foo')
        self.app = TestApp(utils.make_app(theme_strategy=theme_strategy))

    def test_render_alternate_theme(self):
        resp = self.app.get('/?theme=base')
        self.assertIn('base', resp.body)
        self.assertNotIn('foo', resp.body)

        resp = self.app.get('/?theme=foo')
        self.assertIn('base', resp.body)
        self.assertIn('foo', resp.body)


class TestAssetsFunctional(Functional):
    def test_js_tag(self):
        resp = self.app.get('/js-tag')
        resp.mustcontain('main.js')
        resp.mustcontain('require.js')

    def test_css__tag(self):
        resp = self.app.get('/css-tag')
        resp.mustcontain('main.less')
        resp.mustcontain('less.js')


class TestCompiledFunctional(Functional):
    settings = {
        'pyramid_frontend.compile_less': True,
        'pyramid_frontend.compile_requirejs': True,
    }

    def setUp(self):
        Functional.setUp(self)
        utils.compile_assets()

    def test_js_tag(self):
        resp = self.app.get('/js-tag')
        self.assertNotIn('require.js', resp.body)
        # Look for something that looks like a hex digest
        self.assertRegexpMatches(resp.body, '[a-f0-9]{8,}')
        # Fetch a second time to test caching and make sure it's the same
        # result.
        resp2 = self.app.get('/js-tag')
        self.assertEqual(resp.body, resp2.body)

    def test_css__tag(self):
        resp = self.app.get('/css-tag')
        self.assertNotIn('less.js', resp.body)
        # Look for something that looks like a hex digest
        self.assertRegexpMatches(resp.body, '[a-f0-9]{8,}')


class TestImagesFunctional(Functional):
    def setUp(self):
        self.app = TestApp(utils.make_app())

    def test_fetch_image(self):
        url_resp = self.app.get('/image-url')
        img_resp = self.app.get(url_resp.body)
        # This image should be 200x200
        f = StringIO(img_resp.body)
        im = Image.open(f)
        self.assertEqual(im.size, (200, 200))

    def test_image_tag(self):
        url_resp = self.app.get('/image-url')
        tag_resp = self.app.get('/image-tag')
        tag_resp.mustcontain('200')
        tag_resp.mustcontain('height')<commit_msg>Fix typo in test method name<commit_after>from unittest import TestCase
from cStringIO import StringIO

from webtest import TestApp
from pyramid.mako_templating import MakoRenderingException

from PIL import Image

from ..images import files
from ..images.view import MissingOriginal

from . import utils


def setup():
    utils.load_images()


class Functional(TestCase):
    settings = None

    def setUp(self):
        self.app = TestApp(utils.make_app(self.settings))


class TestTemplatingFunctional(Functional):
    def test_render_index(self):
        resp = self.app.get('/')
        resp.mustcontain('base theme base.html')
        resp.mustcontain('foo theme base.html')
        resp.mustcontain('base theme index.html')

    def test_render_bad_return(self):
        with self.assertRaises(ValueError):
            self.app.get('/bad-return')

    def test_render_bad_template(self):
        with self.assertRaises(MakoRenderingException):
            self.app.get('/bad-template')


class TestThemeStrategy(Functional):
    def setUp(self):
        def theme_strategy(req):
            return req.params.get('theme', 'foo')
        self.app = TestApp(utils.make_app(theme_strategy=theme_strategy))

    def test_render_alternate_theme(self):
        resp = self.app.get('/?theme=base')
        self.assertIn('base', resp.body)
        self.assertNotIn('foo', resp.body)

        resp = self.app.get('/?theme=foo')
        self.assertIn('base', resp.body)
        self.assertIn('foo', resp.body)


class TestAssetsFunctional(Functional):
    def test_js_tag(self):
        resp = self.app.get('/js-tag')
        resp.mustcontain('main.js')
        resp.mustcontain('require.js')

    def test_css__tag(self):
        resp = self.app.get('/css-tag')
        resp.mustcontain('main.less')
        resp.mustcontain('less.js')


class TestCompiledFunctional(Functional):
    settings = {
        'pyramid_frontend.compile_less': True,
        'pyramid_frontend.compile_requirejs': True,
    }

    def setUp(self):
        Functional.setUp(self)
        utils.compile_assets()

    def test_js_tag(self):
        resp = self.app.get('/js-tag')
        self.assertNotIn('require.js', resp.body)
        # Look for something that looks like a hex digest
        self.assertRegexpMatches(resp.body, '[a-f0-9]{8,}')
        # Fetch a second time to test caching and make sure it's the same
        # result.
        resp2 = self.app.get('/js-tag')
        self.assertEqual(resp.body, resp2.body)

    def test_css_tag(self):
        resp = self.app.get('/css-tag')
        self.assertNotIn('less.js', resp.body)
        # Look for something that looks like a hex digest
        self.assertRegexpMatches(resp.body, '[a-f0-9]{8,}')


class TestImagesFunctional(Functional):
    def setUp(self):
        self.app = TestApp(utils.make_app())

    def test_fetch_image(self):
        url_resp = self.app.get('/image-url')
        img_resp = self.app.get(url_resp.body)
        # This image should be 200x200
        f = StringIO(img_resp.body)
        im = Image.open(f)
        self.assertEqual(im.size, (200, 200))

    def test_image_tag(self):
        url_resp = self.app.get('/image-url')
        tag_resp = self.app.get('/image-tag')
        tag_resp.mustcontain('200')
        tag_resp.mustcontain('height')
{'fraction_non_alphanumeric': 0.12518899304505593, 'fraction_numerical': 0.006954944058058663, 'mean_word_length': 2.7930045871559632, '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': '3387957', 'n_tokens_mistral': 2153, 'n_tokens_neox': 2082, 'n_words': 406}