1
0

test_playlist_manager.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. """
  2. Unit tests for playlist_manager CRUD operations.
  3. Tests the core playlist management functions:
  4. - Loading playlists from file
  5. - Creating playlists
  6. - Getting playlists
  7. - Modifying playlists
  8. - Deleting playlists
  9. - Listing playlists
  10. - Renaming playlists
  11. """
  12. import json
  13. import pytest
  14. from unittest.mock import patch, MagicMock
  15. class TestPlaylistManagerCRUD:
  16. """Tests for playlist CRUD operations."""
  17. @pytest.fixture
  18. def playlists_file(self, tmp_path):
  19. """Create a temporary playlists.json file."""
  20. file_path = tmp_path / "playlists.json"
  21. file_path.write_text("{}")
  22. return str(file_path)
  23. @pytest.fixture
  24. def playlist_manager_patched(self, playlists_file):
  25. """Patch PLAYLISTS_FILE to use temporary file."""
  26. with patch("modules.core.playlist_manager.PLAYLISTS_FILE", playlists_file):
  27. # Need to re-import to get patched version
  28. from modules.core import playlist_manager
  29. yield playlist_manager
  30. def test_load_playlists_empty_file(self, playlists_file, playlist_manager_patched):
  31. """Test loading playlists from an empty file returns empty dict."""
  32. result = playlist_manager_patched.load_playlists()
  33. assert result == {}
  34. def test_load_playlists_with_data(self, playlists_file, playlist_manager_patched):
  35. """Test loading playlists with existing data."""
  36. # Write some data to the file
  37. with open(playlists_file, "w") as f:
  38. json.dump({"my_playlist": ["pattern1.thr", "pattern2.thr"]}, f)
  39. result = playlist_manager_patched.load_playlists()
  40. assert "my_playlist" in result
  41. assert result["my_playlist"] == ["pattern1.thr", "pattern2.thr"]
  42. def test_create_playlist(self, playlists_file, playlist_manager_patched):
  43. """Test creating a new playlist."""
  44. files = ["circle.thr", "spiral.thr"]
  45. result = playlist_manager_patched.create_playlist("test_playlist", files)
  46. assert result is True
  47. # Verify it was saved
  48. playlists = playlist_manager_patched.load_playlists()
  49. assert "test_playlist" in playlists
  50. assert playlists["test_playlist"] == files
  51. def test_create_playlist_overwrites_existing(self, playlists_file, playlist_manager_patched):
  52. """Test creating a playlist with existing name overwrites it."""
  53. # Create initial playlist
  54. playlist_manager_patched.create_playlist("test_playlist", ["old.thr"])
  55. # Create again with same name
  56. playlist_manager_patched.create_playlist("test_playlist", ["new.thr"])
  57. playlists = playlist_manager_patched.load_playlists()
  58. assert playlists["test_playlist"] == ["new.thr"]
  59. def test_get_playlist_exists(self, playlists_file, playlist_manager_patched):
  60. """Test getting an existing playlist."""
  61. playlist_manager_patched.create_playlist("my_playlist", ["a.thr", "b.thr"])
  62. result = playlist_manager_patched.get_playlist("my_playlist")
  63. assert result is not None
  64. assert result["name"] == "my_playlist"
  65. assert result["files"] == ["a.thr", "b.thr"]
  66. def test_get_playlist_not_found(self, playlists_file, playlist_manager_patched):
  67. """Test getting a non-existent playlist returns None."""
  68. result = playlist_manager_patched.get_playlist("nonexistent")
  69. assert result is None
  70. def test_modify_playlist(self, playlists_file, playlist_manager_patched):
  71. """Test modifying an existing playlist."""
  72. # Create initial playlist
  73. playlist_manager_patched.create_playlist("my_playlist", ["old.thr"])
  74. # Modify it
  75. new_files = ["new1.thr", "new2.thr", "new3.thr"]
  76. result = playlist_manager_patched.modify_playlist("my_playlist", new_files)
  77. assert result is True
  78. # Verify changes
  79. playlist = playlist_manager_patched.get_playlist("my_playlist")
  80. assert playlist["files"] == new_files
  81. def test_delete_playlist(self, playlists_file, playlist_manager_patched):
  82. """Test deleting a playlist."""
  83. # Create a playlist
  84. playlist_manager_patched.create_playlist("to_delete", ["pattern.thr"])
  85. # Delete it
  86. result = playlist_manager_patched.delete_playlist("to_delete")
  87. assert result is True
  88. # Verify it's gone
  89. playlist = playlist_manager_patched.get_playlist("to_delete")
  90. assert playlist is None
  91. def test_delete_playlist_not_found(self, playlists_file, playlist_manager_patched):
  92. """Test deleting a non-existent playlist returns False."""
  93. result = playlist_manager_patched.delete_playlist("nonexistent")
  94. assert result is False
  95. def test_list_all_playlists(self, playlists_file, playlist_manager_patched):
  96. """Test listing all playlist names."""
  97. # Create multiple playlists
  98. playlist_manager_patched.create_playlist("playlist1", ["a.thr"])
  99. playlist_manager_patched.create_playlist("playlist2", ["b.thr"])
  100. playlist_manager_patched.create_playlist("playlist3", ["c.thr"])
  101. result = playlist_manager_patched.list_all_playlists()
  102. assert len(result) == 3
  103. assert "playlist1" in result
  104. assert "playlist2" in result
  105. assert "playlist3" in result
  106. def test_list_all_playlists_empty(self, playlists_file, playlist_manager_patched):
  107. """Test listing playlists when none exist."""
  108. result = playlist_manager_patched.list_all_playlists()
  109. assert result == []
  110. def test_add_to_playlist(self, playlists_file, playlist_manager_patched):
  111. """Test adding a pattern to an existing playlist."""
  112. # Create playlist
  113. playlist_manager_patched.create_playlist("my_playlist", ["existing.thr"])
  114. # Add pattern
  115. result = playlist_manager_patched.add_to_playlist("my_playlist", "new_pattern.thr")
  116. assert result is True
  117. # Verify
  118. playlist = playlist_manager_patched.get_playlist("my_playlist")
  119. assert "new_pattern.thr" in playlist["files"]
  120. assert len(playlist["files"]) == 2
  121. def test_add_to_playlist_not_found(self, playlists_file, playlist_manager_patched):
  122. """Test adding to a non-existent playlist returns False."""
  123. result = playlist_manager_patched.add_to_playlist("nonexistent", "pattern.thr")
  124. assert result is False
  125. class TestPlaylistRename:
  126. """Tests for playlist rename functionality."""
  127. @pytest.fixture
  128. def playlists_file(self, tmp_path):
  129. """Create a temporary playlists.json file."""
  130. file_path = tmp_path / "playlists.json"
  131. file_path.write_text("{}")
  132. return str(file_path)
  133. @pytest.fixture
  134. def playlist_manager_patched(self, playlists_file):
  135. """Patch PLAYLISTS_FILE to use temporary file."""
  136. with patch("modules.core.playlist_manager.PLAYLISTS_FILE", playlists_file):
  137. from modules.core import playlist_manager
  138. yield playlist_manager
  139. def test_rename_playlist_success(self, playlists_file, playlist_manager_patched):
  140. """Test successfully renaming a playlist."""
  141. # Create initial playlist
  142. playlist_manager_patched.create_playlist("old_name", ["a.thr", "b.thr"])
  143. # Rename it
  144. success, message = playlist_manager_patched.rename_playlist("old_name", "new_name")
  145. assert success is True
  146. assert "new_name" in message
  147. # Verify old name is gone
  148. assert playlist_manager_patched.get_playlist("old_name") is None
  149. # Verify new name exists with same files
  150. new_playlist = playlist_manager_patched.get_playlist("new_name")
  151. assert new_playlist is not None
  152. assert new_playlist["files"] == ["a.thr", "b.thr"]
  153. def test_rename_playlist_not_found(self, playlists_file, playlist_manager_patched):
  154. """Test renaming a non-existent playlist."""
  155. success, message = playlist_manager_patched.rename_playlist("nonexistent", "new_name")
  156. assert success is False
  157. assert "not found" in message.lower()
  158. def test_rename_playlist_empty_name(self, playlists_file, playlist_manager_patched):
  159. """Test renaming with empty name fails."""
  160. playlist_manager_patched.create_playlist("my_playlist", ["a.thr"])
  161. success, message = playlist_manager_patched.rename_playlist("my_playlist", "")
  162. assert success is False
  163. assert "empty" in message.lower()
  164. def test_rename_playlist_whitespace_name(self, playlists_file, playlist_manager_patched):
  165. """Test renaming with whitespace-only name fails."""
  166. playlist_manager_patched.create_playlist("my_playlist", ["a.thr"])
  167. success, message = playlist_manager_patched.rename_playlist("my_playlist", " ")
  168. assert success is False
  169. assert "empty" in message.lower()
  170. def test_rename_playlist_same_name(self, playlists_file, playlist_manager_patched):
  171. """Test renaming to the same name succeeds with unchanged message."""
  172. playlist_manager_patched.create_playlist("my_playlist", ["a.thr"])
  173. success, message = playlist_manager_patched.rename_playlist("my_playlist", "my_playlist")
  174. assert success is True
  175. assert "unchanged" in message.lower()
  176. def test_rename_playlist_name_exists(self, playlists_file, playlist_manager_patched):
  177. """Test renaming to an existing playlist name fails."""
  178. playlist_manager_patched.create_playlist("playlist1", ["a.thr"])
  179. playlist_manager_patched.create_playlist("playlist2", ["b.thr"])
  180. success, message = playlist_manager_patched.rename_playlist("playlist1", "playlist2")
  181. assert success is False
  182. assert "already exists" in message.lower()
  183. def test_rename_playlist_trims_whitespace(self, playlists_file, playlist_manager_patched):
  184. """Test renaming trims whitespace from new name."""
  185. playlist_manager_patched.create_playlist("old_name", ["a.thr"])
  186. success, message = playlist_manager_patched.rename_playlist("old_name", " new_name ")
  187. assert success is True
  188. # Verify trimmed name is used
  189. assert playlist_manager_patched.get_playlist("new_name") is not None
  190. assert playlist_manager_patched.get_playlist(" new_name ") is None