| 1 |
import unittest |
| 2 |
|
| 3 |
from importlib import import_module, resources |
| 4 |
|
| 5 |
from . import util |
| 6 |
|
| 7 |
|
| 8 |
class CommonBinaryTests(util.CommonTests, unittest.TestCase): |
| 9 |
def execute(self, package, path): |
| 10 |
resources.files(package).joinpath(path).read_bytes() |
| 11 |
|
| 12 |
|
| 13 |
class CommonTextTests(util.CommonTests, unittest.TestCase): |
| 14 |
def execute(self, package, path): |
| 15 |
resources.files(package).joinpath(path).read_text(encoding='utf-8') |
| 16 |
|
| 17 |
|
| 18 |
class ReadTests: |
| 19 |
def test_read_bytes(self): |
| 20 |
result = resources.files(self.data).joinpath('binary.file').read_bytes() |
| 21 |
self.assertEqual(result, bytes(range(4))) |
| 22 |
|
| 23 |
def test_read_text_default_encoding(self): |
| 24 |
result = ( |
| 25 |
resources.files(self.data) |
| 26 |
.joinpath('utf-8.file') |
| 27 |
.read_text(encoding='utf-8') |
| 28 |
) |
| 29 |
self.assertEqual(result, 'Hello, UTF-8 world!\n') |
| 30 |
|
| 31 |
def test_read_text_given_encoding(self): |
| 32 |
result = ( |
| 33 |
resources.files(self.data) |
| 34 |
.joinpath('utf-16.file') |
| 35 |
.read_text(encoding='utf-16') |
| 36 |
) |
| 37 |
self.assertEqual(result, 'Hello, UTF-16 world!\n') |
| 38 |
|
| 39 |
def test_read_text_with_errors(self): |
| 40 |
""" |
| 41 |
Raises UnicodeError without the 'errors' argument. |
| 42 |
""" |
| 43 |
target = resources.files(self.data) / 'utf-16.file' |
| 44 |
self.assertRaises(UnicodeError, target.read_text, encoding='utf-8') |
| 45 |
result = target.read_text(encoding='utf-8', errors='ignore') |
| 46 |
self.assertEqual( |
| 47 |
result, |
| 48 |
'H\x00e\x00l\x00l\x00o\x00,\x00 ' |
| 49 |
'\x00U\x00T\x00F\x00-\x001\x006\x00 ' |
| 50 |
'\x00w\x00o\x00r\x00l\x00d\x00!\x00\n\x00', |
| 51 |
) |
| 52 |
|
| 53 |
|
| 54 |
class ReadDiskTests(ReadTests, util.DiskSetup, unittest.TestCase): |
| 55 |
pass |
| 56 |
|
| 57 |
|
| 58 |
class ReadZipTests(ReadTests, util.ZipSetup, unittest.TestCase): |
| 59 |
def test_read_submodule_resource(self): |
| 60 |
submodule = import_module('data01.subdirectory') |
| 61 |
result = resources.files(submodule).joinpath('binary.file').read_bytes() |
| 62 |
self.assertEqual(result, bytes(range(4, 8))) |
| 63 |
|
| 64 |
def test_read_submodule_resource_by_name(self): |
| 65 |
result = ( |
| 66 |
resources.files('data01.subdirectory').joinpath('binary.file').read_bytes() |
| 67 |
) |
| 68 |
self.assertEqual(result, bytes(range(4, 8))) |
| 69 |
|
| 70 |
|
| 71 |
class ReadNamespaceTests(ReadTests, util.DiskSetup, unittest.TestCase): |
| 72 |
MODULE = 'namespacedata01' |
| 73 |
|
| 74 |
|
| 75 |
class ReadNamespaceZipTests(ReadTests, util.ZipSetup, unittest.TestCase): |
| 76 |
MODULE = 'namespacedata01' |
| 77 |
|
| 78 |
def test_read_submodule_resource(self): |
| 79 |
submodule = import_module('namespacedata01.subdirectory') |
| 80 |
result = resources.files(submodule).joinpath('binary.file').read_bytes() |
| 81 |
self.assertEqual(result, bytes(range(12, 16))) |
| 82 |
|
| 83 |
def test_read_submodule_resource_by_name(self): |
| 84 |
result = ( |
| 85 |
resources.files('namespacedata01.subdirectory') |
| 86 |
.joinpath('binary.file') |
| 87 |
.read_bytes() |
| 88 |
) |
| 89 |
self.assertEqual(result, bytes(range(12, 16))) |
| 90 |
|
| 91 |
|
| 92 |
if __name__ == '__main__': |
| 93 |
unittest.main() |