Skip to content

Commit

Permalink
add tests for artiststr
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Oct 11, 2023
1 parent 7c4b2ae commit 2f16598
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
18 changes: 9 additions & 9 deletions rose/artiststr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import re
from dataclasses import dataclass
from dataclasses import dataclass, field

TAG_SPLITTER_REGEX = re.compile(r" \\\\ | / |; ?| vs\. ")


@dataclass
class Artists:
main: list[str]
guest: list[str]
remixer: list[str]
producer: list[str]
composer: list[str]
djmixer: list[str]
main: list[str] = field(default_factory=list)
guest: list[str] = field(default_factory=list)
remixer: list[str] = field(default_factory=list)
producer: list[str] = field(default_factory=list)
composer: list[str] = field(default_factory=list)
djmixer: list[str] = field(default_factory=list)


def parse_artist_string(
Expand Down Expand Up @@ -39,7 +39,7 @@ def _split_tag(t: str | None) -> list[str]:
dj, main = re.split(r" ?pres. ", main, maxsplit=1)
li_dj.extend(_split_tag(dj))
if main and " performed by " in main:
composer, main = re.split(r" ?performed by. ", main, maxsplit=1)
composer, main = re.split(r" ?performed by ", main, maxsplit=1)
li_composer.extend(_split_tag(composer))
if main:
li_main.extend(_split_tag(main))
Expand All @@ -57,7 +57,7 @@ def _split_tag(t: str | None) -> list[str]:
def format_artist_string(a: Artists, genres: list[str]) -> str:
r = ";".join(a.producer + a.main + a.remixer)
if a.composer and "Classical" in genres:
r = ";".join(a.composer) + " performed by. " + r
r = ";".join(a.composer) + " performed by " + r
if a.djmixer:
r = ";".join(a.djmixer) + " pres. " + r
if a.guest:
Expand Down
35 changes: 35 additions & 0 deletions rose/artiststr/__test__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from rose.artiststr import Artists, format_artist_string, parse_artist_string


def test_parse_artist_string() -> None:
assert parse_artist_string("A;B feat. C;D") == Artists(
main=["A", "B"],
guest=["C", "D"],
)
assert parse_artist_string("A pres. C;D") == Artists(
djmixer=["A"],
main=["C", "D"],
)
assert parse_artist_string("A performed by C;D") == Artists(
composer=["A"],
main=["C", "D"],
)
assert parse_artist_string("A pres. B;C feat. D;E") == Artists(
djmixer=["A"],
main=["B", "C"],
guest=["D", "E"],
)


def test_format_artist_string() -> None:
assert format_artist_string(Artists(main=["A", "B"], guest=["C", "D"]), []) == "A;B feat. C;D"
assert format_artist_string(Artists(djmixer=["A"], main=["C", "D"]), []) == "A pres. C;D"
assert format_artist_string(Artists(composer=["A"], main=["C", "D"]), []) == "C;D"
assert (
format_artist_string(Artists(composer=["A"], main=["C", "D"]), ["Classical"])
== "A performed by C;D"
)
assert (
format_artist_string(Artists(djmixer=["A"], main=["B", "C"], guest=["D", "E"]), [])
== "A pres. B;C feat. D;E"
)

0 comments on commit 2f16598

Please sign in to comment.