mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-11-04 18:07:44 +03:00 
			
		
		
		
	* Add aiohttp extension module * Add giphynav-aiohttp app * Add missing docstrings * Remove print() call * Remove not needed import from flask extension tests * Improve coroutine provider tests * Add aiohttp extension tests * Update tox.ini * Add aiohttp extras * Try fix Python 3.4 tests * Try fix 3.6 tests * Stop running coroutine tests for Python 3.4 * Rename tests * Remove type hints * Fix pypy and change python version for coverage job to 3.8 * Fix coveralls job * Try fix Python 3.4, 3.5 tests * Make coverage job to run 3.5+ tests * Add tests * Add readme * Update the readmes * Add API docs * Add API docs page * Update changelog
		
			
				
	
	
		
			94 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
"""Dependency injector Aiohttp extension unit tests."""
 | 
						|
 | 
						|
from aiohttp import web
 | 
						|
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop
 | 
						|
 | 
						|
from dependency_injector import containers, providers
 | 
						|
from dependency_injector.ext import aiohttp
 | 
						|
 | 
						|
 | 
						|
async def index(_):
 | 
						|
    return web.Response(text='Hello World!')
 | 
						|
 | 
						|
 | 
						|
async def test(_):
 | 
						|
    return web.Response(text='Test!')
 | 
						|
 | 
						|
 | 
						|
class Test(web.View):
 | 
						|
    async def get(self):
 | 
						|
        return web.Response(text='Test class-based!')
 | 
						|
 | 
						|
 | 
						|
@web.middleware
 | 
						|
async def middleware(request, handler):
 | 
						|
    resp = await handler(request)
 | 
						|
    resp.text = resp.text + ' wink1'
 | 
						|
    return resp
 | 
						|
 | 
						|
 | 
						|
def middleware_factory(text):
 | 
						|
    @web.middleware
 | 
						|
    async def sample_middleware(request, handler):
 | 
						|
        resp = await handler(request)
 | 
						|
        resp.text = resp.text + text
 | 
						|
        return resp
 | 
						|
    return sample_middleware
 | 
						|
 | 
						|
 | 
						|
class ApplicationContainer(containers.DeclarativeContainer):
 | 
						|
 | 
						|
    app = aiohttp.Application(
 | 
						|
        web.Application,
 | 
						|
        middlewares=providers.List(
 | 
						|
            aiohttp.Middleware(middleware),
 | 
						|
            aiohttp.MiddlewareFactory(middleware_factory, text=' wink2'),
 | 
						|
        ),
 | 
						|
    )
 | 
						|
 | 
						|
    index_view = aiohttp.View(index)
 | 
						|
    test_view = aiohttp.View(test)
 | 
						|
    test_class_view = aiohttp.ClassBasedView(Test)
 | 
						|
 | 
						|
 | 
						|
class ApplicationTests(AioHTTPTestCase):
 | 
						|
 | 
						|
    async def get_application(self):
 | 
						|
        """
 | 
						|
        Override the get_app method to return your application.
 | 
						|
        """
 | 
						|
        container = ApplicationContainer()
 | 
						|
        app = container.app()
 | 
						|
        app.container = container
 | 
						|
        app.add_routes([
 | 
						|
            web.get('/', container.index_view.as_view()),
 | 
						|
            web.get('/test', container.test_view.as_view(), name='test'),
 | 
						|
            web.get('/test-class', container.test_class_view.as_view()),
 | 
						|
        ])
 | 
						|
        return app
 | 
						|
 | 
						|
    @unittest_run_loop
 | 
						|
    async def test_index(self):
 | 
						|
        response = await self.client.get('/')
 | 
						|
 | 
						|
        self.assertEqual(response.status, 200)
 | 
						|
        self.assertEqual(await response.text(), 'Hello World! wink2 wink1')
 | 
						|
 | 
						|
    @unittest_run_loop
 | 
						|
    async def test_test(self):
 | 
						|
        response = await self.client.get('/test')
 | 
						|
 | 
						|
        self.assertEqual(response.status, 200)
 | 
						|
        self.assertEqual(await response.text(), 'Test! wink2 wink1')
 | 
						|
 | 
						|
    @unittest_run_loop
 | 
						|
    async def test_test_class_based(self):
 | 
						|
        response = await self.client.get('/test-class')
 | 
						|
 | 
						|
        self.assertEqual(response.status, 200)
 | 
						|
        self.assertEqual(await response.text(), 'Test class-based! wink2 wink1')
 | 
						|
 | 
						|
    @unittest_run_loop
 | 
						|
    async def test_endpoints(self):
 | 
						|
        self.assertEqual(str(self.app.router['test'].url_for()), '/test')
 |