Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert from unittest.TestCase to py.test #453

Merged
merged 6 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/wiki-example-code/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@


class WikiApp(object):

view_template = VIEW_TEMPLATE
edit_template = EDIT_TEMPLATE

Expand All @@ -72,7 +71,7 @@ def __call__(self, environ, start_response):
except AttributeError:
raise exc.HTTPBadRequest("No such action %r" % action)
resp = meth(req, page)
except exc.HTTPException, e:
except exc.HTTPException as e:
resp = e
return resp(environ, start_response)

Expand Down
2 changes: 1 addition & 1 deletion src/webob/acceptparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ def parse(value):
try:
parsed_accepted = Accept.parse(value)

for (media, q, _, _) in parsed_accepted:
for media, q, _, _ in parsed_accepted:
yield (media, q)
except ValueError:
pass
Expand Down
1 change: 0 additions & 1 deletion src/webob/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@


class RequestCookies(MutableMapping):

_cache_key = "webob._parsed_cookies"

def __init__(self, environ):
Expand Down
1 change: 0 additions & 1 deletion src/webob/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ def __call__(self, environ, start_response):


class WSGIHTTPException(Response, HTTPException):

# You should set in subclasses:
# code = 200
# title = 'OK'
Expand Down
1 change: 0 additions & 1 deletion src/webob/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ class BaseRequest:
_charset = None

def __init__(self, environ, **kw):

if type(environ) is not dict:
raise TypeError(f"WSGI environ must be a dict; you passed {environ!r}")

Expand Down
99 changes: 50 additions & 49 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import io
import socket
import unittest

import pytest

class TestSendRequest(unittest.TestCase):

class TestSendRequest:
def _getTargetClass(self):
from webob.client import SendRequest

Expand All @@ -30,7 +31,7 @@ def _makeEnviron(self, extra=None):
def test___call___unknown_scheme(self):
environ = self._makeEnviron({"wsgi.url_scheme": "abc"})
inst = self._makeOne()
self.assertRaises(ValueError, inst, environ, None)
pytest.raises(ValueError, inst, environ, None)

def test___call___gardenpath(self):
environ = self._makeEnviron()
Expand All @@ -39,13 +40,13 @@ def test___call___gardenpath(self):
inst = self._makeOne(HTTPConnection=conn_factory)

def start_response(status, headers):
self.assertEqual(status, "200 OK")
self.assertEqual(headers, [])
assert status == "200 OK"
assert headers == []
inst.start_response_called = True

iterable = inst(environ, start_response)
self.assertTrue(inst.start_response_called)
self.assertEqual(list(iterable), [b"foo"])
assert inst.start_response_called
assert list(iterable) == [b"foo"]

def test___call___no_servername_no_http_host(self):
environ = self._makeEnviron()
Expand All @@ -54,7 +55,7 @@ def test___call___no_servername_no_http_host(self):
response = DummyResponse("msg")
conn_factory = DummyConnectionFactory(response)
inst = self._makeOne(HTTPConnection=conn_factory)
self.assertRaises(ValueError, inst, environ, None)
pytest.raises(ValueError, inst, environ, None)

def test___call___no_servername_colon_not_in_host_http(self):
environ = self._makeEnviron()
Expand All @@ -65,15 +66,15 @@ def test___call___no_servername_colon_not_in_host_http(self):
inst = self._makeOne(HTTPConnection=conn_factory)

def start_response(status, headers):
self.assertEqual(status, "200 OK")
self.assertEqual(headers, [])
assert status == "200 OK"
assert headers == []
inst.start_response_called = True

iterable = inst(environ, start_response)
self.assertTrue(inst.start_response_called)
self.assertEqual(list(iterable), [b"foo"])
self.assertEqual(environ["SERVER_NAME"], "localhost")
self.assertEqual(environ["SERVER_PORT"], "80")
assert inst.start_response_called
assert list(iterable) == [b"foo"]
assert environ["SERVER_NAME"] == "localhost"
assert environ["SERVER_PORT"] == "80"

def test___call___no_servername_colon_not_in_host_https(self):
environ = self._makeEnviron()
Expand All @@ -85,15 +86,15 @@ def test___call___no_servername_colon_not_in_host_https(self):
inst = self._makeOne(HTTPSConnection=conn_factory)

