django-downloadview/tests/io.py

55 lines
1.5 KiB
Python
Raw Normal View History

"""Tests around :mod:`django_downloadview.io`."""
2024-08-05 08:51:17 +00:00
import unittest
2020-01-07 14:18:54 +00:00
from django_downloadview import BytesIteratorIO, TextIteratorIO
2020-01-07 14:21:34 +00:00
HELLO_TEXT = "Hello world!\né\n"
2020-01-07 14:10:42 +00:00
HELLO_BYTES = b"Hello world!\n\xc3\xa9\n"
def generate_hello_text():
"""Generate u'Hello world!\n'."""
2020-01-07 14:21:34 +00:00
yield "Hello "
yield "world!"
yield "\n"
yield "é"
yield "\n"
def generate_hello_bytes():
"""Generate b'Hello world!\n'."""
2020-01-07 14:10:42 +00:00
yield b"Hello "
yield b"world!"
yield b"\n"
yield b"\xc3\xa9"
yield b"\n"
class TextIteratorIOTestCase(unittest.TestCase):
"""Tests around :class:`~django_downloadview.io.TextIteratorIO`."""
2020-01-07 14:10:42 +00:00
def test_read_text(self):
"""TextIteratorIO obviously accepts text generator."""
file_obj = TextIteratorIO(generate_hello_text())
self.assertEqual(file_obj.read(), HELLO_TEXT)
def test_read_bytes(self):
"""TextIteratorIO converts bytes as text."""
file_obj = TextIteratorIO(generate_hello_bytes())
self.assertEqual(file_obj.read(), HELLO_TEXT)
class BytesIteratorIOTestCase(unittest.TestCase):
"""Tests around :class:`~django_downloadview.io.BytesIteratorIO`."""
2020-01-07 14:10:42 +00:00
def test_read_bytes(self):
"""BytesIteratorIO obviously accepts bytes generator."""
file_obj = BytesIteratorIO(generate_hello_bytes())
self.assertEqual(file_obj.read(), HELLO_BYTES)
def test_read_text(self):
"""BytesIteratorIO converts text as bytes."""
file_obj = BytesIteratorIO(generate_hello_text())
self.assertEqual(file_obj.read(), HELLO_BYTES)