| 1 |
from typing import ClassVar |
| 2 |
|
| 3 |
from beets import config, plugins |
| 4 |
from beets.test.helper import PluginTestHelper |
| 5 |
from beetsplug.inline import InlinePlugin |
| 6 |
|
| 7 |
|
| 8 |
class TestInlineRecursion(PluginTestHelper): |
| 9 |
plugin: ClassVar[str] = "inline" |
| 10 |
|
| 11 |
def test_no_recursion_when_inline_shadows_fixed_field(self): |
| 12 |
config["item_fields"] = { |
| 13 |
"track_no": ( |
| 14 |
"f'{disc:02d}-{track:02d}' if disctotal > 1 else f'{track:02d}'" |
| 15 |
) |
| 16 |
} |
| 17 |
|
| 18 |
plugins._instances.clear() |
| 19 |
plugins.load_plugins() |
| 20 |
|
| 21 |
item = self.add_item_fixture( |
| 22 |
artist="Artist", |
| 23 |
album="Album", |
| 24 |
title="Title", |
| 25 |
track=1, |
| 26 |
disc=1, |
| 27 |
disctotal=1, |
| 28 |
) |
| 29 |
|
| 30 |
out = item.evaluate_template("$track_no") |
| 31 |
|
| 32 |
assert out == "01" |
| 33 |
|
| 34 |
def test_inline_function_body_item_field(self): |
| 35 |
plugin = InlinePlugin() |
| 36 |
func = plugin.compile_inline( |
| 37 |
"return track + 1", album=False, field_name="next_track" |
| 38 |
) |
| 39 |
|
| 40 |
item = self.add_item_fixture(track=3) |
| 41 |
assert func(item) == 4 |
| 42 |
|
| 43 |
def test_inline_album_expression_uses_items(self): |
| 44 |
plugin = InlinePlugin() |
| 45 |
func = plugin.compile_inline( |
| 46 |
"len(items)", album=True, field_name="item_count" |
| 47 |
) |
| 48 |
|
| 49 |
album = self.add_album_fixture() |
| 50 |
assert func(album) == len(list(album.items())) |
| 51 |
|
| 52 |
def test_inline_album_expression_uses_items_via_obj(self): |
| 53 |
plugin = InlinePlugin() |
| 54 |
func = plugin.compile_inline( |
| 55 |
"len(db_obj.items())", album=True, field_name="item_count" |
| 56 |
) |
| 57 |
|
| 58 |
album = self.add_album_fixture() |
| 59 |
assert func(album) == len(list(album.items())) |
| 60 |
|
| 61 |
def test_inline_function_body_item_field_via_obj(self): |
| 62 |
plugin = InlinePlugin() |
| 63 |
func = plugin.compile_inline( |
| 64 |
"return db_obj.track + 1", album=False, field_name="next_track" |
| 65 |
) |
| 66 |
|
| 67 |
item = self.add_item_fixture(track=3) |
| 68 |
assert func(item) == 4 |
| 69 |
|
| 70 |
def test_inline_obj_missing(self): |
| 71 |
config["plugins"] = ["inline", "missing"] |
| 72 |
|
| 73 |
config["album_fields"] = {"has_missing": ("bool(db_obj.missing)")} |
| 74 |
|
| 75 |
plugins._instances.clear() |
| 76 |
plugins.load_plugins() |
| 77 |
|
| 78 |
album = self.add_album_fixture(track_count=1) |
| 79 |
album.tracktotal = 3 |
| 80 |
for item in album.items(): |
| 81 |
item.tracktotal = 3 |
| 82 |
item.store() |
| 83 |
album.store() |
| 84 |
assert album._get("has_missing") |
| 85 |
|
| 86 |
def test_inline_function_body_obj(self): |
| 87 |
plugin = InlinePlugin() |
| 88 |
func = plugin.compile_inline( |
| 89 |
"return db_obj.title", album=False, field_name="title_value" |
| 90 |
) |
| 91 |
|
| 92 |
item = self.add_item_fixture(track=3) |
| 93 |
assert func(item) |