def start_response(status, headers):
self.assertEqual(status, "200 OK")
self.assertEqual(headers, [])
assert status == "200 OK"
assert headers == []
inst.start_response_called = True

iterable = inst(environ, start_response)
self.assertTrue(inst.start_response_called)
self.assertEqual(list(iterable), [b"foo"])
self.assertEqual(environ["SERVER_NAME"], "localhost")
self.assertEqual(environ["SERVER_PORT"], "443")
assert inst.start_response_called
assert list(iterable) == [b"foo"]
assert environ["SERVER_NAME"] == "localhost"
assert environ["SERVER_PORT"] == "443"

def test___call___no_content_length(self):
environ = self._makeEnviron()
Expand All @@ -103,13 +104,13 @@ def test___call___no_content_length(self):
inst = self._makeOne(HTTPConnection=conn_factory)

def start_response(status, headers):
self.assertEqual(status, "200 OK")
self.assertEqual(headers, [])
assert status == "200 OK"
assert headers == []
inst.start_response_called = True

iterable = inst(environ, start_response)
self.assertTrue(inst.start_response_called)
self.assertEqual(list(iterable), [b"foo"])
assert inst.start_response_called
assert list(iterable) == [b"foo"]

def test___call___with_webob_client_timeout_and_timeout_supported(self):
environ = self._makeEnviron()
Expand All @@ -119,14 +120,14 @@ def test___call___with_webob_client_timeout_and_timeout_supported(self):
inst = self._makeOne(HTTPConnection=conn_factory)

def start_response(status, headers):
self.assertEqual(status, "200 OK")
self.assertEqual(headers, [])
assert status == "200 OK"
assert headers == []
inst.start_response_called = True

iterable = inst(environ, start_response)
self.assertTrue(inst.start_response_called)
self.assertEqual(list(iterable), [b"foo"])
self.assertEqual(conn_factory.kw, {"timeout": 10})
assert inst.start_response_called
assert list(iterable) == [b"foo"]
assert conn_factory.kw == {"timeout": 10}

def test___call___bad_content_length(self):
environ = self._makeEnviron({"CONTENT_LENGTH": "abc"})
Expand All @@ -135,13 +136,13 @@ def test___call___bad_content_length(self):
inst = self._makeOne(HTTPConnection=conn_factory)

def start_response(status, headers):
self.assertEqual(status, "200 OK")
self.assertEqual(headers, [])
assert status == "200 OK"
assert headers == []
inst.start_response_called = True

iterable = inst(environ, start_response)
self.assertTrue(inst.start_response_called)
self.assertEqual(list(iterable), [b"foo"])
assert inst.start_response_called
assert list(iterable) == [b"foo"]

def test___call___with_socket_timeout(self):
environ = self._makeEnviron()
Expand All @@ -151,12 +152,12 @@ def test___call___with_socket_timeout(self):
inst = self._makeOne(HTTPConnection=conn_factory)

def start_response(status, headers):
self.assertEqual(status, "504 Gateway Timeout")
assert status == "504 Gateway Timeout"
inst.start_response_called = True

iterable = inst(environ, start_response)
self.assertTrue(inst.start_response_called)
self.assertTrue(list(iterable)[0].startswith(b"504"))
assert inst.start_response_called
assert list(iterable)[0].startswith(b"504")

def test___call___with_socket_error_neg2(self):
environ = self._makeEnviron()
Expand All @@ -165,12 +166,12 @@ def test___call___with_socket_error_neg2(self):
inst = self._makeOne(HTTPConnection=conn_factory)

def start_response(status, headers):
self.assertEqual(status, "502 Bad Gateway")
assert status == "502 Bad Gateway"
inst.start_response_called = True

iterable = inst(environ, start_response)
self.assertTrue(inst.start_response_called)
self.assertTrue(list(iterable)[0].startswith(b"502"))
assert inst.start_response_called
assert list(iterable)[0].startswith(b"502")

