Skip to content

Commit

Permalink
Merge pull request #447 from kgaughan/modern-assertions
Browse files Browse the repository at this point in the history
Use modern assertion methods where appropriate
  • Loading branch information
digitalresistor authored Nov 15, 2024
2 parents d005ec2 + 151a5ff commit a20fe86
Show file tree
Hide file tree
Showing 13 changed files with 311 additions and 311 deletions.
58 changes: 29 additions & 29 deletions tests/test_adjustments.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,35 @@ def _callFUT(self, s):

def test_s_is_None(self):
result = self._callFUT(None)
self.assertEqual(result, False)
self.assertFalse(result)

def test_s_is_True(self):
result = self._callFUT(True)
self.assertEqual(result, True)
self.assertTrue(result)

def test_s_is_False(self):
result = self._callFUT(False)
self.assertEqual(result, False)
self.assertFalse(result)

def test_s_is_true(self):
result = self._callFUT("True")
self.assertEqual(result, True)
self.assertTrue(result)

def test_s_is_false(self):
result = self._callFUT("False")
self.assertEqual(result, False)
self.assertFalse(result)

def test_s_is_yes(self):
result = self._callFUT("yes")
self.assertEqual(result, True)
self.assertTrue(result)

def test_s_is_on(self):
result = self._callFUT("on")
self.assertEqual(result, True)
self.assertTrue(result)

def test_s_is_1(self):
result = self._callFUT(1)
self.assertEqual(result, True)
self.assertTrue(result)


class Test_as_socket_list(unittest.TestCase):
Expand All @@ -56,7 +56,7 @@ def test_only_sockets_in_list(self):
if hasattr(socket, "AF_UNIX"):
sockets.append(socket.socket(socket.AF_UNIX, socket.SOCK_STREAM))
new_sockets = as_socket_list(sockets)
self.assertEqual(sockets, new_sockets)
self.assertListEqual(sockets, new_sockets)

for sock in sockets:
sock.close()
Expand All @@ -70,7 +70,7 @@ def test_not_only_sockets_in_list(self):
{"something": "else"},
]
new_sockets = as_socket_list(sockets)
self.assertEqual(new_sockets, [sockets[0], sockets[1]])
self.assertListEqual(new_sockets, [sockets[0], sockets[1]])

for sock in [sock for sock in sockets if isinstance(sock, socket.socket)]:
sock.close()
Expand Down Expand Up @@ -140,9 +140,9 @@ def test_goodvars(self):
self.assertEqual(inst.port, 8080)
self.assertEqual(inst.threads, 5)
self.assertEqual(inst.trusted_proxy, "192.168.1.1")
self.assertEqual(inst.trusted_proxy_headers, {"forwarded"})
self.assertSetEqual(inst.trusted_proxy_headers, {"forwarded"})
self.assertEqual(inst.trusted_proxy_count, 2)
self.assertEqual(inst.log_untrusted_proxy_headers, True)
self.assertTrue(inst.log_untrusted_proxy_headers)
self.assertEqual(inst.url_scheme, "https")
self.assertEqual(inst.backlog, 20)
self.assertEqual(inst.recv_bytes, 200)
Expand All @@ -152,17 +152,17 @@ def test_goodvars(self):
self.assertEqual(inst.connection_limit, 1000)
self.assertEqual(inst.cleanup_interval, 1100)
self.assertEqual(inst.channel_timeout, 1200)
self.assertEqual(inst.log_socket_errors, True)
self.assertTrue(inst.log_socket_errors)
self.assertEqual(inst.max_request_header_size, 1300)
self.assertEqual(inst.max_request_body_size, 1400)
self.assertEqual(inst.expose_tracebacks, True)
self.assertTrue(inst.expose_tracebacks)
self.assertEqual(inst.asyncore_loop_timeout, 5)
self.assertEqual(inst.asyncore_use_poll, True)
self.assertTrue(inst.asyncore_use_poll)
self.assertEqual(inst.ident, "abc")
self.assertEqual(inst.unix_socket_perms, 0o777)
self.assertEqual(inst.url_prefix, "/foo")
self.assertEqual(inst.ipv4, True)
self.assertEqual(inst.ipv6, False)
self.assertTrue(inst.ipv4)
self.assertFalse(inst.ipv6)

