<commit_before>from unittest import TestCase

from paste.wsgiwrappers import WSGIRequest
from paste.fixture import TestApp
from paste.registry import RegistryManager

import pylons
from pylons.util import ContextObj
from pylons.controllers import Controller, WSGIController, XMLRPCController

from __init__ import TestWSGIController, SetupCacheGlobal, ControllerWrap

class BasicController(Controller):
    def index(self):
        return "hi all"

    def view(self, id, name):
        return "Hi %s, %s" % (id, name)
    
    def kargs(self, id, **kargs):
        return [id, kargs]

class BasicFilteredController(Controller):
    def __init__(self):
        self.before = 0
        self.after = 0

    def __before__(self):
        self.before += 1

    def __after__(self):
        self.after += 1

    def index(self):
        return 'hi all, before is %s' % self.before

class BasicWSGIController(WSGIController):
    def index(self):
        return pylons.Response('hello world')

    def yield_fun(self):
        def its():
            x = 0
            while x < 100:
                yield 'hi'
                x += 1
        return its()
    
    def strme(self):
        return "hi there"

class FilteredWSGIController(WSGIController):
    def __init__(self):
        self.before = 0
        self.after = 0

    def __before__(self):
        self.before += 1

    def __after__(self):
        self.after += 1

    def index(self):
        return pylons.Response('hi all, before is %s' % self.before)


class TestBasicController(TestCase):
    def setUp(self):
        self.environ = {'pylons.routes_dict':dict(action='index'),
                        'paste.config':dict(global_conf=dict(debug=True))}
        pylons.request._push_object(WSGIRequest(self.environ))
        pylons.c._push_object(ContextObj())
        self.controller = BasicController()
        self.controller.start_response = None
    
    def tearDown(self):
        pylons.request._pop_object()
        pylons.c._pop_object()

    def test_basic_call(self):
        assert "hi all" == self.controller()
    
    def test_inspect_call(self):
        self.environ['pylons.routes_dict'].update(dict(action='view', id=4, name='fred'))
        assert "Hi 4, fred" == self.controller()

    def test_private_action(self):
        self.environ['pylons.routes_dict']['action'] = '_private'
        response = self.controller()
        assert isinstance(response, pylons.Response)
        assert response.status_code == 404
    
    def test_attach_locals(self):
        pylons.g._push_object(object())
        pylons.session._push_object(object())
        pylons.cache._push_object(object())
        pylons.buffet._push_object(object())
        c = id(pylons.c._current_obj())
        g = id(pylons.g._current_obj())
        cache = id(pylons.cache._current_obj())
        session = id(pylons.session._current_obj())
        request = id(pylons.request._current_obj())
        buffet = id(pylons.buffet._current_obj())
        cr = self.controller
        cr._attach_locals()
        assert c == id(cr.c)
        assert g == id(cr.g)
        assert request == id(cr.request)
        assert cache == id(cr.cache)
        assert session == id(cr.session)
        assert buffet == id(cr.buffet)
    
    def test_kwargs_call(self):
        self.environ['pylons.routes_dict'].update(dict(action='kargs', id=4, 
                                                       name='fred'))
        id, kargs = self.controller()<commit_msg>[svn] Switched controller test to capture and expect DeprecationWarning.<commit_after>import warnings
from unittest import TestCase

from paste.wsgiwrappers import WSGIRequest
from paste.fixture import TestApp
from paste.registry import RegistryManager

import pylons
from pylons.util import ContextObj
from pylons.controllers import Controller, WSGIController, XMLRPCController

from __init__ import TestWSGIController, SetupCacheGlobal, ControllerWrap

class BasicController(Controller):
    def index(self):
        return "hi all"

    def view(self, id, name):
        return "Hi %s, %s" % (id, name)
    
    def kargs(self, id, **kargs):
        return [id, kargs]

class BasicFilteredController(Controller):
    def __init__(self):
        self.before = 0
        self.after = 0

    def __before__(self):
        self.before += 1

    def __after__(self):
        self.after += 1

    def index(self):
        return 'hi all, before is %s' % self.before

class BasicWSGIController(WSGIController):
    def index(self):
        return pylons.Response('hello world')

    def yield_fun(self):
        def its():
            x = 0
            while x < 100:
                yield 'hi'
                x += 1
        return its()
    
    def strme(self):
        return "hi there"

class FilteredWSGIController(WSGIController):
    def __init__(self):
        self.before = 0
        self.after = 0

    def __before__(self):
        self.before += 1

    def __after__(self):
        self.after += 1

    def index(self):
        return pylons.Response('hi all, before is %s' % self.before)


class TestBasicController(TestCase):
    def setUp(self):
        self.environ = {'pylons.routes_dict':dict(action='index'),
                        'paste.config':dict(global_conf=dict(debug=True))}
        pylons.request._push_object(WSGIRequest(self.environ))
        pylons.c._push_object(ContextObj())
        self.controller = BasicController()
        self.controller.start_response = None
    
    def tearDown(self):
        pylons.request._pop_object()
        pylons.c._pop_object()

    def test_basic_call(self):
        assert "hi all" == self.controller()
    
    def test_inspect_call(self):
        self.environ['pylons.routes_dict'].update(dict(action='view', id=4, name='fred'))
        assert "Hi 4, fred" == self.controller()

    def test_private_action(self):
        self.environ['pylons.routes_dict']['action'] = '_private'
        response = self.controller()
        assert isinstance(response, pylons.Response)
        assert response.status_code == 404
    
    def test_attach_locals(self):
        pylons.g._push_object(object())
        pylons.session._push_object(object())
        pylons.cache._push_object(object())
        pylons.buffet._push_object(object())
        c = id(pylons.c._current_obj())
        g = id(pylons.g._current_obj())
        cache = id(pylons.cache._current_obj())
        session = id(pylons.session._current_obj())
        request = id(pylons.request._current_obj())
        buffet = id(pylons.buffet._current_obj())
        cr = self.controller
        
        # Attach locals is now deprecated, expect the warning it'll throw
        warnings.simplefilter('error', DeprecationWarning)
        try:
            cr._attach_locals()
        except DeprecationWarning, msg:
            assert '_attach_locals is deprecated: Pylons special objects are now' in msg[0]
        
        # Drop the notice of it this time, and do the command anyways, then 
        # turn it back to normal
        warnings.simplefilter('ignore', DeprecationWarning)
        cr._attach_locals()
        warnings.simplefilter('always', DeprecationWarning)
        assert c == id(cr.c)
        assert g == id(cr.g)
        assert request == id(cr.request)
        assert cache == id(cr.cache)
        assert session == id(cr.session)
        assert buffet == id(cr.buffet)
    
    def test_kwargs_call(self):
        self.environ['pylons.routes_dict'].update(dict(action='kargs', id=4, 
                                                       name='fred'))
        id, kargs = self.controller()
{'fraction_non_alphanumeric': 0.11023725258880587, 'fraction_numerical': 0.005112072355485647, 'mean_word_length': 2.4650317892824707, 'pattern_counts': {'":': 0, '<': 5, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '652967', 'n_tokens_mistral': 2313, 'n_tokens_neox': 2274, 'n_words': 513}