Skip to content

Commit

Permalink
Merge pull request #359 from kurtmckee/update-pytest
Browse files Browse the repository at this point in the history
Update pytest
  • Loading branch information
kurtmckee committed Apr 11, 2023
2 parents 73a83ce + a6057fe commit 3803b58
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 133 deletions.
4 changes: 4 additions & 0 deletions changelog.d/20230410_205649_kurtmckee_update_pytest.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Project development
-------------------

* Escalate warnings to errors when running the test suite.
4 changes: 4 additions & 0 deletions changelog.d/20230410_212433_kurtmckee_update_pytest.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Project development
-------------------

* Fix warnings in the test suite that occur in Python 3.12.0a7.
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,9 @@ fail_under = 93

[tool.coverage.html]
directory = "htmlcov/"


[tool.pytest.ini_options]
filterwarnings = [
"error",
]
225 changes: 92 additions & 133 deletions tests/test_encoding_helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import io
import unittest

import pytest

Expand Down Expand Up @@ -73,36 +72,6 @@ def test_convert_file_to_utf8_decode_error_fallback():
)


class TestEncodingsHelpers(unittest.TestCase):
...


def make_prefix_file_wrapper_test(make_file):
def test(self):
f = feedparser.encodings.PrefixFileWrapper(b"abc", make_file(b"def"))
self.assertEqual(f.read(), b"abcdef")
self.assertEqual(f.read(), b"")

f = feedparser.encodings.PrefixFileWrapper(b"abc", make_file(b"def"))
self.assertEqual(f.read(2), b"ab")
self.assertEqual(f.read(2), b"cd")
self.assertEqual(f.read(2), b"ef")
self.assertEqual(f.read(2), b"")
self.assertEqual(f.read(), b"")

f = feedparser.encodings.PrefixFileWrapper(b"abc", make_file(b"def"))
self.assertEqual(f.read(3), b"abc")
self.assertEqual(f.read(3), b"def")
self.assertEqual(f.read(3), b"")
self.assertEqual(f.read(), b"")

f = feedparser.encodings.PrefixFileWrapper(b"abc", make_file(b"def"))
self.assertEqual(f.read(0), b"")
self.assertEqual(f.read(), b"abcdef")

return test


def _make_file(data):
return io.BytesIO(data)

Expand All @@ -125,124 +94,114 @@ def read(self, size=-1):
return super().read(1)


PREFIX_FILE_WRAPPER_FACTORIES = [
_make_file,
_make_file_in_the_middle,
_make_file_one_by_one,
]

for factory in PREFIX_FILE_WRAPPER_FACTORIES:
func = make_prefix_file_wrapper_test(factory)
setattr(
TestEncodingsHelpers,
f"test_prefix_file_wrapper_{factory.__name__.lstrip('_')}",
func,
)
del factory, func


def make_convert_file_prefix_to_utf8_test(headers):
from feedparser.encodings import convert_file_prefix_to_utf8, convert_to_utf8
@pytest.mark.parametrize(
"factory",
[
_make_file,
_make_file_in_the_middle,
_make_file_one_by_one,
],
)
def test_prefix_file_wrapper(factory):
f = feedparser.encodings.PrefixFileWrapper(b"abc", factory(b"def"))
assert f.read() == b"abcdef"
assert f.read() == b""

def test(self):
def call(data, **kwargs):
expected_result = {}
expected_output = convert_to_utf8(
headers, data.encode("utf-8"), expected_result
)
file = io.BytesIO(data.encode("utf-8"))
f = feedparser.encodings.PrefixFileWrapper(b"abc", factory(b"def"))
assert f.read(2) == b"ab"
assert f.read(2) == b"cd"
assert f.read(2) == b"ef"
assert f.read(2) == b""
assert f.read() == b""

actual_result = {}
prefix = convert_file_prefix_to_utf8(headers, file, actual_result, **kwargs)
rest = file.read()
f = feedparser.encodings.PrefixFileWrapper(b"abc", factory(b"def"))
assert f.read(3) == b"abc"
assert f.read(3) == b"def"
assert f.read(3) == b""
assert f.read() == b""

self.assertEqual(prefix + rest, expected_output)
self.assertEqual(
prefix.decode("utf-8") + rest.decode("utf-8"),
expected_output.decode("utf-8"),
)
f = feedparser.encodings.PrefixFileWrapper(b"abc", factory(b"def"))
assert f.read(0) == b""
assert f.read() == b"abcdef"

expected_result.pop("bozo_exception", None)
actual_result.pop("bozo_exception", None)
self.assertEqual(actual_result, expected_result)

