Skip to content

Commit

Permalink
Apply awesome re-assert library for regex assertions in tests (#5134)
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Oct 25, 2020
1 parent e797e24 commit 2bcfb1f
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 44 deletions.
5 changes: 1 addition & 4 deletions aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ async def sendfile(self) -> None:
self.transport.write(data)
if self._count != 0:
await loop.sendfile(
self.transport,
self._fobj,
self._offset,
self._count
self.transport, self._fobj, self._offset, self._count
)
await super().write_eof()
return
Expand Down
1 change: 1 addition & 0 deletions requirements/ci-wheel.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ multidict==5.0.0
pytest==6.1.1
pytest-cov==2.10.1
pytest-mock==3.3.1
re-assert==1.1.0
typing_extensions==3.7.4.3
yarl==1.6.1

Expand Down
7 changes: 4 additions & 3 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import contextlib
import gc
import json
import re
import sys
from http.cookies import SimpleCookie
from io import BytesIO
from unittest import mock

import pytest
from multidict import CIMultiDict, MultiDict
from re_assert import Matches
from yarl import URL

import aiohttp
Expand Down Expand Up @@ -321,8 +321,9 @@ async def make_sess():
return ClientSession(connector=connector, loop=loop)

loop.run_until_complete(make_sess())
assert re.match(
"Session and connector has to use same event loop", str(ctx.value)
assert (
Matches("Session and connector has to use same event loop")
== str(ctx.value).strip()
)


Expand Down
9 changes: 4 additions & 5 deletions tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import re
from unittest import mock

import pytest
from multidict import CIMultiDict
from re_assert import Matches

from aiohttp.signals import Signal
from aiohttp.test_utils import make_mocked_coro, make_mocked_request
Expand Down Expand Up @@ -162,7 +162,6 @@ async def test_repr(app) -> None:

signal.append(callback)

assert re.match(
r"<Signal owner=<Application .+>, frozen=False, " r"\[<Mock id='\d+'>\]>",
repr(signal),
)
assert Matches(
r"<Signal owner=<Application .+>, frozen=False, " r"\[<Mock id='\d+'>\]>"
) == repr(signal)
4 changes: 2 additions & 2 deletions tests/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import abc
import asyncio
import gc
import re
import types
from collections import defaultdict
from itertools import groupby
from unittest import mock

import pytest
from re_assert import Matches

from aiohttp import streams

Expand Down Expand Up @@ -949,7 +949,7 @@ async def test___repr__waiter(self) -> None:
loop = asyncio.get_event_loop()
stream = self._make_one()
stream._waiter = loop.create_future()
assert re.search(r"<StreamReader w=<Future pending[\S ]*>>", repr(stream))
assert Matches(r"<StreamReader w=<Future pending[\S ]*>>") == repr(stream)
stream._waiter.set_result(None)
await stream._waiter
stream._waiter = None
Expand Down
7 changes: 4 additions & 3 deletions tests/test_urldispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from urllib.parse import unquote

import pytest
from re_assert import Matches
from yarl import URL

import aiohttp
Expand Down Expand Up @@ -312,7 +313,7 @@ def test_double_add_url_with_the_same_name(router) -> None:
regexp = "Duplicate 'name', already handled by"
with pytest.raises(ValueError) as ctx:
router.add_route("GET", "/get_other", handler2, name="name")
assert re.match(regexp, str(ctx.value))
assert Matches(regexp) == str(ctx.value)


def test_route_plain(router) -> None:
Expand Down Expand Up @@ -503,7 +504,7 @@ def test_contains(router) -> None:

def test_static_repr(router) -> None:
router.add_static("/get", os.path.dirname(aiohttp.__file__), name="name")
assert re.match(r"<StaticResource 'name' /get", repr(router["name"]))
assert Matches(r"<StaticResource 'name' /get") == repr(router["name"])


def test_static_adds_slash(router) -> None:
Expand Down Expand Up @@ -625,7 +626,7 @@ async def test_regular_match_info(router) -> None:
req = make_mocked_request("GET", "/get/john")
match_info = await router.resolve(req)
assert {"name": "john"} == match_info
assert re.match("<MatchInfo {'name': 'john'}: .+<Dynamic.+>>", repr(match_info))
assert Matches("<MatchInfo {'name': 'john'}: .+<Dynamic.+>>") == repr(match_info)


async def test_match_info_with_plus(router) -> None:
Expand Down
60 changes: 33 additions & 27 deletions tests/test_web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import datetime
import gzip
import json
import re
from concurrent.futures import ThreadPoolExecutor
from unittest import mock

import pytest
from multidict import CIMultiDict, CIMultiDictProxy
from re_assert import Matches

from aiohttp import HttpVersion, HttpVersion10, HttpVersion11, hdrs, signals
from aiohttp.payload import BytesPayload
Expand Down Expand Up @@ -314,7 +314,7 @@ async def test_chunked_encoding_forbidden_for_http_10() -> None:

with pytest.raises(RuntimeError) as ctx:
await resp.prepare(req)
assert re.match("Using chunked encoding is forbidden for HTTP/1.0", str(ctx.value))
assert Matches("Using chunked encoding is forbidden for HTTP/1.0") == str(ctx.value)


async def test_compression_no_accept() -> None:
Expand Down Expand Up @@ -689,7 +689,7 @@ def test_response_cookies() -> None:
'Set-Cookie: name=("")?; '
"expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/"
)
assert re.match(expected, str(resp.cookies))
assert Matches(expected) == str(resp.cookies)

resp.set_cookie("name", "value", domain="local.host")
expected = "Set-Cookie: name=value; Domain=local.host; Path=/"
Expand Down Expand Up @@ -741,7 +741,7 @@ def test_response_cookie__issue_del_cookie() -> None:
'Set-Cookie: name=("")?; '
"expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/"
)
assert re.match(expected, str(resp.cookies))
assert Matches(expected) == str(resp.cookies)


