[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 scrobbles.models import Scrobble
|
||||||
|
|
||||||
from vrobbler.apps.scrobbles.utils import (
|
from vrobbler.apps.scrobbles.utils import (
|
||||||
|
analyze_scrobble_sentiment,
|
||||||
deduplicate_scrobbles,
|
deduplicate_scrobbles,
|
||||||
timestamp_user_tz_to_utc,
|
timestamp_user_tz_to_utc,
|
||||||
)
|
)
|
||||||
@ -143,3 +144,75 @@ def test_deduplicate_scrobbles_filters_by_media_type(db):
|
|||||||
|
|
||||||
assert count == 0
|
assert count == 0
|
||||||
assert Scrobble.objects.filter(user=user, track=track).count() == 2
|
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
|
||||||
|
|||||||
@ -248,10 +248,11 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
{% with notes_html=object.logdata.notes_as_html %}
|
{% with notes_html=object.logdata.notes_as_html sentiment=object.log.sentiment %}
|
||||||
{% if notes_html %}
|
{% if notes_html %}
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<h4>Notes</h4>
|
<h4>Notes</h4>
|
||||||
|
{% if sentiment %}
|
||||||
<span class="badge fs-8
|
<span class="badge fs-8
|
||||||
{% if sentiment.compound >= 0.5 %}bg-success
|
{% if sentiment.compound >= 0.5 %}bg-success
|
||||||
{% elif sentiment.compound >= 0.05 %}bg-info text-dark
|
{% elif sentiment.compound >= 0.05 %}bg-info text-dark
|
||||||
@ -266,6 +267,7 @@
|
|||||||
{% else %}Negative
|
{% else %}Negative
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</span>
|
</span>
|
||||||
|
{% endif %}
|
||||||
<div class="notes-list">
|
<div class="notes-list">
|
||||||
{{ notes_html|safe }}
|
{{ notes_html|safe }}
|
||||||
</div>
|
</div>
|
||||||
@ -273,13 +275,6 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
|
|
||||||
{% with sentiment=object.log.sentiment %}
|
|
||||||
{% if sentiment %}
|
|
||||||
<div class="mb-3">
|
|
||||||
</div>
|
|
||||||
{% endif %}
|
|
||||||
{% endwith %}
|
|
||||||
|
|
||||||
{% if object.logdata.avg_seconds_per_page %}
|
{% if object.logdata.avg_seconds_per_page %}
|
||||||
<p>Rate: {{object.logdata.avg_seconds_per_page}}s per page</p>
|
<p>Rate: {{object.logdata.avg_seconds_per_page}}s per page</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|||||||
@ -164,10 +164,11 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
{% with notes_html=object.public_logdata.notes_as_html %}
|
{% with notes_html=object.public_logdata.notes_as_html sentiment=object.public_log.sentiment %}
|
||||||
{% if notes_html %}
|
{% if notes_html %}
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<h4>Notes</h4>
|
<h4>Notes</h4>
|
||||||
|
{% if sentiment %}
|
||||||
<span class="badge fs-8
|
<span class="badge fs-8
|
||||||
{% if sentiment.compound >= 0.5 %}bg-success
|
{% if sentiment.compound >= 0.5 %}bg-success
|
||||||
{% elif sentiment.compound >= 0.05 %}bg-info text-dark
|
{% elif sentiment.compound >= 0.05 %}bg-info text-dark
|
||||||
@ -182,6 +183,7 @@
|
|||||||
{% else %}Negative
|
{% else %}Negative
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</span>
|
</span>
|
||||||
|
{% endif %}
|
||||||
<div class="notes-list">
|
<div class="notes-list">
|
||||||
{{ notes_html|safe }}
|
{{ notes_html|safe }}
|
||||||
</div>
|
</div>
|
||||||
@ -189,13 +191,6 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
|
|
||||||
{% with sentiment=object.public_log.sentiment %}
|
|
||||||
{% if sentiment %}
|
|
||||||
<div class="mb-3">
|
|
||||||
</div>
|
|
||||||
{% endif %}
|
|
||||||
{% endwith %}
|
|
||||||
|
|
||||||
{% if object.public_logdata.avg_seconds_per_page %}
|
{% if object.public_logdata.avg_seconds_per_page %}
|
||||||
<p>Rate: {{object.public_logdata.avg_seconds_per_page}}s per page</p>
|
<p>Rate: {{object.public_logdata.avg_seconds_per_page}}s per page</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|||||||
Reference in New Issue
Block a user