Debian Codegraph @

test/util/test_diff.py

raw
1 from textwrap import dedent
2
3 import pytest
4
5 from beets.library import Item
6 from beets.util.diff import _field_diff
7
8 p = pytest.param
9
10
11 class TestFieldDiff:
12 @pytest.fixture(autouse=True)
13 def configure_color(self, config, color):
14 config["ui"]["color"] = color
15
16 @pytest.fixture(autouse=True)
17 def patch_colorize(self, monkeypatch):
18 """Patch to return a deterministic string format instead of ANSI codes."""
19 monkeypatch.setattr(
20 "beets.util.color._colorize",
21 lambda color_name, text: f"[{color_name}]{text}[/]",
22 )
23
24 @staticmethod
25 def diff_fmt(old, new):
26 return f"[text_diff_removed]{old}[/] -> [text_diff_added]{new}[/]"
27
28 @pytest.mark.parametrize(
29 "old_data, new_data, field, expected_diff",
30 [
31 p({"title": "foo"}, {"title": "foo"}, "title", None, id="no_change"),
32 p({"bpm": 120.0}, {"bpm": 120.005}, "bpm", None, id="float_close_enough"),
33 p({"bpm": 120.0}, {"bpm": 121.0}, "bpm", f"bpm: {diff_fmt('120', '121')}", id="float_changed"),
34 p({"title": "foo"}, {"title": "bar"}, "title", f"title: {diff_fmt('foo', 'bar')}", id="string_full_replace"),
35 p({"title": "prefix foo"}, {"title": "prefix bar"}, "title", "title: prefix [text_diff_removed]foo[/] -> prefix [text_diff_added]bar[/]", id="string_partial_change"),
36 p({"year": 2000}, {"year": 2001}, "year", f"year: {diff_fmt('2000', '2001')}", id="int_changed"),
37 p({}, {"artist": "Artist"}, "artist", "artist: -> [text_diff_added]Artist[/]", id="field_added"),
38 p({"artist": "Artist"}, {}, "artist", "artist: [text_diff_removed]Artist[/] -> ", id="field_removed"),
39 p({"track": 1}, {"track": 2}, "track", f"track: {diff_fmt('01', '02')}", id="formatted_value_changed"),
40 p({"mb_trackid": None}, {"mb_trackid": "1234"}, "mb_trackid", "mb_trackid: -> [text_diff_added]1234[/]", id="none_to_value"),
41 p({}, {"new_flex": "foo"}, "new_flex", "[text_diff_added]new_flex: foo[/]", id="flex_field_added"),
42 p({"old_flex": "foo"}, {}, "old_flex", "[text_diff_removed]old_flex: foo[/]", id="flex_field_removed"),
43 p({"albumtypes": ["album", "ep"]}, {"albumtypes": ["ep", "album"]}, "albumtypes", None, id="multi_value_unchanged"),
44 p(
45 {"albumtypes": ["ep"]},
46 {"albumtypes": ["album", "compilation"]},
47 "albumtypes",
48 dedent("""
49 albumtypes:
50 [text_diff_removed] - ep[/]
51 [text_diff_added] + album[/]
52 [text_diff_added] + compilation[/]
53 """).strip(),
54 id="multi_value_changed"
55 ),
56 ],
57 ) # fmt: skip
58 @pytest.mark.parametrize("color", [True], ids=["color_enabled"])
59 def test_field_diff_colors(self, old_data, new_data, field, expected_diff):
60 old_item = Item(**old_data)
61 new_item = Item(**new_data)
62
63 diff = _field_diff(field, old_item.formatted(), new_item.formatted())
64
65 assert diff == expected_diff
66
67 @pytest.mark.parametrize("color", [False], ids=["color_disabled"])
68 def test_field_diff_no_color(self):
69 old_item = Item(title="foo")
70 new_item = Item(title="bar")
71
72 diff = _field_diff("title", old_item.formatted(), new_item.formatted())
73
74 assert diff == "title: foo -> bar"