def test_cookie_set_after_del() -> None:
Expand Down Expand Up @@ -981,13 +981,15 @@ async def test_send_headers_for_empty_body(buf, writer) -> None:
await resp.prepare(req)
await resp.write_eof()
txt = buf.decode("utf8")
assert re.match(
"HTTP/1.1 200 OK\r\n"
"Content-Length: 0\r\n"
"Content-Type: application/octet-stream\r\n"
"Date: .+\r\n"
"Server: .+\r\n\r\n",
txt,
assert (
Matches(
"HTTP/1.1 200 OK\r\n"
"Content-Length: 0\r\n"
"Content-Type: application/octet-stream\r\n"
"Date: .+\r\n"
"Server: .+\r\n\r\n"
)
== txt
)


Expand All @@ -999,14 +1001,16 @@ async def test_render_with_body(buf, writer) -> None:
await resp.write_eof()

txt = buf.decode("utf8")
assert re.match(
"HTTP/1.1 200 OK\r\n"
"Content-Length: 4\r\n"
"Content-Type: application/octet-stream\r\n"
"Date: .+\r\n"
"Server: .+\r\n\r\n"
"data",
txt,
assert (
Matches(
"HTTP/1.1 200 OK\r\n"
"Content-Length: 4\r\n"
"Content-Type: application/octet-stream\r\n"
"Date: .+\r\n"
"Server: .+\r\n\r\n"
"data"
)
== txt
)


Expand All @@ -1019,14 +1023,16 @@ async def test_send_set_cookie_header(buf, writer) -> None:
await resp.write_eof()

txt = buf.decode("utf8")
assert re.match(
"HTTP/1.1 200 OK\r\n"
"Content-Length: 0\r\n"
"Set-Cookie: name=value\r\n"
"Content-Type: application/octet-stream\r\n"
"Date: .+\r\n"
"Server: .+\r\n\r\n",
txt,
assert (
Matches(
"HTTP/1.1 200 OK\r\n"
"Content-Length: 0\r\n"
"Set-Cookie: name=value\r\n"
"Content-Type: application/octet-stream\r\n"
"Date: .+\r\n"
"Server: .+\r\n\r\n"
)
== txt
)


Expand Down

0 comments on commit 2bcfb1f

Please sign in to comment.