Enabling Dependencies in (Python) Google App Engine

I want to achieve maximum testability in the Google App Engine application that I am writing in Python.

Basically what I am doing is creating a universal base handler that inherits google.appengine.ext.webapp.RequestHandler. My base handler will expose common functions in my application, such as repository functions, session object, etc.

When it WSGIApplicationreceives the request, it will find the handler class that has been registered for the requested URL and call its constructor, after which it will call the method with the name initializepassing in requestand responseobjects.

Now, for verification, I want to be able to "mock" these objects (along with my own objects). So my question is: how do I start introducing these bullying? I can override the method initializein my base handler and check some global "check flag" and initialize some stub objects requestand response. But this seems wrong (at least in my opinion). And how do I start initializing my other objects (which may depend on objects requestand response)?

As you can probably say, I'm a little new to Python, so any recommendations would be most welcome.

EDIT:

I was told that this question is a little difficult to answer without any code, so here:

from google.appengine.ext import webapp
from ..utils import gmemsess
from .. import errors
_user_id_name = 'userid'

class Handler(webapp.RequestHandler):
    '''
    classdocs
    '''

    def __init__(self):
        '''
        Constructor
        '''
        self.charset = 'utf8'
        self._session = None

    def _getsession(self):
        if not self._session:
            self._session = gmemsess.Session(self)
        return self._session

    def _get_is_logged_in(self):
        return self.session.has_key(_user_id_name)

    def _get_user_id(self):
        if not self.is_logged_in:
            raise errors.UserNotLoggedInError()
        return self.session[_user_id_name]

    session = property(_getsession)
    is_logged_in = property(_get_is_logged_in)
    user_id = property(_get_user_id)

, . gmemsess.Session(self). Session , request ( cookie). self , webapp.RequestHandler. , () WSGIApplication initialize, ( ). initialize (webapp.RequestHandler). :

def initialize(self, request, response):
    """Initializes this request handler with the given Request and 
     Response."""
    self.request = request
    self.response = response

, WSGIApplication :

def __call__(self, environ, start_response):
    """Called by WSGI when a request comes in."""
    request = self.REQUEST_CLASS(environ)
    response = self.RESPONSE_CLASS()
    WSGIApplication.active_instance = self
    handler = None
    groups = ()
    for regexp, handler_class in self._url_mapping:
        match = regexp.match(request.path)
        if match:
            handler = handler_class()
            handler.initialize(request, response)
            groups = match.groups()
            break

    self.current_request_args = groups
    if handler:
        try:
            method = environ['REQUEST_METHOD']
            if method == 'GET':
                handler.get(*groups)
            elif method == 'POST':
                handler.post(*groups)
            '''SNIP'''

, :

     handler = handler_class()
     handler.initialize(request, response)

, . , , , , ( ):

    def __init__(self, session_type):
        '''
        Constructor
        '''
        self.charset = 'utf8'
        self._session = None
        self._session_type = session_type

    def _getsession(self):
        if not self._session:
            self._session = self._session_type(self)
        return self._session

, , WSGIApplication . , session_type , ( ), , , Python, , , . , - .

.

+3
2

, , - , :

# myhandler.py
session_class = gmemsess.Session

class Handler(webapp.Request
    def _getsession(self):
        if not self._session:
            self._session = session_class(self)
        return self._session

, , :

import myhandler

if testing:
    myhandler.session_class = MyTestingSession

, , WSGIApplication , .

+2

? , mock Request and Response, , , handler.initialize(, ) mocks. .

+1

Source: https://habr.com/ru/post/1769869/


All Articles