mirror of
https://github.com/fly-apps/live_beats.git
synced 2024-11-22 08:01:00 +00:00
test delete_expired_songs/2
This commit is contained in:
parent
1cb2e7454c
commit
ec6606f29c
2 changed files with 32 additions and 1 deletions
3
mix.exs
3
mix.exs
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue