| 1 |
import time |
| 2 |
from datetime import datetime |
| 3 |
|
| 4 |
import pytest |
| 5 |
from confuse import ConfigValueError |
| 6 |
|
| 7 |
from beets.test.helper import IOMixin, PluginTestCase |
| 8 |
|
| 9 |
|
| 10 |
class TypesPluginTest(IOMixin, PluginTestCase): |
| 11 |
plugin = "types" |
| 12 |
|
| 13 |
def test_integer_modify_and_query(self): |
| 14 |
self.config["types"] = {"myint": "int"} |
| 15 |
item = self.add_item(artist="aaa") |
| 16 |
|
| 17 |
# Do not match unset values |
| 18 |
out = self.list("myint:1..3") |
| 19 |
assert "" == out |
| 20 |
|
| 21 |
self.modify("myint=2") |
| 22 |
item.load() |
| 23 |
assert item["myint"] == 2 |
| 24 |
|
| 25 |
# Match in range |
| 26 |
out = self.list("myint:1..3") |
| 27 |
assert "aaa" in out |
| 28 |
|
| 29 |
def test_album_integer_modify_and_query(self): |
| 30 |
self.config["types"] = {"myint": "int"} |
| 31 |
album = self.add_album(albumartist="aaa") |
| 32 |
|
| 33 |
# Do not match unset values |
| 34 |
out = self.list_album("myint:1..3") |
| 35 |
assert "" == out |
| 36 |
|
| 37 |
self.modify("-a", "myint=2") |
| 38 |
album.load() |
| 39 |
assert album["myint"] == 2 |
| 40 |
|
| 41 |
# Match in range |
| 42 |
out = self.list_album("myint:1..3") |
| 43 |
assert "aaa" in out |
| 44 |
|
| 45 |
def test_float_modify_and_query(self): |
| 46 |
self.config["types"] = {"myfloat": "float"} |
| 47 |
item = self.add_item(artist="aaa") |
| 48 |
|
| 49 |
# Do not match unset values |
| 50 |
out = self.list("myfloat:10..0") |
| 51 |
assert "" == out |
| 52 |
|
| 53 |
self.modify("myfloat=-9.1") |
| 54 |
item.load() |
| 55 |
assert item["myfloat"] == -9.1 |
| 56 |
|
| 57 |
# Match in range |
| 58 |
out = self.list("myfloat:-10..0") |
| 59 |
assert "aaa" in out |
| 60 |
|
| 61 |
def test_bool_modify_and_query(self): |
| 62 |
self.config["types"] = {"mybool": "bool"} |
| 63 |
true = self.add_item(artist="true") |
| 64 |
false = self.add_item(artist="false") |
| 65 |
self.add_item(artist="unset") |
| 66 |
|
| 67 |
# Do not match unset values |
| 68 |
out = self.list("mybool:true, mybool:false") |
| 69 |
assert "" == out |
| 70 |
|
| 71 |
# Set true |
| 72 |
self.modify("mybool=1", "artist:true") |
| 73 |
true.load() |
| 74 |
assert true["mybool"] |
| 75 |
|
| 76 |
# Set false |
| 77 |
self.modify("mybool=false", "artist:false") |
| 78 |
false.load() |
| 79 |
assert not false["mybool"] |
| 80 |
|
| 81 |
# Query bools |
| 82 |
out = self.list("mybool:true", "$artist $mybool") |
| 83 |
assert "true True" == out |
| 84 |
|
| 85 |
out = self.list("mybool:false", "$artist $mybool") |
| 86 |
|
| 87 |
# Dealing with unset fields? |
| 88 |
# assert 'false False' == out |
| 89 |
# out = self.list('mybool:', '$artist $mybool') |
| 90 |
# assert 'unset $mybool' in out |
| 91 |
|
| 92 |
def test_date_modify_and_query(self): |
| 93 |
self.config["types"] = {"mydate": "date"} |
| 94 |
# FIXME parsing should also work with default time format |
| 95 |
self.config["time_format"] = "%Y-%m-%d" |
| 96 |
old = self.add_item(artist="prince") |
| 97 |
new = self.add_item(artist="britney") |
| 98 |
|
| 99 |
# Do not match unset values |
| 100 |
out = self.list("mydate:..2000") |
| 101 |
assert "" == out |
| 102 |
|
| 103 |
self.modify("mydate=1999-01-01", "artist:prince") |
| 104 |
old.load() |
| 105 |
assert old["mydate"] == mktime(1999, 1, 1) |
| 106 |
|
| 107 |
self.modify("mydate=1999-12-30", "artist:britney") |
| 108 |
new.load() |
| 109 |
assert new["mydate"] == mktime(1999, 12, 30) |
| 110 |
|
| 111 |
# Match in range |
| 112 |
out = self.list("mydate:..1999-07", "$artist $mydate") |
| 113 |
assert "prince 1999-01-01" == out |
| 114 |
|
| 115 |
# FIXME some sort of timezone issue here |
| 116 |
# out = self.list('mydate:1999-12-30', '$artist $mydate') |
| 117 |
# assert 'britney 1999-12-30' == out |
| 118 |
|
| 119 |
def test_unknown_type_error(self): |
| 120 |
self.config["types"] = {"flex": "unkown type"} |
| 121 |
with pytest.raises(ConfigValueError): |
| 122 |
self.add_item(flex="test") |
| 123 |
|
| 124 |
def test_template_if_def(self): |
| 125 |
# Tests for a subtle bug when using %ifdef in templates along with |
| 126 |
# types that have truthy default values (e.g. '0', '0.0', 'False') |
| 127 |
# https://github.com/beetbox/beets/issues/3852 |
| 128 |
self.config["types"] = { |
| 129 |
"playcount": "int", |
| 130 |
"rating": "float", |
| 131 |
"starred": "bool", |
| 132 |
} |
| 133 |
|
| 134 |
with_fields = self.add_item(artist="prince") |
| 135 |
self.modify("playcount=10", "artist=prince") |
| 136 |
self.modify("rating=5.0", "artist=prince") |
| 137 |
self.modify("starred=yes", "artist=prince") |
| 138 |
with_fields.load() |
| 139 |
|
| 140 |
without_fields = self.add_item(artist="britney") |
| 141 |
|
| 142 |
int_template = "%ifdef{playcount,Play count: $playcount,Not played}" |
| 143 |
assert with_fields.evaluate_template(int_template) == "Play count: 10" |
| 144 |
assert without_fields.evaluate_template(int_template) == "Not played" |
| 145 |
|
| 146 |
float_template = "%ifdef{rating,Rating: $rating,Not rated}" |
| 147 |
assert with_fields.evaluate_template(float_template) == "Rating: 5.0" |
| 148 |
assert without_fields.evaluate_template(float_template) == "Not rated" |
| 149 |
|
| 150 |
bool_template = "%ifdef{starred,Starred: $starred,Not starred}" |
| 151 |
assert with_fields.evaluate_template(bool_template).lower() in ( |
| 152 |
"starred: true", |
| 153 |
"starred: yes", |
| 154 |
"starred: y", |
| 155 |
) |
| 156 |
assert without_fields.evaluate_template(bool_template) == "Not starred" |
| 157 |
|
| 158 |
def modify(self, *args): |
| 159 |
return self.run_with_output( |
| 160 |
"modify", "--yes", "--nowrite", "--nomove", *args |
| 161 |
) |
| 162 |
|
| 163 |
def list(self, query, fmt="$artist - $album - $title"): |
| 164 |
return self.run_with_output("ls", "-f", fmt, query).strip() |
| 165 |
|
| 166 |
def list_album(self, query, fmt="$albumartist - $album - $title"): |
| 167 |
return self.run_with_output("ls", "-a", "-f", fmt, query).strip() |
| 168 |
|
| 169 |
|
| 170 |
def mktime(*args): |
| 171 |
return time.mktime(datetime(*args).timetuple()) |