separate describe block for expire_songs_older_than/2

This commit is contained in:
Berenice Medel 2022-01-18 11:02:17 -06:00
parent 1ec2254b6e
commit 2c709ffe2a

View file

@ -3,13 +3,11 @@ defmodule LiveBeats.MediaLibraryTest do
alias LiveBeats.MediaLibrary alias LiveBeats.MediaLibrary
alias LiveBeats.Accounts alias LiveBeats.Accounts
describe "songs" do
alias LiveBeats.MediaLibrary.Song alias LiveBeats.MediaLibrary.Song
import LiveBeats.AccountsFixtures import LiveBeats.AccountsFixtures
import LiveBeats.MediaLibraryFixtures import LiveBeats.MediaLibraryFixtures
describe "songs" do
@invalid_attrs %{ @invalid_attrs %{
album_artist: nil, album_artist: nil,
artist: nil, artist: nil,
@ -69,14 +67,21 @@ 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
end
test "expire_songs_older_than/2 deletes the song expired before the required interval" do describe "expire_songs_older_than/2" do
user = user_fixture() setup do
today = DateTime.utc_now() today = DateTime.utc_now()
three_months_ago = add_n_months(today, -3) creation_dates = Enum.map([-1, -3, -4], &add_n_months(today, &1))
four_months_ago = add_n_months(today, -4)
one_month_ago = add_n_months(today, -1) %{creation_dates: creation_dates}
end
test "deletes the song expired before the required interval", %{
creation_dates: [one_month_ago, three_months_ago, four_months_ago]
} do
user = user_fixture()
expired_song_1 = expired_song_1 =
song_fixture(user_id: user.id, title: "song1", inserted_at: four_months_ago) song_fixture(user_id: user.id, title: "song1", inserted_at: four_months_ago)
@ -93,25 +98,22 @@ defmodule LiveBeats.MediaLibraryTest do
assert active_song == MediaLibrary.get_song!(active_song.id) assert active_song == MediaLibrary.get_song!(active_song.id)
end end
test "Users song_count is decremented when expire_songs_older_than/2 is called and user songs are deleted" do test "Users song_count is decremented when user songs are deleted", %{
creation_dates: creation_dates
} do
user = user_fixture() user = user_fixture()
today = DateTime.utc_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)
creation_dates = [three_months_ago, four_months_ago, one_month_ago]
songs_changesets = songs_changesets =
["1", "2", "3"] ["1", "2", "3"]
|> Enum.reduce(%{}, fn song_number, acc -> |> Enum.reduce(%{}, fn song_number, acc ->
song_changeset = Song.changeset(%Song{}, %{title: "song#{song_number}", artist: "artist_one"}) song_changeset =
Song.changeset(%Song{}, %{title: "song#{song_number}", artist: "artist_one"})
Map.put_new(acc, song_number, song_changeset) Map.put_new(acc, song_number, song_changeset)
end) end)
assert {:ok, results} =
assert {:ok, results} = MediaLibrary.import_songs(user, songs_changesets, fn one, two -> {one, two} end) MediaLibrary.import_songs(user, songs_changesets, fn one, two -> {one, two} end)
assert Accounts.get_user(user.id).songs_count == 3 assert Accounts.get_user(user.id).songs_count == 3
@ -127,7 +129,6 @@ defmodule LiveBeats.MediaLibraryTest do
assert Accounts.get_user(user.id).songs_count == 1 assert Accounts.get_user(user.id).songs_count == 1
end end
end
defp add_n_months(datetime, n) do defp add_n_months(datetime, n) do
seconds = 30 * (60 * 60 * 24) * n seconds = 30 * (60 * 60 * 24) * n
@ -137,4 +138,5 @@ defmodule LiveBeats.MediaLibraryTest do
|> DateTime.to_naive() |> DateTime.to_naive()
|> NaiveDateTime.truncate(:second) |> NaiveDateTime.truncate(:second)
end end
end
end end