[scrobbles] Fix sentiment badge always rendering Negative
This commit is contained in:
@ -7,6 +7,7 @@ from music.models import Artist, Track
|
||||
from scrobbles.models import Scrobble
|
||||
|
||||
from vrobbler.apps.scrobbles.utils import (
|
||||
analyze_scrobble_sentiment,
|
||||
deduplicate_scrobbles,
|
||||
timestamp_user_tz_to_utc,
|
||||
)
|
||||
@ -143,3 +144,75 @@ def test_deduplicate_scrobbles_filters_by_media_type(db):
|
||||
|
||||
assert count == 0
|
||||
assert Scrobble.objects.filter(user=user, track=track).count() == 2
|
||||
|
||||
|
||||
def _make_boardgame_scrobble_with_notes(notes):
|
||||
from boardgames.models import BoardGame
|
||||
|
||||
user = User.objects.create(email="notes@example.com")
|
||||
board_game = BoardGame.objects.create(title="Test Board Game")
|
||||
return Scrobble.objects.create(
|
||||
user=user,
|
||||
board_game=board_game,
|
||||
media_type="BoardGame",
|
||||
log={"notes": notes},
|
||||
)
|
||||
|
||||
|
||||
def test_analyze_scrobble_sentiment_positive(db):
|
||||
scrobble = _make_boardgame_scrobble_with_notes(
|
||||
{"1774476716": "This was amazing and delightful, I loved every minute."}
|
||||
)
|
||||
|
||||
analyzed = analyze_scrobble_sentiment(scrobble)
|
||||
|
||||
assert analyzed is True
|
||||
sentiment = scrobble.log["sentiment"]
|
||||
assert sentiment["compound"] > 0
|
||||
assert set(sentiment) == {"neg", "neu", "pos", "compound"}
|
||||
|
||||
|
||||
def test_analyze_scrobble_sentiment_negative(db):
|
||||
scrobble = _make_boardgame_scrobble_with_notes(
|
||||
{"1774476716": "This was terrible, I hated every minute of it."}
|
||||
)
|
||||
|
||||
analyzed = analyze_scrobble_sentiment(scrobble)
|
||||
|
||||
assert analyzed is True
|
||||
assert scrobble.log["sentiment"]["compound"] < 0
|
||||
|
||||
|
||||
def test_analyze_scrobble_sentiment_no_notes_skips(db):
|
||||
scrobble = _make_boardgame_scrobble_with_notes({})
|
||||
|
||||
analyzed = analyze_scrobble_sentiment(scrobble)
|
||||
|
||||
assert analyzed is False
|
||||
assert scrobble.log.get("sentiment") is None
|
||||
|
||||
|
||||
def test_analyze_scrobble_sentiment_skips_when_already_done(db):
|
||||
scrobble = _make_boardgame_scrobble_with_notes(
|
||||
{"1774476716": "This was amazing and delightful."}
|
||||
)
|
||||
scrobble.log["sentiment"] = {"neg": 0, "neu": 1, "pos": 0, "compound": 0.0}
|
||||
scrobble.save()
|
||||
|
||||
analyzed = analyze_scrobble_sentiment(scrobble)
|
||||
|
||||
assert analyzed is False
|
||||
assert scrobble.log["sentiment"]["compound"] == 0.0
|
||||
|
||||
|
||||
def test_analyze_scrobble_sentiment_overwrite(db):
|
||||
scrobble = _make_boardgame_scrobble_with_notes(
|
||||
{"1774476716": "This was amazing and delightful."}
|
||||
)
|
||||
scrobble.log["sentiment"] = {"neg": 0, "neu": 1, "pos": 0, "compound": 0.0}
|
||||
scrobble.save()
|
||||
|
||||
analyzed = analyze_scrobble_sentiment(scrobble, overwrite=True)
|
||||
|
||||
assert analyzed is True
|
||||
assert scrobble.log["sentiment"]["compound"] > 0
|
||||
|
||||
Reference in New Issue
Block a user