| 1 |
"""Tests around :mod:`django_downloadview.io`.""" |
| 2 |
|
| 3 |
import unittest |
| 4 |
|
| 5 |
from django_downloadview import BytesIteratorIO, TextIteratorIO |
| 6 |
|
| 7 |
HELLO_TEXT = "Hello world!\né\n" |
| 8 |
HELLO_BYTES = b"Hello world!\n\xc3\xa9\n" |
| 9 |
|
| 10 |
|
| 11 |
def generate_hello_text(): |
| 12 |
"""Generate u'Hello world!\n'.""" |
| 13 |
yield "Hello " |
| 14 |
yield "world!" |
| 15 |
yield "\n" |
| 16 |
yield "é" |
| 17 |
yield "\n" |
| 18 |
|
| 19 |
|
| 20 |
def generate_hello_bytes(): |
| 21 |
"""Generate b'Hello world!\n'.""" |
| 22 |
yield b"Hello " |
| 23 |
yield b"world!" |
| 24 |
yield b"\n" |
| 25 |
yield b"\xc3\xa9" |
| 26 |
yield b"\n" |
| 27 |
|
| 28 |
|
| 29 |
class TextIteratorIOTestCase(unittest.TestCase): |
| 30 |
"""Tests around :class:`~django_downloadview.io.TextIteratorIO`.""" |
| 31 |
|
| 32 |
def test_read_text(self): |
| 33 |
"""TextIteratorIO obviously accepts text generator.""" |
| 34 |
file_obj = TextIteratorIO(generate_hello_text()) |
| 35 |
self.assertEqual(file_obj.read(), HELLO_TEXT) |
| 36 |
|
| 37 |
def test_read_bytes(self): |
| 38 |
"""TextIteratorIO converts bytes as text.""" |
| 39 |
file_obj = TextIteratorIO(generate_hello_bytes()) |
| 40 |
self.assertEqual(file_obj.read(), HELLO_TEXT) |
| 41 |
|
| 42 |
|
| 43 |
class BytesIteratorIOTestCase(unittest.TestCase): |
| 44 |
"""Tests around :class:`~django_downloadview.io.BytesIteratorIO`.""" |
| 45 |
|
| 46 |
def test_read_bytes(self): |
| 47 |
"""BytesIteratorIO obviously accepts bytes generator.""" |
| 48 |
file_obj = BytesIteratorIO(generate_hello_bytes()) |
| 49 |
self.assertEqual(file_obj.read(), HELLO_BYTES) |
| 50 |
|
| 51 |
def test_read_text(self): |
| 52 |
"""BytesIteratorIO converts text as bytes.""" |
| 53 |
file_obj = BytesIteratorIO(generate_hello_text()) |
| 54 |
self.assertEqual(file_obj.read(), HELLO_BYTES) |