From fab7a27e9ff54aecb9c8565ac7b718cb965978a9 Mon Sep 17 00:00:00 2001 From: "Graham R. Cobb" Date: Mon, 15 Mar 2021 12:43:58 +0000 Subject: [PATCH] Add a couple more tests and make lint happy. Signed-off-by: Graham R. Cobb --- beetsplug/bareasc.py | 10 +++++++- test/test_bareasc.py | 54 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/beetsplug/bareasc.py b/beetsplug/bareasc.py index f0c43fdfd..bdae32284 100644 --- a/beetsplug/bareasc.py +++ b/beetsplug/bareasc.py @@ -27,9 +27,14 @@ from unidecode import unidecode class BareascQuery(StringFieldQuery): - """Matches items using bare ASCII, without accents etc.""" + """Compare items using bare ASCII, without accents etc.""" @classmethod def string_match(cls, pattern, val): + """Convert both pattern and string to plain ASCII before matching. + + If pattern is all lower case, also convert string to lower case so + match is also case insensitive + """ # smartcase if pattern.islower(): val = val.lower() @@ -39,12 +44,15 @@ class BareascQuery(StringFieldQuery): class BareascPlugin(BeetsPlugin): + """Plugin to provide bare-ASCII option for beets matching.""" def __init__(self): + """Default prefix for selecting bare-ASCII matching is #.""" super(BareascPlugin, self).__init__() self.config.add({ 'prefix': '#', }) def queries(self): + """Reguster bare-ASCII matching.""" prefix = self.config['prefix'].as_str() return {prefix: BareascQuery} diff --git a/test/test_bareasc.py b/test/test_bareasc.py index 4b004ff62..d49ea4448 100644 --- a/test/test_bareasc.py +++ b/test/test_bareasc.py @@ -14,8 +14,10 @@ from beets import logging class BareascPluginTest(unittest.TestCase, TestHelper): + """Test bare ASCII query matching.""" def setUp(self): + """Set up test environment for bare ASCII query matching.""" self.setup_beets() self.log = logging.getLogger('beets.web') self.config['bareasc']['prefix'] = u'#' @@ -31,22 +33,36 @@ class BareascPluginTest(unittest.TestCase, TestHelper): self.add_item(title=u'with umlaut', album_id=2, artist=u'Brüggen') - self.add_item(title=u'without umlaut', + self.add_item(title=u'without umlaut or e', artist=u'Bruggen') + self.add_item(title=u'without umlaut with e', + artist=u'Brueggen') def test_search_normal_noaccent(self): + """Normal search, no accents, not using bare-ASCII match. + + Finds just the unaccented entry. + """ items = self.lib.items(u'dvorak') self.assertEqual(len(items), 1) self.assertEqual([items[0].title], [u'without accents']) def test_search_normal_accent(self): + """Normal search, with accents, not using bare-ASCII match. + + Finds just the accented entry. + """ items = self.lib.items(u'dvořák') self.assertEqual(len(items), 1) self.assertEqual([items[0].title], [u'with accents']) def test_search_bareasc_noaccent(self): + """Bare-ASCII search, no accents. + + Finds both entries. + """ items = self.lib.items(u'#dvorak') self.assertEqual(len(items), 2) @@ -56,6 +72,10 @@ class BareascPluginTest(unittest.TestCase, TestHelper): ) def test_search_bareasc_accent(self): + """Bare-ASCII search, with accents. + + Finds both entries. + """ items = self.lib.items(u'#dvořák') self.assertEqual(len(items), 2) @@ -64,26 +84,54 @@ class BareascPluginTest(unittest.TestCase, TestHelper): {u'without accents', u'with accents'} ) + def test_search_bareasc_wrong_accent(self): + """Bare-ASCII search, with incorrect accent. + + Finds both entries. + """ + items = self.lib.items(u'#dvořäk') + + self.assertEqual(len(items), 2) + self.assertEqual( + {items[0].title, items[1].title}, + {u'without accents', u'with accents'} + ) + def test_search_bareasc_noumlaut(self): + """Bare-ASCII search, with no umlaut. + + Finds entry with 'u' not 'ue', although German speaker would + normally replace ü with ue. + + This is expected behaviour for this simple plugin. + """ items = self.lib.items(u'#Bruggen') self.assertEqual(len(items), 2) self.assertEqual( {items[0].title, items[1].title}, - {u'without umlaut', u'with umlaut'} + {u'without umlaut or e', u'with umlaut'} ) def test_search_bareasc_umlaut(self): + """Bare-ASCII search, with umlaut. + + Finds entry with 'u' not 'ue', although German speaker would + normally replace ü with ue. + + This is expected behaviour for this simple plugin. + """ items = self.lib.items(u'#Brüggen') self.assertEqual(len(items), 2) self.assertEqual( {items[0].title, items[1].title}, - {u'without umlaut', u'with umlaut'} + {u'without umlaut or e', u'with umlaut'} ) def suite(): + """loader.""" return unittest.TestLoader().loadTestsFromName(__name__) if __name__ == '__main__':