<commit_before>from comparch import Lookup
from morepath.app import App
from morepath.publish import publish
from morepath.request import Request
from morepath.resource import register_resource
from morepath.setup import setup
from werkzeug.test import EnvironBuilder


def get_request(*args, **kw):
    app = kw.pop('app')
    lookup = Lookup(app.class_lookup())
    result = Request(EnvironBuilder(*args, **kw).get_environ())
    result.lookup = lookup
    return result


class Model(object):
    pass


# XXX these tests have gained more dependencies, on app and setup and
# lookup generation. see about refactor code so that they can be tested
# with less heavy installation
def test_resource():
    setup()
    app = App()

    def resource(request, model):
        return "Resource!"

    register_resource(app, Model, resource, predicates=dict(name=''))

    model = Model()
    result = publish(get_request(path='', app=app), model)
    assert result.data == 'Resource!'


def test_predicates():
    setup()
    app = App()

    def resource(request, model):
        return "all"

    def post_resource(request, model):
        return "post"

    register_resource(app, Model, resource, predicates=dict(name=''))
    register_resource(app, Model, post_resource,
                      predicates=dict(name='', request_method='POST'))

    model = Model()
    assert publish(get_request(path='', app=app), model).data == 'all'
    
    assert (publish(get_request(path='', method='POST', app=app),
                    model).data == 'post')
    
def test_notfound():
    setup()
    app = App()
    model = Model()
    response = publish(get_request(path='', app=app), model)
    assert response.status == '404 NOT FOUND'


def test_notfound_with_predicates():
    setup()
    app = App()

    def resource(request, model):
        return "resource"

    register_resource(app, Model, resource, predicates=dict(name=''))
    model = Model()
    response = publish(get_request(path='foo', app=app), model)
    assert response.status == '404 NOT FOUND'


# def test_model_is_response():
#     class MyModel(Response):
#         pass
#     reg = Registry()
#     model = MyModel()
<commit_msg>A test for returning Response explicitly from resource.<commit_after>from comparch import Lookup
from morepath.app import App
from morepath.publish import publish
from morepath.request import Request, Response
from morepath.resource import register_resource
from morepath.setup import setup
from werkzeug.test import EnvironBuilder


def get_request(*args, **kw):
    app = kw.pop('app')
    lookup = Lookup(app.class_lookup())
    result = Request(EnvironBuilder(*args, **kw).get_environ())
    result.lookup = lookup
    return result


class Model(object):
    pass


# XXX these tests have gained more dependencies, on app and setup and
# lookup generation. see about refactor code so that they can be tested
# with less heavy installation
def test_resource():
    setup()
    app = App()

    def resource(request, model):
        return "Resource!"

    register_resource(app, Model, resource, predicates=dict(name=''))

    model = Model()
    result = publish(get_request(path='', app=app), model)
    assert result.data == 'Resource!'


def test_predicates():
    setup()
    app = App()

    def resource(request, model):
        return "all"

    def post_resource(request, model):
        return "post"

    register_resource(app, Model, resource, predicates=dict(name=''))
    register_resource(app, Model, post_resource,
                      predicates=dict(name='', request_method='POST'))

    model = Model()
    assert publish(get_request(path='', app=app), model).data == 'all'
    assert (publish(get_request(path='', method='POST', app=app),
                    model).data == 'post')


def test_notfound():
    setup()
    app = App()
    model = Model()
    response = publish(get_request(path='', app=app), model)
    assert response.status == '404 NOT FOUND'


def test_notfound_with_predicates():
    setup()
    app = App()

    def resource(request, model):
        return "resource"

    register_resource(app, Model, resource, predicates=dict(name=''))
    model = Model()
    response = publish(get_request(path='foo', app=app), model)
    assert response.status == '404 NOT FOUND'


def test_response_returned():
    setup()
    app = App()

    def resource(request, model):
        return Response('Hello world!')

    register_resource(app, Model, resource)
    model = Model()
    response = publish(get_request(path='', app=app), model)
    assert response.data == 'Hello world!'

# def test_model_is_response():
#     class MyModel(Response):
#         pass
#     reg = Registry()
#     model = MyModel()

{'fraction_non_alphanumeric': 0.12254798565703438, 'fraction_numerical': 0.0025311115798354778, 'mean_word_length': 3.5161904761904763, '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': '6863017', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1340, 'n_words': 398}