| 1 |
"""Tests for the 'bucket' plugin.""" |
| 2 |
|
| 3 |
from datetime import datetime |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from beets import config |
| 8 |
from beets.exceptions import UserError |
| 9 |
from beets.test.helper import BeetsTestCase |
| 10 |
from beetsplug import bucket |
| 11 |
|
| 12 |
|
| 13 |
class BucketPluginTest(BeetsTestCase): |
| 14 |
def setUp(self): |
| 15 |
super().setUp() |
| 16 |
self.plugin = bucket.BucketPlugin() |
| 17 |
|
| 18 |
def _setup_config( |
| 19 |
self, |
| 20 |
bucket_year=[], |
| 21 |
bucket_alpha=[], |
| 22 |
bucket_alpha_regex={}, |
| 23 |
extrapolate=False, |
| 24 |
): |
| 25 |
config["bucket"]["bucket_year"] = bucket_year |
| 26 |
config["bucket"]["bucket_alpha"] = bucket_alpha |
| 27 |
config["bucket"]["bucket_alpha_regex"] = bucket_alpha_regex |
| 28 |
config["bucket"]["extrapolate"] = extrapolate |
| 29 |
self.plugin.setup() |
| 30 |
|
| 31 |
def test_year_single_year(self): |
| 32 |
"""If a single year is given, range starts from this year and stops at |
| 33 |
the year preceding the one of next bucket.""" |
| 34 |
self._setup_config(bucket_year=["1950s", "1970s"]) |
| 35 |
assert self.plugin._tmpl_bucket("1959") == "1950s" |
| 36 |
assert self.plugin._tmpl_bucket("1969") == "1950s" |
| 37 |
|
| 38 |
def test_year_single_year_last_folder(self): |
| 39 |
"""If a single year is given for the last bucket, extend it to current |
| 40 |
year.""" |
| 41 |
self._setup_config(bucket_year=["1950", "1970"]) |
| 42 |
assert self.plugin._tmpl_bucket("2014") == "1970" |
| 43 |
next_year = datetime.now().year + 1 |
| 44 |
assert self.plugin._tmpl_bucket(str(next_year)) == str(next_year) |
| 45 |
|
| 46 |
def test_year_two_years(self): |
| 47 |
"""Buckets can be named with the 'from-to' syntax.""" |
| 48 |
self._setup_config(bucket_year=["1950-59", "1960-1969"]) |
| 49 |
assert self.plugin._tmpl_bucket("1959") == "1950-59" |
| 50 |
assert self.plugin._tmpl_bucket("1969") == "1960-1969" |
| 51 |
|
| 52 |
def test_year_multiple_years(self): |
| 53 |
"""Buckets can be named by listing all the years""" |
| 54 |
self._setup_config(bucket_year=["1950,51,52,53"]) |
| 55 |
assert self.plugin._tmpl_bucket("1953") == "1950,51,52,53" |
| 56 |
assert self.plugin._tmpl_bucket("1974") == "1974" |
| 57 |
|
| 58 |
def test_year_out_of_range(self): |
| 59 |
"""If no range match, return the year""" |
| 60 |
self._setup_config(bucket_year=["1950-59", "1960-69"]) |
| 61 |
assert self.plugin._tmpl_bucket("1974") == "1974" |
| 62 |
self._setup_config(bucket_year=[]) |
| 63 |
assert self.plugin._tmpl_bucket("1974") == "1974" |
| 64 |
|
| 65 |
def test_year_out_of_range_extrapolate(self): |
| 66 |
"""If no defined range match, extrapolate all ranges using the most |
| 67 |
common syntax amongst existing buckets and return the matching one.""" |
| 68 |
self._setup_config(bucket_year=["1950-59", "1960-69"], extrapolate=True) |
| 69 |
assert self.plugin._tmpl_bucket("1914") == "1910-19" |
| 70 |
# pick single year format |
| 71 |
self._setup_config( |
| 72 |
bucket_year=["1962-81", "2002", "2012"], extrapolate=True |
| 73 |
) |
| 74 |
assert self.plugin._tmpl_bucket("1983") == "1982" |
| 75 |
# pick from-end format |
| 76 |
self._setup_config( |
| 77 |
bucket_year=["1962-81", "2002", "2012-14"], extrapolate=True |
| 78 |
) |
| 79 |
assert self.plugin._tmpl_bucket("1983") == "1982-01" |
| 80 |
# extrapolate add ranges, but never modifies existing ones |
| 81 |
self._setup_config( |
| 82 |
bucket_year=["1932", "1942", "1952", "1962-81", "2002"], |
| 83 |
extrapolate=True, |
| 84 |
) |
| 85 |
assert self.plugin._tmpl_bucket("1975") == "1962-81" |
| 86 |
|
| 87 |
def test_alpha_all_chars(self): |
| 88 |
"""Alphabet buckets can be named by listing all their chars""" |
| 89 |
self._setup_config(bucket_alpha=["ABCD", "FGH", "IJKL"]) |
| 90 |
assert self.plugin._tmpl_bucket("garry") == "FGH" |
| 91 |
|
| 92 |
def test_alpha_first_last_chars(self): |
| 93 |
"""Alphabet buckets can be named by listing the 'from-to' syntax""" |
| 94 |
self._setup_config(bucket_alpha=["0->9", "A->D", "F-H", "I->Z"]) |
| 95 |
assert self.plugin._tmpl_bucket("garry") == "F-H" |
| 96 |
assert self.plugin._tmpl_bucket("2pac") == "0->9" |
| 97 |
|
| 98 |
def test_alpha_out_of_range(self): |
| 99 |
"""If no range match, return the initial""" |
| 100 |
self._setup_config(bucket_alpha=["ABCD", "FGH", "IJKL"]) |
| 101 |
assert self.plugin._tmpl_bucket("errol") == "E" |
| 102 |
self._setup_config(bucket_alpha=[]) |
| 103 |
assert self.plugin._tmpl_bucket("errol") == "E" |
| 104 |
|
| 105 |
def test_alpha_regex(self): |
| 106 |
"""Check regex is used""" |
| 107 |
self._setup_config( |
| 108 |
bucket_alpha=["foo", "bar"], |
| 109 |
bucket_alpha_regex={"foo": "^[a-d]", "bar": "^[e-z]"}, |
| 110 |
) |
| 111 |
assert self.plugin._tmpl_bucket("alpha") == "foo" |
| 112 |
assert self.plugin._tmpl_bucket("delta") == "foo" |
| 113 |
assert self.plugin._tmpl_bucket("zeta") == "bar" |
| 114 |
assert self.plugin._tmpl_bucket("Alpha") == "A" |
| 115 |
|
| 116 |
def test_alpha_regex_mix(self): |
| 117 |
"""Check mixing regex and non-regex is possible""" |
| 118 |
self._setup_config( |
| 119 |
bucket_alpha=["A - D", "E - L"], |
| 120 |
bucket_alpha_regex={"A - D": "^[0-9a-dA-D…äÄ]"}, |
| 121 |
) |
| 122 |
assert self.plugin._tmpl_bucket("alpha") == "A - D" |
| 123 |
assert self.plugin._tmpl_bucket("Ärzte") == "A - D" |
| 124 |
assert self.plugin._tmpl_bucket("112") == "A - D" |
| 125 |
assert self.plugin._tmpl_bucket("…and Oceans") == "A - D" |
| 126 |
assert self.plugin._tmpl_bucket("Eagles") == "E - L" |
| 127 |
|
| 128 |
def test_bad_alpha_range_def(self): |
| 129 |
"""If bad alpha range definition, a UserError is raised.""" |
| 130 |
with pytest.raises(UserError): |
| 131 |
self._setup_config(bucket_alpha=["$%"]) |
| 132 |
|
| 133 |
def test_bad_year_range_def_no4digits(self): |
| 134 |
"""If bad year range definition, a UserError is raised. |
| 135 |
Range origin must be expressed on 4 digits. |
| 136 |
""" |
| 137 |
with pytest.raises(UserError): |
| 138 |
self._setup_config(bucket_year=["62-64"]) |
| 139 |
|
| 140 |
def test_bad_year_range_def_nodigits(self): |
| 141 |
"""If bad year range definition, a UserError is raised. |
| 142 |
At least the range origin must be declared. |
| 143 |
""" |
| 144 |
with pytest.raises(UserError): |
| 145 |
self._setup_config(bucket_year=["nodigits"]) |
| 146 |
|
| 147 |
def check_span_from_str(self, sstr, dfrom, dto): |
| 148 |
d = bucket.span_from_str(sstr) |
| 149 |
assert dfrom == d["from"] |
| 150 |
assert dto == d["to"] |
| 151 |
|
| 152 |
def test_span_from_str(self): |
| 153 |
self.check_span_from_str("1980 2000", 1980, 2000) |
| 154 |
self.check_span_from_str("1980 00", 1980, 2000) |
| 155 |
self.check_span_from_str("1930 00", 1930, 2000) |
| 156 |
self.check_span_from_str("1930 50", 1930, 1950) |