|
| 1 | +"""Tests for default sorting in list commands.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import json |
| 5 | +import tempfile |
| 6 | +import os |
| 7 | +import datetime |
| 8 | +from click.testing import CliRunner |
| 9 | +from scrobbledb.commands import albums, tracks, artists |
| 10 | +import sqlite_utils |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def runner(): |
| 14 | + return CliRunner() |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def temp_db(): |
| 18 | + fd, path = tempfile.mkstemp(suffix=".db") |
| 19 | + os.close(fd) |
| 20 | + db = sqlite_utils.Database(path) |
| 21 | + |
| 22 | + # Setup schema |
| 23 | + db.execute(""" |
| 24 | + CREATE TABLE artists ( |
| 25 | + id TEXT PRIMARY KEY, |
| 26 | + name TEXT |
| 27 | + ) |
| 28 | + """) |
| 29 | + db.execute(""" |
| 30 | + CREATE TABLE albums ( |
| 31 | + id TEXT PRIMARY KEY, |
| 32 | + title TEXT, |
| 33 | + artist_id TEXT, |
| 34 | + FOREIGN KEY(artist_id) REFERENCES artists(id) |
| 35 | + ) |
| 36 | + """) |
| 37 | + db.execute(""" |
| 38 | + CREATE TABLE tracks ( |
| 39 | + id TEXT PRIMARY KEY, |
| 40 | + title TEXT, |
| 41 | + album_id TEXT, |
| 42 | + FOREIGN KEY(album_id) REFERENCES albums(id) |
| 43 | + ) |
| 44 | + """) |
| 45 | + db.execute(""" |
| 46 | + CREATE TABLE plays ( |
| 47 | + track_id TEXT, |
| 48 | + timestamp TEXT, |
| 49 | + FOREIGN KEY(track_id) REFERENCES tracks(id) |
| 50 | + ) |
| 51 | + """) |
| 52 | + |
| 53 | + # Insert data |
| 54 | + # Artist A: 10 plays, last played 2024-01-01 |
| 55 | + # Artist B: 5 plays, last played 2024-02-01 (More recent, fewer plays) |
| 56 | + |
| 57 | + db["artists"].insert_all([ |
| 58 | + {"id": "art-a", "name": "Artist A"}, |
| 59 | + {"id": "art-b", "name": "Artist B"}, |
| 60 | + ]) |
| 61 | + |
| 62 | + db["albums"].insert_all([ |
| 63 | + {"id": "alb-a", "title": "Album A", "artist_id": "art-a"}, |
| 64 | + {"id": "alb-b", "title": "Album B", "artist_id": "art-b"}, |
| 65 | + ]) |
| 66 | + |
| 67 | + db["tracks"].insert_all([ |
| 68 | + {"id": "trk-a", "title": "Track A", "album_id": "alb-a"}, |
| 69 | + {"id": "trk-b", "title": "Track B", "album_id": "alb-b"}, |
| 70 | + ]) |
| 71 | + |
| 72 | + # Plays for A (10 plays, old) |
| 73 | + for i in range(10): |
| 74 | + db["plays"].insert({ |
| 75 | + "track_id": "trk-a", |
| 76 | + "timestamp": f"2024-01-01T12:00:0{i}" |
| 77 | + }) |
| 78 | + |
| 79 | + # Plays for B (5 plays, recent) |
| 80 | + for i in range(5): |
| 81 | + db["plays"].insert({ |
| 82 | + "track_id": "trk-b", |
| 83 | + "timestamp": f"2024-02-01T12:00:0{i}" |
| 84 | + }) |
| 85 | + |
| 86 | + yield path |
| 87 | + |
| 88 | + db.close() |
| 89 | + if os.path.exists(path): |
| 90 | + os.unlink(path) |
| 91 | + |
| 92 | +def test_albums_list_default_sort(runner, temp_db): |
| 93 | + """Test that albums list defaults to sorting by recent (last played).""" |
| 94 | + result = runner.invoke(albums.albums, ["list", "--database", temp_db, "--format", "json"]) |
| 95 | + assert result.exit_code == 0 |
| 96 | + data = json.loads(result.output) |
| 97 | + |
| 98 | + # Should be sorted by recent: Album B (Feb) then Album A (Jan) |
| 99 | + assert len(data) == 2 |
| 100 | + assert data[0]["album_title"] == "Album B" |
| 101 | + assert data[1]["album_title"] == "Album A" |
| 102 | + |
| 103 | +def test_artists_list_default_sort(runner, temp_db): |
| 104 | + """Test that artists list defaults to sorting by recent (last played).""" |
| 105 | + result = runner.invoke(artists.artists, ["list", "--database", temp_db, "--format", "json"]) |
| 106 | + assert result.exit_code == 0 |
| 107 | + data = json.loads(result.output) |
| 108 | + |
| 109 | + # Should be sorted by recent: Artist B (Feb) then Artist A (Jan) |
| 110 | + assert len(data) == 2 |
| 111 | + assert data[0]["artist_name"] == "Artist B" |
| 112 | + assert data[1]["artist_name"] == "Artist A" |
| 113 | + |
| 114 | +def test_tracks_list_default_sort(runner, temp_db): |
| 115 | + """Test that tracks list defaults to sorting by recent (last played).""" |
| 116 | + result = runner.invoke(tracks.tracks, ["list", "--database", temp_db, "--format", "json"]) |
| 117 | + assert result.exit_code == 0 |
| 118 | + data = json.loads(result.output) |
| 119 | + |
| 120 | + # Should be sorted by recent: Track B (Feb) then Track A (Jan) |
| 121 | + assert len(data) == 2 |
| 122 | + assert data[0]["track_title"] == "Track B" |
| 123 | + assert data[1]["track_title"] == "Track A" |
| 124 | + |
| 125 | +def test_plays_list_default_sort(runner, temp_db): |
| 126 | + """Test that plays list defaults to sorting by recent (last played).""" |
| 127 | + from scrobbledb.commands import plays |
| 128 | + result = runner.invoke(plays.plays, ["list", "--database", temp_db, "--format", "json"]) |
| 129 | + assert result.exit_code == 0 |
| 130 | + data = json.loads(result.output) |
| 131 | + |
| 132 | + # Should be sorted by recent: Feb plays then Jan plays |
| 133 | + # We inserted 5 Feb plays (Track B) and 10 Jan plays (Track A) |
| 134 | + # Total 15 plays. |
| 135 | + # The most recent should be from Feb. |
| 136 | + assert len(data) == 15 |
| 137 | + assert "2024-02-01" in data[0]["timestamp"] |
| 138 | + assert "2024-01-01" in data[-1]["timestamp"] |
0 commit comments