def test_retrieve_genres_success(self): user = sample_user() sample_genre(user, "Genre1") sample_genre(user, "Genre2") response = self.client.get(GENRE_URL) genres = Genre.objects.all() serializer = GenreSerializer(genres, many=True) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, serializer.data)
def test_genres_search(self): genre1 = sample_genre(self.user, "Rock") genre2 = sample_genre(self.user, "Progressive Metal") response = self.client.get(SEARCH_URL, { "value": "metal", "page_number": 1 }) serializer1 = GenreSerializer(genre1) serializer2 = GenreSerializer(genre2) self.assertIn(serializer2.data, response.data["genres"]) self.assertNotIn(serializer1.data, response.data["genres"])
def test_retrieve_genres_assigned_to_song(self, mock_get_artist, mock_get_song, mock_get_video_url): genre1 = sample_genre(self.user, "Genre1") genre2 = sample_genre(self.user, "Genre2") artist = sample_artist(self.user) song = sample_song(self.user, artist) song.genres.set([genre1]) response = self.client.get(GENRE_URL, {"is_assigned": 1}) serializer1 = GenreSerializer(genre1) serializer2 = GenreSerializer(genre2) self.assertIn(serializer1.data, response.data) self.assertNotIn(serializer2.data, response.data)
def test_artists_attached_to_genre(self, mock_get_artist): genre1 = sample_genre(self.user, "Rock") genre2 = sample_genre(self.user, "Progressive metal") artist1 = sample_artist(self.user, "The Doors") artist2 = sample_artist(self.user, "Haken") artist1.genres.set([genre1]) artist2.genres.set([genre2]) response = self.client.get(SEARCH_URL, { "value": "progressive", "page_number": 1 }) serializer1 = ArtistSerializer(artist1) serializer2 = ArtistSerializer(artist2) self.assertIn(serializer2.data, response.data["artists"]) self.assertNotIn(serializer1.data, response.data["artists"])
def test_filter_songs_by_genre(self, mock_get_song, mock_get_video_url): genre1 = sample_genre(self.user, "Genre1") genre2 = sample_genre(self.user, "Genre2") song1 = sample_song(self.user, self.artist, "Title1") song2 = sample_song(self.user, self.artist, "Title2") song1.genres.set([genre1]) song2.genres.set([genre2]) response = self.client.get(SONG_URL, {"genre": f"{genre1.id}"}) serializer1 = SongSerializer(song1) serializer2 = SongSerializer(song2) self.assertIn(serializer1.data, response.data) self.assertNotIn(serializer2.data, response.data)
def test_update_other_user_not_allowed(self): creator_user = sample_user("creator", "pass123") genre = sample_genre(creator_user) response = self.client.patch(get_genre_detail_url(genre.id), {"name": "updated genre"}) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_songs_attached_to_genre(self, mock_get_artist, mock_get_song, mock_get_video_url): genre1 = sample_genre(self.user, "Rock") genre2 = sample_genre(self.user, "Psychodelic rock") artist = sample_artist(self.user, "The Doors") song1 = sample_song(self.user, artist, "The End") song2 = sample_song(self.user, artist, "Light my fire") song1.genres.set([genre1]) song2.genres.set([genre2]) response = self.client.get(SEARCH_URL, { "value": "psychodelic", "page_number": 1 }) serializer1 = SongSerializer(song1) serializer2 = SongSerializer(song2) self.assertIn(serializer2.data, response.data["songs"]) self.assertNotIn(serializer1.data, response.data["songs"])
def test_retrieve_unique_genres(self, mock_get_artist, mock_get_song, mock_get_video_url): genre = sample_genre(self.user) artist = sample_artist(self.user) song1 = sample_song(self.user, artist, "Song1") song2 = sample_song(self.user, artist, "Song2") song1.genres.set([genre]) song2.genres.set([genre]) response = self.client.get(GENRE_URL, {"is_assigned": 1}) self.assertEqual(len(response.data), 1)
def setUp(self): self.user = sample_user() self.genre = sample_genre(self.user)