test delete_expired_songs/2

This commit is contained in:
Berenice Medel 2022-01-11 15:09:50 -06:00
parent 1cb2e7454c
commit ec6606f29c
2 changed files with 32 additions and 1 deletions

View file

@ -52,7 +52,8 @@ defmodule LiveBeats.MixProject do
{:mint, "~> 1.0"}, {:mint, "~> 1.0"},
{:heroicons, "~> 0.2.2"}, {:heroicons, "~> 0.2.2"},
{:castore, "~> 0.1.13"}, {:castore, "~> 0.1.13"},
{:tailwind, "~> 0.1"} {:tailwind, "~> 0.1"},
{:timex, "~> 3.0", only: :test}
] ]
end end

View file

@ -53,5 +53,35 @@ defmodule LiveBeats.MediaLibraryTest do
song = song_fixture() song = song_fixture()
assert %Ecto.Changeset{} = MediaLibrary.change_song(song) assert %Ecto.Changeset{} = MediaLibrary.change_song(song)
end 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
end end