[PYTHON] How to test the attributes added by add_request_method of pyramid

pyramid add_request_method

You can add the property of request with the directive add_request_method of pyramid. (In the past, there was a directive called add_request_property, but it became deprecated in haste)

Effect of add_request_method

document: http://docs.pylonsproject.org/projects/pyramid/en/latest/api/config.html#pyramid.config.Configurator.add_request_method

There are two options to keep in mind:

--reify --Add an attribute to request. The added attributes are cached --property --Add an attribute to request. The added attribute is calculated each time

If nothing is added, it will be added as a method. (Also, if verify = True, it is automatically added as property)

## configuration
def includeme(config):
    ## request.get_foo()Can be called as
    config.add_request_method(get_foo, "get_foo")

    ## request.Can be accessed as foo
    config.add_request_method(get_foo2, "foo", reify=True)
    config.add_request_method(get_foo2, "foo", property=True)

Test of attributes added by add_request_method

test. If it's a unit test, you can use mock for text. Somehow it can be done by using dummy. A story about what to do during an integration test.

Where the directive defined by add_request_method is valid.

Let's look at the pyramid code. The answer is in pyramid / router.py. Roughly speaking, we can see the following.

--add_request_method stores an interface called IRequestExtensions in the key --Set with pyramid.request.Request._set_extensions ()

An example of how to write a test

An example of how to write an integration test with reference to the above This time, set request.foo. Just check if you can get it with request.foo.

# -*- coding:utf-8 -*-
import unittest


## definition

foo = object()

def get_foo(request):
    return foo

def includeme(config):
    config.add_request_method(get_foo, "foo", reify=True)


## test

class Tests(unittest.TestCase):
    def _makeOne(self, config, path="/"):
        from pyramid.request import Request
        from pyramid.interfaces import IRequestExtensions

        request = Request.blank(path)
        extensions = config.registry.getUtility(IRequestExtensions)
        request.registry = config.registry
        request._set_extensions(extensions)
        return request

    def test_it(self):
        from pyramid.testing import testConfig

        with testConfig() as config:
            config.include(includeme)

            ### request time
            request = self._makeOne(config, path="/")
            self.assertEqual(request.foo, foo)

Recommended Posts

How to test the attributes added by add_request_method of pyramid
How to check the version of Django
Setting to debug test by entering the contents of the library with pytest
__Getattr__ and __getattribute__ to customize the acquisition of object attributes by dots
I tried to verify the result of A / B test by chi-square test
How to calculate the volatility of a brand
How to find the area of the Voronoi diagram
How to specify attributes with Mock of python
How to erase the characters output by Python
[python] How to sort by the Nth Mth element of a multidimensional array
Note: How to get the last day of the month with python (added the first day of the month)
How to know the port number of the xinetd service
How to run the Ansible module added in Ansible Tower
How to get the number of digits in Python
How to visualize the decision tree model of scikit-learn
How to check the Java version used by Maven
[Blender] How to dynamically set the selection of EnumProperty
[Python] Summary of how to specify the color of the figure
How to hit the document of Magic Function (Line Magic)
How to access the global variable of the imported module
[Selenium] How to specify the relative path of chromedriver?
How to unprefix the DB name used by pytest-django
How to avoid the cut-off label of the graph created by the plot module using matplotlib
[Ruby] How to replace only a part of the string matched by the regular expression?
How to extract conditions (acquire all elements of Group that satisfy the conditions) for Group by Group
How to increase the processing speed of vertex position acquisition
[Ubuntu] How to delete the entire contents of a directory
How to find the optimal number of clusters in k-means
[Python] How to make a list of character strings character by character
How to switch the configuration file to be read by Python
How to use the generator
How to calculate the amount of calculation learned from ABC134-D
(Note) How to pass the path of your own module
How to check if the contents of the dictionary are the same in Python by hash value
How to summarize the results of FreeSurfer ~ aparc, aseg, wmparc ~
How to run the Export function of GCP Datastore automatically
How to increase the number of machine learning dataset images
Debug by attaching to the Python process of the SSH destination
How to see the contents of the Jupyter notebook ipynb file
How to find the scaling factor of a biorthogonal wavelet
How to use the decorator
How to plot a lot of legends by changing the color of the graph continuously with matplotlib
How to increase the axis
How to start the program
How to connect the contents of a list into a string
How to get the pixel value of the point from the satellite image by specifying the latitude and longitude
How to intercept or tamper with the SSL communication of the actual iOS device by a proxy
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
How to get and set the NTP server name by DHCP
How to handle multiple versions of CUDA in the same environment
How to determine the existence of a selenium element in Python
How to change the log level of Azure SDK for Python
How to implement Java code in the background of RedHat (LinuxONE)
How to know the internal structure of an object in Python
How to change the color of just the button pressed in Tkinter
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
[EC2] How to install chrome and the contents of each command
How to check the memory size of a variable in Python
Make the theme of Pythonista 3 like Monokai (how to make your own theme)
[Python] How to get the first and last days of the month
How to make a command to read the configuration file with pyramid