test_key_mapper.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import pytest
  2. from pydantic import ValidationError
  3. from clean_python.s3 import KeyMapper
  4. @pytest.mark.parametrize(
  5. "pattern,ids,expected",
  6. [
  7. ("{}", ("foo",), "foo"),
  8. ("{}", (25,), "25"),
  9. ("bla/{}", ("foo",), "bla/foo"),
  10. ("raster-{}/{}", (25, "foo"), "raster-25/foo"),
  11. ],
  12. )
  13. def test_to_key(pattern, ids, expected):
  14. mapper = KeyMapper(pattern=pattern)
  15. assert mapper.to_key(*ids) == expected
  16. @pytest.mark.parametrize(
  17. "pattern,ids,expected",
  18. [
  19. ("{}", (), ""),
  20. ("bla/{}", (), "bla/"),
  21. ("raster-{}/{}", (25,), "raster-25/"),
  22. ],
  23. )
  24. def test_to_key_prefix(pattern, ids, expected):
  25. mapper = KeyMapper(pattern=pattern)
  26. assert mapper.to_key_prefix(*ids) == expected
  27. @pytest.mark.parametrize(
  28. "pattern,expected,key",
  29. [
  30. ("{}", ("foo",), "foo"),
  31. ("{}", (25,), "25"),
  32. ("bla/{}", ("foo",), "bla/foo"),
  33. ("raster-{}/{}", (25, "foo"), "raster-25/foo"),
  34. ],
  35. )
  36. def test_from_key(pattern, expected, key):
  37. mapper = KeyMapper(pattern=pattern)
  38. assert mapper.from_key(key) == expected
  39. @pytest.mark.parametrize("pattern", ["", "/{}", "{}-bla", "{a}/{}"])
  40. def test_validate_pattern(pattern):
  41. with pytest.raises(ValidationError):
  42. KeyMapper(pattern=pattern)
  43. @pytest.mark.parametrize(
  44. "pattern,names,expected",
  45. [
  46. ("{}", ("name",), "{name}"),
  47. ("raster-{}/{}", ("id", "name"), "raster-{id}/{name}"),
  48. ],
  49. )
  50. def test_get_named_pattern(pattern, names, expected):
  51. mapper = KeyMapper(pattern=pattern)
  52. assert mapper.get_named_pattern(*names) == expected