summaryrefslogtreecommitdiffstats
path: root/test/plugins/test_bareasc.py
blob: 66d8495e56522b5dd30ae5c00eb26118543ca9f7 (plain)
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
# This file is part of beets.
# Copyright 2021, Graham R. Cobb.

"""Tests for the 'bareasc' plugin."""

import unittest

from beets import logging
from beets.test.helper import TestHelper, capture_stdout


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"] = "#"
        self.load_plugins("bareasc")

        # Add library elements. Note that self.lib.add overrides any "id=<n>"
        # and assigns the next free id number.
        self.add_item(title="with accents", album_id=2, artist="Antonín Dvořák")
        self.add_item(title="without accents", artist="Antonín Dvorak")
        self.add_item(title="with umlaut", album_id=2, artist="Brüggen")
        self.add_item(title="without umlaut or e", artist="Bruggen")
        self.add_item(title="without umlaut with e", artist="Brueggen")

    def test_bareasc_search(self):
        test_cases = [
            (
                "dvorak",
                ["without accents"],
            ),  # Normal search, no accents, not using bare-ASCII match.
            (
                "dvořák",
                ["with accents"],
            ),  # Normal search, with accents, not using bare-ASCII match.
            (
                "#dvorak",
                ["without accents", "with accents"],
            ),  # Bare-ASCII search, no accents.
            (
                "#dvořák",
                ["without accents", "with accents"],
            ),  # Bare-ASCII search, with accents.
            (
                "#dvořäk",
                ["without accents", "with accents"],
            ),  # Bare-ASCII search, with incorrect accent.
            (
                "#Bruggen",
                ["without umlaut or e", "with umlaut"],
            ),  # Bare-ASCII search, with no umlaut.
            (
                "#Brüggen",
                ["without umlaut or e", "with umlaut"],
            ),  # Bare-ASCII search, with umlaut.
        ]

        for query, expected_titles in test_cases:
            with self.subTest(query=query, expected_titles=expected_titles):
                items = self.lib.items(query)
                self.assertListEqual(
                    [item.title for item in items], expected_titles
                )

    def test_bareasc_list_output(self):
        """Bare-ASCII version of list command - check output."""
        with capture_stdout() as output:
            self.run_command("bareasc", "with accents")

        self.assertIn("Antonin Dvorak", output.getvalue())

    def test_bareasc_format_output(self):
        """Bare-ASCII version of list -f command - check output."""
        with capture_stdout() as output:
            self.run_command(
                "bareasc", "with accents", "-f", "$artist:: $title"
            )

        self.assertEqual("Antonin Dvorak:: with accents\n", output.getvalue())


def suite():
    """loader."""
    return unittest.TestLoader().loadTestsFromName(__name__)


if __name__ == "__main__":
    unittest.main(defaultTest="suite")