2021-10-29 16:12:23 +00:00
|
|
|
defmodule LiveBeats.MediaLibraryTest do
|
|
|
|
use LiveBeats.DataCase
|
|
|
|
|
|
|
|
alias LiveBeats.MediaLibrary
|
|
|
|
|
|
|
|
describe "songs" do
|
|
|
|
alias LiveBeats.MediaLibrary.Song
|
|
|
|
|
2021-12-14 15:35:51 +00:00
|
|
|
import LiveBeats.AccountsFixtures
|
2021-10-29 16:12:23 +00:00
|
|
|
import LiveBeats.MediaLibraryFixtures
|
|
|
|
|
|
|
|
@invalid_attrs %{album_artist: nil, artist: nil, date_recorded: nil, date_released: nil, duration: nil, title: nil}
|
|
|
|
|
2021-12-14 15:35:51 +00:00
|
|
|
test "list_profile_songs/1 returns all songs for a profile" do
|
|
|
|
user = user_fixture()
|
|
|
|
profile = MediaLibrary.get_profile!(user)
|
|
|
|
song = song_fixture(%{user_id: user.id})
|
|
|
|
assert MediaLibrary.list_profile_songs(profile) == [song]
|
2021-10-29 16:12:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "get_song!/1 returns the song with given id" do
|
|
|
|
song = song_fixture()
|
|
|
|
assert MediaLibrary.get_song!(song.id) == song
|
|
|
|
end
|
|
|
|
|
|
|
|
test "update_song/2 with valid data updates the song" do
|
|
|
|
song = song_fixture()
|
|
|
|
update_attrs = %{album_artist: "some updated album_artist", artist: "some updated artist", date_recorded: ~N[2021-10-27 20:11:00], date_released: ~N[2021-10-27 20:11:00], duration: 43, title: "some updated title"}
|
|
|
|
|
|
|
|
assert {:ok, %Song{} = song} = MediaLibrary.update_song(song, update_attrs)
|
|
|
|
assert song.album_artist == "some updated album_artist"
|
|
|
|
assert song.artist == "some updated artist"
|
|
|
|
assert song.date_recorded == ~N[2021-10-27 20:11:00]
|
|
|
|
assert song.date_released == ~N[2021-10-27 20:11:00]
|
2021-12-14 15:35:51 +00:00
|
|
|
assert song.duration == 42
|
2021-10-29 16:12:23 +00:00
|
|
|
assert song.title == "some updated title"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "update_song/2 with invalid data returns error changeset" do
|
|
|
|
song = song_fixture()
|
|
|
|
assert {:error, %Ecto.Changeset{}} = MediaLibrary.update_song(song, @invalid_attrs)
|
|
|
|
assert song == MediaLibrary.get_song!(song.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delete_song/1 deletes the song" do
|
2021-12-14 15:35:51 +00:00
|
|
|
user = user_fixture()
|
|
|
|
song = song_fixture(%{user_id: user.id})
|
|
|
|
assert :ok = MediaLibrary.delete_song(song)
|
2021-10-29 16:12:23 +00:00
|
|
|
assert_raise Ecto.NoResultsError, fn -> MediaLibrary.get_song!(song.id) end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "change_song/1 returns a song changeset" do
|
|
|
|
song = song_fixture()
|
|
|
|
assert %Ecto.Changeset{} = MediaLibrary.change_song(song)
|
|
|
|
end
|
2022-01-11 21:09:50 +00:00
|
|
|
|
|
|
|
test "delete_expired_songs/2 deletes the song expired before the required interval" do
|
|
|
|
user = user_fixture()
|
|
|
|
today = Timex.now()
|
|
|
|
|
|
|
|
three_months_ago = add_n_months(today, -3)
|
|
|
|
four_months_ago = add_n_months(today, -4)
|
|
|
|
one_month_ago = add_n_months(today, 1)
|
|
|
|
|
|
|
|
expired_song_1 =
|
|
|
|
song_fixture(user_id: user.id, title: "song1", inserted_at: four_months_ago)
|
|
|
|
|
|
|
|
expired_song_2 =
|
|
|
|
song_fixture(user_id: user.id, title: "song2", inserted_at: three_months_ago)
|
|
|
|
|
|
|
|
active_song = song_fixture(user_id: user.id, title: "song3", inserted_at: one_month_ago)
|
|
|
|
|
|
|
|
MediaLibrary.delete_expired_songs(-2, "month")
|
|
|
|
|
|
|
|
assert_raise Ecto.NoResultsError, fn -> MediaLibrary.get_song!(expired_song_1.id) end
|
|
|
|
assert_raise Ecto.NoResultsError, fn -> MediaLibrary.get_song!(expired_song_2.id) end
|
|
|
|
assert active_song == MediaLibrary.get_song!(active_song.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp add_n_months(datetime, n) do
|
|
|
|
datetime
|
|
|
|
|> Timex.shift(months: n)
|
|
|
|
|> DateTime.to_naive()
|
|
|
|
|> NaiveDateTime.truncate(:second)
|
2021-10-29 16:12:23 +00:00
|
|
|
end
|
|
|
|
end
|