Skip to content

Commit

Permalink
Final function change and test.
Browse files Browse the repository at this point in the history
  • Loading branch information
sheriferson committed Jan 19, 2024
1 parent d09cf71 commit de72fb7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/scrobble/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ def choose_tracks(tracks: list[Track]) -> list[Track]:
pre_selections = ','.join(['"' + track_str + '"' for track_str in track_dict.keys()])
picked_tracks = subprocess.check_output(
f"{gum_path} choose {choices} --no-limit --selected={pre_selections}",
env={'GUM_CHOOSE_HEIGHT': str(len(tracks))},
shell=True,
encoding='UTF-8').rstrip()

return [track for track in tracks if str(track) in picked_tracks]

else:
raise NotImplementedError("Track choosing without charmbracelet/gum installation is not implemented yet.")

Expand Down
48 changes: 45 additions & 3 deletions tests/test__utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import pytest
from scrobble.utils import Config, find_command
import unittest
from unittest.mock import patch

from scrobble.utils import Config, find_command, choose_tracks
from scrobble.musicbrainz import Track
from typing import List


TEST_TRACKS: List[Track] = [
Track(track_title="track1", disc_no=1, track_position=1, track_length=10),
Track(track_title="track1", disc_no=1, track_position=2, track_length=20),
Track(track_title="track1", disc_no=1, track_position=3, track_length=30),
]

def test_valid_config_has_lastfm_api():
test_config = Config(config_path='tests/resources/scrobble_complete_valid.toml')
Expand All @@ -23,4 +34,35 @@ def test_find_command_succeeding():

def test_find_command_returning_none():
bad_command_check = find_command('badfakecommand')
assert bad_command_check is None
assert bad_command_check is None


class TestChooseTracksFunction(unittest.TestCase):
@patch('scrobble.utils.find_command')
@patch('subprocess.check_output')
def test_choose_tracks_with_gum(self, mock_check_output, mock_find_command):
mock_find_command.side_effect = lambda *args, **kwargs: '/path/to/gum'
mock_check_output.return_value = f"{str(TEST_TRACKS[0])},{str(TEST_TRACKS[1])}"

# Call the function
result = choose_tracks(TEST_TRACKS)

# Assert that the subprocess.check_output was called with the correct arguments
mock_check_output.assert_called_once_with(
f'/path/to/gum choose "{str(TEST_TRACKS[0])}" "{str(TEST_TRACKS[1])}" "{str(TEST_TRACKS[2])}"'
f' --no-limit --selected="{str(TEST_TRACKS[0])}","{str(TEST_TRACKS[1])}","{str(TEST_TRACKS[2])}"',
env={'GUM_CHOOSE_HEIGHT': '3'},
shell=True,
encoding='UTF-8'
)

# Assert that the result is as expected
self.assertEqual(result, [TEST_TRACKS[0], TEST_TRACKS[1]])

@patch('scrobble.utils.find_command')
def test_choose_tracks_without_gum(self, mock_find_command):
mock_find_command.return_value = None

# Call the function and expect NotImplementedError to be raised
with self.assertRaises(NotImplementedError):
choose_tracks(TEST_TRACKS)

0 comments on commit de72fb7

Please sign in to comment.