test_resources.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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)
  57. def test_files_joinpath():
  58. assert ir.files("resources") / "submodule"
  59. assert ir.files("resources") / "foo.txt"
  60. assert ir.files("resources") / "submodule" / "bar.txt"
  61. assert ir.files("resources.submodule") / "bar.txt"
  62. @pytest.mark.parametrize(
  63. "package, resource, expected",
  64. (
  65. ("resources", "foo.txt", b"bar"),
  66. ("resources.submodule", "bar.txt", b"foo"),
  67. ),
  68. )
  69. def test_files_read_bytes(package, resource, expected):
  70. assert (ir.files(package) / resource).read_bytes() == expected
  71. @pytest.mark.parametrize(
  72. "package, resource, expected",
  73. (
  74. ("resources", "foo.txt", "bar"),
  75. ("resources.submodule", "bar.txt", "foo"),
  76. ),
  77. )
  78. def test_files_read_text(package, resource, expected):
  79. assert (ir.files(package) / resource).read_text() == expected
  80. @pytest.mark.parametrize(
  81. "package, expected",
  82. (
  83. ("resources", ("foo.txt", "submodule")),
  84. ("resources.submodule", ("bar.txt",)),
  85. ),
  86. )
  87. def test_files_iterdir(package, expected):
  88. assert tuple(resource.name for resource in ir.files(package).iterdir()) == expected