-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert get_session tests into pytest style
- Loading branch information
Showing
1 changed file
with
14 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,24 @@ | ||
import asyncio | ||
import unittest | ||
from unittest import mock | ||
import pytest | ||
|
||
import aiohttp | ||
from aiohttp.web import Request, HttpVersion | ||
from aiohttp import CIMultiDict, RawRequestMessage | ||
from aiohttp.test_utils import make_mocked_request | ||
|
||
from aiohttp_session import Session, get_session, SESSION_KEY | ||
|
||
|
||
class TestGetSession(unittest.TestCase): | ||
@asyncio.coroutine | ||
def test_get_stored_session(): | ||
req = make_mocked_request('GET', '/') | ||
session = Session('identity', data=None, new=False) | ||
req[SESSION_KEY] = session | ||
|
||
def setUp(self): | ||
self.loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(None) | ||
ret = yield from get_session(req) | ||
assert session is ret | ||
|
||
def tearDown(self): | ||
self.loop.stop() | ||
self.loop.run_forever() | ||
self.loop.close() | ||
|
||
def make_request(self, method, path, headers=CIMultiDict(), *, | ||
version=HttpVersion(1, 1), closing=False): | ||
self.app = mock.Mock() | ||
if tuple(int(i) for i in aiohttp.__version__.split('.')) < (0, 21): | ||
message = RawRequestMessage(method, path, version, headers, | ||
closing, False) | ||
else: | ||
message = RawRequestMessage(method, path, version, headers, | ||
[(k.encode('utf-8'), v.encode('utf-8')) | ||
for k, v in headers.items()], | ||
closing, False) | ||
self.payload = mock.Mock() | ||
self.transport = mock.Mock() | ||
self.writer = mock.Mock() | ||
self.reader = mock.Mock() | ||
req = Request(self.app, message, self.payload, | ||
self.transport, self.reader, self.writer) | ||
return req | ||
@asyncio.coroutine | ||
def test_session_is_not_stored(): | ||
req = make_mocked_request('GET', '/') | ||
|
||
def test_get_stored_session(self): | ||
|
||
@asyncio.coroutine | ||
def go(): | ||
req = self.make_request('GET', '/') | ||
session = Session('identity', data=None, new=False) | ||
req[SESSION_KEY] = session | ||
|
||
ret = yield from get_session(req) | ||
self.assertIs(session, ret) | ||
|
||
self.loop.run_until_complete(go()) | ||
|
||
def test_session_is_not_stored(self): | ||
|
||
@asyncio.coroutine | ||
def go(): | ||
req = self.make_request('GET', '/') | ||
|
||
with self.assertRaises(RuntimeError): | ||
yield from get_session(req) | ||
|
||
self.loop.run_until_complete(go()) | ||
with pytest.raises(RuntimeError): | ||
yield from get_session(req) |