test_resources.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- coding: utf-8 -*-
  2. import importlib.resources as ir
  3. import pytest
  4. @pytest.mark.parametrize("package, resource", (
  5. ("resources", "foo.txt"),
  6. ("resources.submodule", "bar.txt")
  7. ))
  8. def test_is_resource_good_path(package, resource):
  9. assert ir.is_resource(package, resource)
  10. @pytest.mark.parametrize("package, resource", (
  11. ("resources", "111.txt"),
  12. ("resources.submodule", "222.txt")
  13. ))
  14. def test_is_resource_missing(package, resource):
  15. assert not ir.is_resource(package, resource)
  16. def test_is_resource_subresource_directory():
  17. # Directories are not resources.
  18. assert not ir.is_resource("resources", "submodule")
  19. @pytest.mark.parametrize("package, resource, expected", (
  20. ("resources", "foo.txt", b"bar"),
  21. ("resources.submodule", "bar.txt", b"foo")
  22. ))
  23. def test_read_binary_good_path(package, resource, expected):
  24. assert ir.read_binary(package, resource) == expected
  25. def test_read_binary_missing():
  26. with pytest.raises(FileNotFoundError):
  27. ir.read_binary("resources", "111.txt")
  28. @pytest.mark.parametrize("package, resource, expected", (
  29. ("resources", "foo.txt", "bar"),
  30. ("resources.submodule", "bar.txt", "foo")
  31. ))
  32. def test_read_text_good_path(package, resource, expected):
  33. assert ir.read_text(package, resource) == expected
  34. def test_read_text_missing():
  35. with pytest.raises(FileNotFoundError):
  36. ir.read_text("resources", "111.txt")
  37. @pytest.mark.parametrize("package, expected", (
  38. ("resources", ["submodule", "foo.txt"]),
  39. ("resources.submodule", ["bar.txt"])
  40. ))
  41. def test_contents_good_path(package, expected):
  42. assert sorted(ir.contents(package)) == sorted(expected)