<commit_before>from web import page

import fake

import pytest


test_string = 'hello {}'
test_fill = 'world'


@pytest.fixture(scope='function')
def tmp(tmpdir):
    with tmpdir.join('test.html').open('w') as file:
        file.write(test_string)

    return str(tmpdir)


def test_page(tmp):
    class TestHandler(page.PageHandler):
        directory = tmp
        page = 'test.html'

    request = fake.FakeHTTPRequest(None, ('', 0), None, method='GET', handler=TestHandler)

    headers, response = request.response.headers, request.handler.respond()

    assert headers.get('Content-Type').startswith('text/html; charset=')

    assert response[0] == 200
    assert response[1] == test_string


def test_page_format(tmp):
    class TestHandler(page.PageHandler):
        directory = tmp
        page = 'test.html'

        def format(self, page):
            return test_string.format(test_fill)

    request = fake.FakeHTTPRequest(None, ('', 0), None, method='GET', handler=TestHandler)

    headers, response = request.response.headers, request.handler.respond()

    assert headers.get('Content-Type').startswith('text/html; charset=')

    assert response[0] == 200
    assert response[1] == test_string.format(test_fill)
<commit_msg>round out coverage for page.py<commit_after>from web import web, page

import fake

import pytest


test_string = 'hello {}'
test_fill = 'world'

test_error = '{message}'


@pytest.fixture(scope='function')
def tmp(tmpdir):
    with tmpdir.join('test.html').open('w') as file:
        file.write(test_string)

    with tmpdir.join('error.html').open('w') as file:
        file.write(test_error)

    return str(tmpdir)


def test_page(tmp):
    class TestHandler(page.PageHandler):
        directory = tmp
        page = 'test.html'

    request = fake.FakeHTTPRequest(None, ('', 0), None, method='GET', handler=TestHandler)

    headers, response = request.response.headers, request.handler.respond()

    assert headers.get('Content-Type').startswith('text/html; charset=')

    assert response[0] == 200
    assert response[1] == test_string


def test_page_format(tmp):
    class TestHandler(page.PageHandler):
        directory = tmp
        page = 'test.html'

        def format(self, page):
            return test_string.format(test_fill)

    request = fake.FakeHTTPRequest(None, ('', 0), None, method='GET', handler=TestHandler)

    headers, response = request.response.headers, request.handler.respond()

    assert headers.get('Content-Type').startswith('text/html; charset=')

    assert response[0] == 200
    assert response[1] == test_string.format(test_fill)


def test_page_new_error():
    assert page.new_error() == {'[0-9]{3}': page.PageErrorHandler}


def test_page_error(tmp):
    class TestHandler(page.PageErrorHandler):
        directory = tmp

        def respond(self):
            self.error = web.HTTPError(500)

            return super().respond()

    request = fake.FakeHTTPRequest(None, ('', 0), None, handler=TestHandler)

    headers, response = request.response.headers, request.handler.respond()

    assert headers.get('Content-Type').startswith('text/html; charset=')

    import sys
    sys.stderr.write(repr(response[1]))
    assert response[0] == 500
    assert response[1] == '500 - Internal Server Error\n'


def test_page_error_message(tmp):
    class TestHandler(page.PageErrorHandler):
        directory = tmp

        def respond(self):
            self.error = web.HTTPError(500, message=test_fill, status_message='a')

            return super().respond()

    request = fake.FakeHTTPRequest(None, ('', 0), None, handler=TestHandler)

    headers, response = request.response.headers, request.handler.respond()

    assert headers.get('Content-Type').startswith('text/html; charset=')

    assert response[0] == 500
    assert response[1] == test_fill

{'fraction_non_alphanumeric': 0.13636363636363635, 'fraction_numerical': 0.012727272727272728, 'mean_word_length': 3.5845238095238097, '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': '4849346', 'n_tokens_mistral': 1218, 'n_tokens_neox': 1145, 'n_words': 248}