live_beats/test/live_beats/media_library_test.exs
2022-01-17 10:02:19 -06:00

88 lines
3.2 KiB
Elixir

defmodule LiveBeats.MediaLibraryTest do
use LiveBeats.DataCase
alias LiveBeats.MediaLibrary
describe "songs" do
alias LiveBeats.MediaLibrary.Song
import LiveBeats.AccountsFixtures
import LiveBeats.MediaLibraryFixtures
@invalid_attrs %{album_artist: nil, artist: nil, date_recorded: nil, date_released: nil, duration: nil, title: nil}
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]
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]
assert song.duration == 42
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
user = user_fixture()
song = song_fixture(%{user_id: user.id})
assert :ok = MediaLibrary.delete_song(song)
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
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)
end
end