diff --git a/.coverage b/.coverage index 20cee1c..849b8dd 100644 Binary files a/.coverage and b/.coverage differ diff --git a/.drone.yml b/.drone.yml index 70aa1a0..285c745 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,7 +8,7 @@ name: run_tests steps: # Run tests against Python/Flask engine backend (with pytest) - - name: coverage + - name: pytest with coverage image: python:3.10.4 commands: # Install dependencies @@ -16,22 +16,7 @@ steps: - pip install poetry - poetry install # Start with a fresh database (which is already running as a service from Drone) - - poetry run pytest --cov-report term --cov=vrobbler tests - environment: - VROBBLER_DATABASE_URL: sqlite:///test.db - volumes: - # Mount pip cache from host - - name: pip_cache - path: /root/.cache/pip - - name: pytest - image: python:3.10.4 - commands: - # Install dependencies - - cp vrobbler.conf.test vrobbler.conf - - pip install poetry - - poetry install - # Start with a fresh database (which is already running as a service from Drone) - - poetry run pytest + - poetry run pytest --cov-report term:skip-covered --cov=vrobbler tests environment: VROBBLER_DATABASE_URL: sqlite:///test.db volumes: diff --git a/tests/scrobbles_tests/conftest.py b/tests/scrobbles_tests/conftest.py index 938cebc..e854911 100644 --- a/tests/scrobbles_tests/conftest.py +++ b/tests/scrobbles_tests/conftest.py @@ -39,8 +39,16 @@ class MopidyRequest: @pytest.fixture -def mopidy_track_request_data(**kwargs): - return MopidyRequest(**kwargs).request_json +def mopidy_track_request_data(): + return MopidyRequest().request_json + + +@pytest.fixture +def mopidy_track_diff_album_request_data(**kwargs): + mb_album_id = "0c56c457-afe1-4679-baab-759ba8dd2a58" + return MopidyRequest( + album="Gold", musicbrainz_album_id=mb_album_id + ).request_json @pytest.fixture diff --git a/tests/scrobbles_tests/test_views.py b/tests/scrobbles_tests/test_views.py index a139e28..2587882 100644 --- a/tests/scrobbles_tests/test_views.py +++ b/tests/scrobbles_tests/test_views.py @@ -38,6 +38,31 @@ def test_scrobble_mopidy_track(client, mopidy_track_request_data): assert scrobble.media_obj.title == "Same in the End" +@pytest.mark.django_db +def test_scrobble_mopidy_same_track_different_album( + client, mopidy_track_request_data, mopidy_track_diff_album_request_data +): + url = reverse('scrobbles:mopidy-websocket') + response = client.post( + url, mopidy_track_request_data, content_type='application/json' + ) + assert response.status_code == 200 + assert response.data == {'scrobble_id': 1} + scrobble = Scrobble.objects.get(id=1) + assert scrobble.media_obj.album.name == "Sublime" + + response = client.post( + url, + mopidy_track_diff_album_request_data, + content_type='application/json', + ) + + scrobble = Scrobble.objects.get(id=2) + assert scrobble.media_obj.__class__ == Track + assert scrobble.media_obj.album.name == "Gold" + assert scrobble.media_obj.title == "Same in the End" + + @pytest.mark.django_db def test_scrobble_mopidy_podcast(client, mopidy_podcast_request_data): url = reverse('scrobbles:mopidy-websocket') diff --git a/vrobbler/apps/music/migrations/0009_alter_track_musicbrainz_id_and_more.py b/vrobbler/apps/music/migrations/0009_alter_track_musicbrainz_id_and_more.py new file mode 100644 index 0000000..c5541c5 --- /dev/null +++ b/vrobbler/apps/music/migrations/0009_alter_track_musicbrainz_id_and_more.py @@ -0,0 +1,22 @@ +# Generated by Django 4.1.5 on 2023-01-19 20:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('music', '0008_alter_track_options'), + ] + + operations = [ + migrations.AlterField( + model_name='track', + name='musicbrainz_id', + field=models.CharField(blank=True, max_length=255, null=True), + ), + migrations.AlterUniqueTogether( + name='track', + unique_together={('album', 'musicbrainz_id')}, + ), + ] diff --git a/vrobbler/apps/music/models.py b/vrobbler/apps/music/models.py index c8f2418..c817e57 100644 --- a/vrobbler/apps/music/models.py +++ b/vrobbler/apps/music/models.py @@ -103,7 +103,10 @@ class Track(ScrobblableMixin): artist = models.ForeignKey(Artist, on_delete=models.DO_NOTHING) album = models.ForeignKey(Album, on_delete=models.DO_NOTHING, **BNULL) - musicbrainz_id = models.CharField(max_length=255, unique=True, **BNULL) + musicbrainz_id = models.CharField(max_length=255, **BNULL) + + class Meta: + unique_together = [['album', 'musicbrainz_id']] def __str__(self): return f"{self.title} by {self.artist}"