1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
# -*- coding: utf-8 -*-
# This file is part of beets.
# Copyright 2016, Fabrice Laporte.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
"""Tests for the 'bucket' plugin."""
from __future__ import division, absolute_import, print_function
from test._common import unittest
from beetsplug import bucket
from beets import config, ui
from test.helper import TestHelper
class BucketPluginTest(unittest.TestCase, TestHelper):
def setUp(self):
self.setup_beets()
self.plugin = bucket.BucketPlugin()
def tearDown(self):
self.teardown_beets()
def _setup_config(self, bucket_year=[], bucket_alpha=[],
bucket_alpha_regex={}, extrapolate=False):
config['bucket']['bucket_year'] = bucket_year
config['bucket']['bucket_alpha'] = bucket_alpha
config['bucket']['bucket_alpha_regex'] = bucket_alpha_regex
config['bucket']['extrapolate'] = extrapolate
self.plugin.setup()
def test_year_single_year(self):
"""If a single year is given, range starts from this year and stops at
the year preceding the one of next bucket."""
self._setup_config(bucket_year=['1950s', '1970s'])
self.assertEqual(self.plugin._tmpl_bucket('1959'), '1950s')
self.assertEqual(self.plugin._tmpl_bucket('1969'), '1950s')
def test_year_single_year_last_folder(self):
"""If a single year is given for the last bucket, extend it to current
year."""
self._setup_config(bucket_year=['1950', '1970'])
self.assertEqual(self.plugin._tmpl_bucket('2014'), '1970')
self.assertEqual(self.plugin._tmpl_bucket('2025'), '2025')
def test_year_two_years(self):
"""Buckets can be named with the 'from-to' syntax."""
self._setup_config(bucket_year=['1950-59', '1960-1969'])
self.assertEqual(self.plugin._tmpl_bucket('1959'), '1950-59')
self.assertEqual(self.plugin._tmpl_bucket('1969'), '1960-1969')
def test_year_multiple_years(self):
"""Buckets can be named by listing all the years"""
self._setup_config(bucket_year=['1950,51,52,53'])
self.assertEqual(self.plugin._tmpl_bucket('1953'), '1950,51,52,53')
self.assertEqual(self.plugin._tmpl_bucket('1974'), '1974')
def test_year_out_of_range(self):
"""If no range match, return the year"""
self._setup_config(bucket_year=['1950-59', '1960-69'])
self.assertEqual(self.plugin._tmpl_bucket('1974'), '1974')
self._setup_config(bucket_year=[])
self.assertEqual(self.plugin._tmpl_bucket('1974'), '1974')
def test_year_out_of_range_extrapolate(self):
"""If no defined range match, extrapolate all ranges using the most
common syntax amongst existing buckets and return the matching one."""
self._setup_config(bucket_year=['1950-59', '1960-69'],
extrapolate=True)
self.assertEqual(self.plugin._tmpl_bucket('1914'), '1910-19')
# pick single year format
self._setup_config(bucket_year=['1962-81', '2002', '2012'],
extrapolate=True)
self.assertEqual(self.plugin._tmpl_bucket('1983'), '1982')
# pick from-end format
self._setup_config(bucket_year=['1962-81', '2002', '2012-14'],
extrapolate=True)
self.assertEqual(self.plugin._tmpl_bucket('1983'), '1982-01')
# extrapolate add ranges, but never modifies existing ones
self._setup_config(bucket_year=['1932', '1942', '1952', '1962-81',
'2002'], extrapolate=True)
self.assertEqual(self.plugin._tmpl_bucket('1975'), '1962-81')
def test_alpha_all_chars(self):
"""Alphabet buckets can be named by listing all their chars"""
self._setup_config(bucket_alpha=['ABCD', 'FGH', 'IJKL'])
self.assertEqual(self.plugin._tmpl_bucket('garry'), 'FGH')
def test_alpha_first_last_chars(self):
"""Alphabet buckets can be named by listing the 'from-to' syntax"""
self._setup_config(bucket_alpha=['0->9', 'A->D', 'F-H', 'I->Z'])
self.assertEqual(self.plugin._tmpl_bucket('garry'), 'F-H')
self.assertEqual(self.plugin._tmpl_bucket('2pac'), '0->9')
def test_alpha_out_of_range(self):
"""If no range match, return the initial"""
self._setup_config(bucket_alpha=['ABCD', 'FGH', 'IJKL'])
self.assertEqual(self.plugin._tmpl_bucket('errol'), 'E')
self._setup_config(bucket_alpha=[])
self.assertEqual(self.plugin._tmpl_bucket('errol'), 'E')
def test_alpha_regex(self):
"""Check regex is used"""
self._setup_config(bucket_alpha=['foo', 'bar'],
bucket_alpha_regex={'foo': '^[a-d]',
'bar': '^[e-z]'})
self.assertEqual(self.plugin._tmpl_bucket('alpha'), 'foo')
self.assertEqual(self.plugin._tmpl_bucket('delta'), 'foo')
self.assertEqual(self.plugin._tmpl_bucket('zeta'), 'bar')
self.assertEqual(self.plugin._tmpl_bucket('Alpha'), 'A')
def test_alpha_regex_mix(self):
"""Check mixing regex and non-regex is possible"""
self._setup_config(bucket_alpha=['A - D', 'E - L'],
bucket_alpha_regex={'A - D': '^[0-9a-dA-D…äÄ]'})
self.assertEqual(self.plugin._tmpl_bucket('alpha'), 'A - D')
self.assertEqual(self.plugin._tmpl_bucket('Ärzte'), 'A - D')
self.assertEqual(self.plugin._tmpl_bucket('112'), 'A - D')
self.assertEqual(self.plugin._tmpl_bucket('…and Oceans'), 'A - D')
self.assertEqual(self.plugin._tmpl_bucket('Eagles'), 'E - L')
def test_bad_alpha_range_def(self):
"""If bad alpha range definition, a UserError is raised."""
with self.assertRaises(ui.UserError):
self._setup_config(bucket_alpha=['$%'])
def test_bad_year_range_def_no4digits(self):
"""If bad year range definition, a UserError is raised.
Range origin must be expressed on 4 digits.
"""
with self.assertRaises(ui.UserError):
self._setup_config(bucket_year=['62-64'])
def test_bad_year_range_def_nodigits(self):
"""If bad year range definition, a UserError is raised.
At least the range origin must be declared.
"""
with self.assertRaises(ui.UserError):
self._setup_config(bucket_year=['nodigits'])
def check_span_from_str(self, sstr, dfrom, dto):
d = bucket.span_from_str(sstr)
self.assertEqual(dfrom, d['from'])
self.assertEqual(dto, d['to'])
def test_span_from_str(self):
self.check_span_from_str("1980 2000", 1980, 2000)
self.check_span_from_str("1980 00", 1980, 2000)
self.check_span_from_str("1930 00", 1930, 2000)
self.check_span_from_str("1930 50", 1930, 1950)
def suite():
return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == '__main__':
unittest.main(defaultTest='suite')
|