bind_pairs = [
sockaddr[:2]
Expand All @@ -172,35 +172,35 @@ def test_goodvars(self):

# On Travis, somehow we start listening to two sockets when resolving
# localhost...
self.assertEqual(("127.0.0.1", 8080), bind_pairs[0])
self.assertTupleEqual(("127.0.0.1", 8080), bind_pairs[0])

def test_goodvar_listen(self):
inst = self._makeOne(listen="127.0.0.1")

bind_pairs = [(host, port) for (_, _, _, (host, port)) in inst.listen]

self.assertEqual(bind_pairs, [("127.0.0.1", 8080)])
self.assertListEqual(bind_pairs, [("127.0.0.1", 8080)])

def test_default_listen(self):
inst = self._makeOne()

bind_pairs = [(host, port) for (_, _, _, (host, port)) in inst.listen]

self.assertEqual(bind_pairs, [("0.0.0.0", 8080)])
self.assertListEqual(bind_pairs, [("0.0.0.0", 8080)])

def test_multiple_listen(self):
inst = self._makeOne(listen="127.0.0.1:9090 127.0.0.1:8080")

bind_pairs = [sockaddr[:2] for (_, _, _, sockaddr) in inst.listen]

self.assertEqual(bind_pairs, [("127.0.0.1", 9090), ("127.0.0.1", 8080)])
self.assertListEqual(bind_pairs, [("127.0.0.1", 9090), ("127.0.0.1", 8080)])

def test_wildcard_listen(self):
inst = self._makeOne(listen="*:8080")

bind_pairs = [sockaddr[:2] for (_, _, _, sockaddr) in inst.listen]

self.assertTrue(len(bind_pairs) >= 1)
self.assertGreaterEqual(len(bind_pairs), 1)

def test_ipv6_no_port(self): # pragma: nocover
if not self._hasIPv6():
Expand All @@ -210,7 +210,7 @@ def test_ipv6_no_port(self): # pragma: nocover

bind_pairs = [sockaddr[:2] for (_, _, _, sockaddr) in inst.listen]

self.assertEqual(bind_pairs, [("::1", 8080)])
self.assertListEqual(bind_pairs, [("::1", 8080)])

def test_bad_port(self):
self.assertRaises(ValueError, self._makeOne, listen="127.0.0.1:test")
Expand All @@ -230,7 +230,7 @@ def test_service_port(self):

bind_pairs = [sockaddr[:2] for (_, _, _, sockaddr) in inst.listen]

self.assertEqual(bind_pairs, [("127.0.0.1", 80), ("0.0.0.0", 443)])
self.assertListEqual(bind_pairs, [("127.0.0.1", 80), ("0.0.0.0", 443)])

def test_dont_mix_host_port_listen(self):
self.assertRaises(
Expand All @@ -247,7 +247,7 @@ def test_good_sockets(self):
socket.socket(socket.AF_INET, socket.SOCK_STREAM),
]
inst = self._makeOne(sockets=sockets)
self.assertEqual(inst.sockets, sockets)
self.assertListEqual(inst.sockets, sockets)
sockets[0].close()
sockets[1].close()

Expand Down Expand Up @@ -329,7 +329,7 @@ def test_trusted_proxy_headers_string_list(self):
trusted_proxy="localhost",
trusted_proxy_headers="x-forwarded-for x-forwarded-by",
)
self.assertEqual(
self.assertSetEqual(
inst.trusted_proxy_headers, {"x-forwarded-for", "x-forwarded-by"}
)

Expand All @@ -338,7 +338,7 @@ def test_trusted_proxy_headers_string_list_newlines(self):
trusted_proxy="localhost",
trusted_proxy_headers="x-forwarded-for\nx-forwarded-by\nx-forwarded-host",
)
self.assertEqual(
self.assertSetEqual(
inst.trusted_proxy_headers,
{"x-forwarded-for", "x-forwarded-by", "x-forwarded-host"},
)
Expand Down Expand Up @@ -376,10 +376,10 @@ def test_ipv6_disabled(self):

def test_server_header_removable(self):
inst = self._makeOne(ident=None)
self.assertEqual(inst.ident, None)
self.assertIsNone(inst.ident)

inst = self._makeOne(ident="")
self.assertEqual(inst.ident, None)
self.assertIsNone(inst.ident)

inst = self._makeOne(ident="specific_header")
self.assertEqual(inst.ident, "specific_header")
Expand Down
40 changes: 20 additions & 20 deletions tests/test_buffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def test___len__(self):
def test___nonzero__(self):
inst = self._makeOne()
inst.remain = 10
self.assertEqual(bool(inst), True)
self.assertTrue(bool(inst))
inst.remain = 0
self.assertEqual(bool(inst), True)
self.assertTrue(bool(inst))

def test_append(self):
f = io.BytesIO(b"data")
Expand Down Expand Up @@ -95,7 +95,7 @@ def test_prune_remain_notzero(self):
nf = io.BytesIO()
inst.newfile = lambda *x: nf
inst.prune()
self.assertTrue(inst.file is not f)
self.assertIsNot(inst.file, f)
self.assertEqual(nf.getvalue(), b"d")

def test_prune_remain_zero_tell_notzero(self):
Expand All @@ -105,15 +105,15 @@ def test_prune_remain_zero_tell_notzero(self):
inst.newfile = lambda *x: nf
inst.remain = 0
inst.prune()
self.assertTrue(inst.file is not f)
self.assertIsNot(inst.file, f)
self.assertEqual(nf.getvalue(), b"d")

def test_prune_remain_zero_tell_zero(self):
f = io.BytesIO()
inst = self._makeOne(f)
inst.remain = 0
inst.prune()
self.assertTrue(inst.file is f)
self.assertIs(inst.file, f)

def test_close(self):
f = io.BytesIO()
Expand Down Expand Up @@ -188,14 +188,14 @@ def test_prepare_not_seekable(self):
self.assertFalse(hasattr(inst, "seek"))
self.assertFalse(hasattr(inst, "tell"))
result = inst.prepare()
self.assertEqual(result, False)
self.assertFalse(result)
self.assertEqual(inst.remain, 0)

def test_prepare_not_seekable_closeable(self):
f = KindaFilelike(b"abc", close=1)
inst = self._makeOne(f)
result = inst.prepare()
self.assertEqual(result, False)
self.assertFalse(result)
self.assertEqual(inst.remain, 0)
self.assertTrue(hasattr(inst, "close"))

Expand Down Expand Up @@ -297,9 +297,9 @@ def test___len__buf_is_not_None(self):
def test___nonzero__(self):
inst = self._makeOne()
inst.buf = b"abc"
self.assertEqual(bool(inst), True)
self.assertTrue(bool(inst))
inst.buf = b""
self.assertEqual(bool(inst), False)
self.assertFalse(bool(inst))
self.buffers_to_close.remove(inst)

def test___nonzero___on_int_overflow_buffer(self):
Expand All @@ -311,9 +311,9 @@ def __len__(self):
return 0x7FFFFFFFFFFFFFFF + 1

inst.buf = int_overflow_buf()
self.assertEqual(bool(inst), True)
self.assertTrue(bool(inst))
inst.buf = b""
self.assertEqual(bool(inst), False)
self.assertFalse(bool(inst))
self.buffers_to_close.remove(inst)

def test__create_buffer_large(self):
Expand All @@ -322,7 +322,7 @@ def test__create_buffer_large(self):
inst = self._makeOne()
inst.strbuf = b"x" * 11
inst._create_buffer()
self.assertEqual(inst.buf.__class__, TempfileBasedBuffer)
self.assertIsInstance(inst.buf, TempfileBasedBuffer)
self.assertEqual(inst.buf.get(100), b"x" * 11)
self.assertEqual(inst.strbuf, b"")

Expand All @@ -332,7 +332,7 @@ def test__create_buffer_small(self):
inst = self._makeOne()
inst.strbuf = b"x" * 5
inst._create_buffer()
self.assertEqual(inst.buf.__class__, BytesIOBasedBuffer)
self.assertIsInstance(inst.buf, BytesIOBasedBuffer)
self.assertEqual(inst.buf.get(100), b"x" * 5)
self.assertEqual(inst.strbuf, b"")

Expand All @@ -346,7 +346,7 @@ def test_append_with_len_more_than_max_int(self):
result = inst.append(b"x")
# we don't want this to throw an OverflowError on Python 2 (see
# https://github.com/Pylons/waitress/issues/47)
self.assertEqual(result, None)
self.assertIsNone(result)
self.buffers_to_close.remove(inst)

def test_append_buf_None_not_longer_than_srtbuf_limit(self):
Expand Down Expand Up @@ -392,22 +392,22 @@ def test_get_buf_None_skip_True(self):
inst = self._makeOne()
inst.strbuf = b"x" * 5
r = inst.get(5, skip=True)
self.assertFalse(inst.buf is None)
self.assertIsNotNone(inst.buf)
self.assertEqual(r, b"xxxxx")

def test_skip_buf_None(self):
inst = self._makeOne()
inst.strbuf = b"data"
inst.skip(4)
self.assertEqual(inst.strbuf, b"")
self.assertNotEqual(inst.buf, None)
self.assertIsNotNone(inst.buf)

def test_skip_buf_None_allow_prune_True(self):
inst = self._makeOne()
inst.strbuf = b"data"
inst.skip(4, True)
self.assertEqual(inst.strbuf, b"")
self.assertEqual(inst.buf, None)
self.assertIsNone(inst.buf)

def test_prune_buf_None(self):
inst = self._makeOne()
Expand All @@ -423,7 +423,7 @@ def prune(self):

inst.buf = Buf()
inst.prune()
self.assertEqual(inst.buf.pruned, True)
self.assertTrue(inst.buf.pruned)
self.buffers_to_close.remove(inst)

def test_prune_with_buf_overflow(self):
Expand Down Expand Up @@ -459,7 +459,7 @@ def test_prune_with_buflen_more_than_max_int(self):
result = inst.prune()
# we don't want this to throw an OverflowError on Python 2 (see
# https://github.com/Pylons/waitress/issues/47)
self.assertEqual(result, None)
self.assertIsNone(result)

def test_getfile_buf_None(self):
inst = self._makeOne()
Expand All @@ -477,7 +477,7 @@ def test_getfile_buf_not_None(self):
def test_close_nobuf(self):
inst = self._makeOne()
inst.buf = None
self.assertEqual(inst.close(), None) # doesnt raise
self.assertIsNone(inst.close()) # doesnt raise
self.buffers_to_close.remove(inst)

def test_close_withbuf(self):
Expand Down
Loading

0 comments on commit a20fe86

Please sign in to comment.