def test___call___with_socket_error_ENODATA(self):
import errno
Expand All @@ -184,12 +185,12 @@ def test___call___with_socket_error_ENODATA(self):
inst = self._makeOne(HTTPConnection=conn_factory)

def start_response(status, headers):
self.assertEqual(status, "502 Bad Gateway")
assert status == "502 Bad Gateway"
inst.start_response_called = True

iterable = inst(environ, start_response)
self.assertTrue(inst.start_response_called)
self.assertTrue(list(iterable)[0].startswith(b"502"))
assert inst.start_response_called
assert list(iterable)[0].startswith(b"502")

def test___call___with_socket_error_unknown(self):
environ = self._makeEnviron()
Expand All @@ -198,10 +199,10 @@ def test___call___with_socket_error_unknown(self):
inst = self._makeOne(HTTPConnection=conn_factory)

def start_response(status, headers):
self.assertEqual(status, "502 Bad Gateway")
assert status == "502 Bad Gateway"
inst.start_response_called = True

self.assertRaises(socket.error, inst, environ, start_response)
pytest.raises(socket.error, inst, environ, start_response)

def test___call___nolength(self):
environ = self._makeEnviron()
Expand All @@ -210,14 +211,14 @@ def test___call___nolength(self):
inst = self._makeOne(HTTPConnection=conn_factory)

def start_response(status, headers):
self.assertEqual(status, "200 OK")
self.assertEqual(headers, [])
assert status == "200 OK"
assert headers == []
inst.start_response_called = True

iterable = inst(environ, start_response)
self.assertTrue(inst.start_response_called)
self.assertEqual(list(iterable), [b"foo"])
self.assertEqual(response.length, None)
assert inst.start_response_called
assert list(iterable) == [b"foo"]
assert response.length == None


class DummyMessage:
Expand Down
31 changes: 15 additions & 16 deletions tests/test_compat.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,56 @@
from io import BytesIO
import sys
import unittest

import pytest


class text_Tests(unittest.TestCase):
class TestText:
def _callFUT(self, *arg, **kw):
from webob.util import text_

return text_(*arg, **kw)

def test_binary(self):
result = self._callFUT(b"123")
self.assertTrue(isinstance(result, str))
self.assertEqual(result, str(b"123", "ascii"))
assert isinstance(result, str)
assert result == str(b"123", "ascii")

def test_binary_alternate_decoding(self):
result = self._callFUT(b"La Pe\xc3\xb1a", "utf-8")
self.assertTrue(isinstance(result, str))
self.assertEqual(result, str(b"La Pe\xc3\xb1a", "utf-8"))
assert isinstance(result, str)
assert result == str(b"La Pe\xc3\xb1a", "utf-8")

def test_binary_decoding_error(self):
self.assertRaises(UnicodeDecodeError, self._callFUT, b"\xff", "utf-8")
pytest.raises(UnicodeDecodeError, self._callFUT, b"\xff", "utf-8")

def test_text(self):
result = self._callFUT(str(b"123", "ascii"))
self.assertTrue(isinstance(result, str))
self.assertEqual(result, str(b"123", "ascii"))
assert isinstance(result, str)
assert result == str(b"123", "ascii")


class bytes_Tests(unittest.TestCase):
class TestBytes:
def _callFUT(self, *arg, **kw):
from webob.util import bytes_

return bytes_(*arg, **kw)

def test_binary(self):
result = self._callFUT(b"123")
self.assertTrue(isinstance(result, bytes))
self.assertEqual(result, b"123")
assert isinstance(result, bytes)
assert result == b"123"

def test_text(self):
val = str(b"123", "ascii")
result = self._callFUT(val)
self.assertTrue(isinstance(result, bytes))
self.assertEqual(result, b"123")
assert isinstance(result, bytes)
assert result == b"123"

def test_text_alternate_encoding(self):
val = str(b"La Pe\xc3\xb1a", "utf-8")
result = self._callFUT(val, "utf-8")
self.assertTrue(isinstance(result, bytes))
self.assertEqual(result, b"La Pe\xc3\xb1a")
assert isinstance(result, bytes)
assert result == b"La Pe\xc3\xb1a"


class Test_cgi_FieldStorage_Py3_tests:
Expand Down
Loading