# these should be parametrized, but it's too complicated to do
# Each emoji is 4 bytes long when encoded in UTF-8.
@pytest.mark.parametrize("data", ("😀😛🤯😱", "😀a😛b🤯c😱"))
@pytest.mark.parametrize(
"data_multiplier, kwargs",
(
(1, {"prefix_len": 3}),
(1, {"prefix_len": 4}),
(1, {"prefix_len": 5}),
(1, {"prefix_len": 8}),
(1, {"prefix_len": 40}),
(8, {"prefix_len": 2, "read_to_ascii_len": 4}),
(8, {"prefix_len": 4, "read_to_ascii_len": 4}),
),
)
@pytest.mark.parametrize("headers", ({}, {"content-type": "not-a-valid-content-type"}))
def test_convert_file_prefix_to_utf8(data, data_multiplier, kwargs, headers):
data = data * data_multiplier

# each of the emojis is 4 bytes long when encoded as utf-8
data = "😀😛🤯😱"
call(data, prefix_len=3)
call(data, prefix_len=4)
call(data, prefix_len=5)
call(data, prefix_len=8)
call(data, prefix_len=40)
call(data * 8, prefix_len=2, read_to_ascii_len=4)
call(data * 8, prefix_len=4, read_to_ascii_len=4)
expected_result = {}
expected_output = feedparser.encodings.convert_to_utf8(
headers, data.encode("utf-8"), expected_result
)
file = io.BytesIO(data.encode("utf-8"))

data = "😀a😛b🤯c😱"
call(data, prefix_len=3)
call(data, prefix_len=4)
call(data, prefix_len=5)
call(data * 8, prefix_len=2, read_to_ascii_len=4)
call(data * 8, prefix_len=4, read_to_ascii_len=4)
actual_result = {}
prefix = feedparser.encodings.convert_file_prefix_to_utf8(
headers, file, actual_result, **kwargs
)
rest = file.read()

return test
assert prefix + rest == expected_output
assert prefix.decode("utf-8") + rest.decode("utf-8") == expected_output.decode(
"utf-8"
)

expected_result.pop("bozo_exception", None)
actual_result.pop("bozo_exception", None)
assert actual_result == expected_result

def make_convert_file_to_utf8_test(headers, length):
from feedparser.encodings import convert_file_to_utf8, convert_to_utf8

@pytest.mark.parametrize("headers", ({}, {"content-type": "not-a-valid-content-type"}))
@pytest.mark.parametrize(
"length",
(
feedparser.encodings.CONVERT_FILE_PREFIX_LEN,
feedparser.encodings.CONVERT_FILE_STR_PREFIX_LEN,
),
)
def test_convert_file_to_utf8(headers, length):
digits = b"0123456789abcdef"
input = convert_to_utf8({}, b"", {}) + digits * int(length / len(digits) + 2)

def test(self):
expected_result = {}
expected_output = convert_to_utf8(headers, input, expected_result)
expected_result.pop("bozo_exception", None)

actual_result = {}
factory = convert_file_to_utf8(headers, io.BytesIO(input), actual_result)

self.assertEqual(
factory.get_text_file().read(), expected_output.decode("utf-8")
)
self.assertEqual(factory.get_binary_file().read(), expected_output)

actual_result.pop("bozo_exception", None)
self.assertEqual(actual_result, expected_result)
data = feedparser.encodings.convert_to_utf8({}, b"", {}) + digits * int(
length / len(digits) + 2
)

actual_result = {}
factory = convert_file_to_utf8(
headers, io.StringIO(input.decode("utf-8")), actual_result
)
expected_result = {}
expected_output = feedparser.encodings.convert_to_utf8(
headers, data, expected_result
)
expected_result.pop("bozo_exception", None)

self.assertEqual(
factory.get_text_file().read(), expected_output.decode("utf-8")
)
actual_result = {}
factory = feedparser.encodings.convert_file_to_utf8(
headers, io.BytesIO(data), actual_result
)

actual_result.pop("bozo_exception", None)
self.assertEqual(actual_result, expected_result)
assert factory.get_text_file().read() == expected_output.decode("utf-8")
assert factory.get_binary_file().read() == expected_output

return test
actual_result.pop("bozo_exception", None)
assert actual_result == expected_result

actual_result = {}
factory = feedparser.encodings.convert_file_to_utf8(
headers, io.StringIO(data.decode("utf-8")), actual_result
)

CONVERT_TO_UTF8_HEADERS = {
"simple": {},
"bad_content_type": {"content-type": "not-a-valid-content-type"},
}
CONVERT_TO_UTF8_LENGTHS = [
feedparser.encodings.CONVERT_FILE_PREFIX_LEN,
feedparser.encodings.CONVERT_FILE_STR_PREFIX_LEN,
]
assert factory.get_text_file().read() == expected_output.decode("utf-8")

for name, headers in CONVERT_TO_UTF8_HEADERS.items():
setattr(
TestEncodingsHelpers,
f"test_convert_file_prefix_to_utf8_{name}",
make_convert_file_prefix_to_utf8_test(headers),
)
for length in CONVERT_TO_UTF8_LENGTHS:
setattr(
TestEncodingsHelpers,
f"test_convert_file_to_utf8_{name}",
make_convert_file_to_utf8_test(headers, length),
)
actual_result.pop("bozo_exception", None)
assert actual_result == expected_result

0 comments on commit 3803b58

Please sign in to comment.