test_resources.py 1.7 KB

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