Refactored media item upload_date_index
This commit is contained in:
parent
e8c8613074
commit
306d19e653
4 changed files with 134 additions and 147 deletions
|
|
@ -130,8 +130,7 @@ defmodule Pinchflat.Media.MediaItem do
|
|||
~w(__meta__ __struct__ metadata tasks media_items_search_index)a
|
||||
end
|
||||
|
||||
# TODO: refactor
|
||||
defp update_upload_date_index(%{changes: changes} = changeset) when is_map_key(changes, :upload_date) do
|
||||
defp update_upload_date_index(%{changes: changes} = changeset) when is_map_key(changes, :uploaded_at) do
|
||||
source_id = get_field(changeset, :source_id)
|
||||
source = Sources.get_source!(source_id)
|
||||
# Channels should count down from 99, playlists should count up from 0
|
||||
|
|
@ -143,7 +142,7 @@ defmodule Pinchflat.Media.MediaItem do
|
|||
|
||||
current_max =
|
||||
MediaQuery.new()
|
||||
|> where(^dynamic([mi], mi.upload_date == ^changes.upload_date and ^MediaQuery.for_source(source)))
|
||||
|> where(^dynamic([mi], ^MediaQuery.upload_date_matches(changes.uploaded_at) and ^MediaQuery.for_source(source)))
|
||||
|> Repo.aggregate(aggregator, :upload_date_index)
|
||||
|
||||
case current_max do
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ defmodule Pinchflat.Media.MediaQuery do
|
|||
def culling_prevented, do: dynamic([mi], mi.prevent_culling == true)
|
||||
def culled, do: dynamic([mi], not is_nil(mi.culled_at))
|
||||
def redownloaded, do: dynamic([mi], not is_nil(mi.media_redownloaded_at))
|
||||
def upload_date_matches(other_date), do: dynamic([mi], fragment("date(?) = date(?)", mi.uploaded_at, ^other_date))
|
||||
|
||||
def upload_date_after_source_cutoff do
|
||||
dynamic([mi, source], is_nil(source.download_cutoff_date) or mi.upload_date >= source.download_cutoff_date)
|
||||
|
|
|
|||
|
|
@ -34,150 +34,6 @@ defmodule Pinchflat.MediaTest do
|
|||
end
|
||||
end
|
||||
|
||||
describe "schema when testing upload_date_index and source is a channel" do
|
||||
test "upload_date_index is set to 99 if it's the only video uploaded that day" do
|
||||
upload_date = Date.utc_today()
|
||||
source = source_fixture(%{collection_type: :channel})
|
||||
media_item = media_item_fixture(%{source_id: source.id, upload_date: upload_date})
|
||||
|
||||
assert media_item.upload_date_index == 99
|
||||
end
|
||||
|
||||
test "upload_date_index is set to 98 if it's the second video uploaded that day" do
|
||||
upload_date = Date.utc_today()
|
||||
source = source_fixture(%{collection_type: :channel})
|
||||
|
||||
media_item_one = media_item_fixture(%{source_id: source.id, upload_date: upload_date})
|
||||
media_item_two = media_item_fixture(%{source_id: source.id, upload_date: upload_date})
|
||||
|
||||
assert media_item_one.upload_date_index == 99
|
||||
assert media_item_two.upload_date_index == 98
|
||||
end
|
||||
|
||||
test "upload_date_index doesn't decrement if the video is uploaded on a different day" do
|
||||
today = Date.utc_today()
|
||||
one_day_ago = Date.add(today, -1)
|
||||
source = source_fixture(%{collection_type: :channel})
|
||||
|
||||
media_item_new = media_item_fixture(%{source_id: source.id, upload_date: today})
|
||||
media_item_old = media_item_fixture(%{source_id: source.id, upload_date: one_day_ago})
|
||||
|
||||
assert media_item_new.upload_date_index == 99
|
||||
assert media_item_old.upload_date_index == 99
|
||||
end
|
||||
|
||||
test "recomputes upload_date_index if an upload_date is changed...somehow" do
|
||||
today = Date.utc_today()
|
||||
one_day_ago = Date.add(today, -1)
|
||||
source = source_fixture(%{collection_type: :channel})
|
||||
|
||||
media_item_new = media_item_fixture(%{source_id: source.id, upload_date: today})
|
||||
media_item_old = media_item_fixture(%{source_id: source.id, upload_date: one_day_ago})
|
||||
|
||||
{:ok, updated_media_item} = Media.update_media_item(media_item_old, %{upload_date: today})
|
||||
|
||||
assert media_item_new.upload_date_index == 99
|
||||
assert updated_media_item.upload_date_index == 98
|
||||
end
|
||||
|
||||
test "upload_date_index doesn't decrement if the video is for a different source" do
|
||||
today = Date.utc_today()
|
||||
|
||||
source_one = source_fixture(%{collection_type: :channel})
|
||||
source_two = source_fixture(%{collection_type: :channel})
|
||||
|
||||
media_item_one = media_item_fixture(%{source_id: source_one.id, upload_date: today})
|
||||
media_item_two = media_item_fixture(%{source_id: source_two.id, upload_date: today})
|
||||
|
||||
assert media_item_one.upload_date_index == 99
|
||||
assert media_item_two.upload_date_index == 99
|
||||
end
|
||||
|
||||
test "upload_date_index doesn't decrement if the a video's upload_date is updated but doesn't change" do
|
||||
today = Date.utc_today()
|
||||
source = source_fixture(%{collection_type: :channel})
|
||||
|
||||
media_item_one = media_item_fixture(%{source_id: source.id, upload_date: today})
|
||||
_media_item_two = media_item_fixture(%{source_id: source.id, upload_date: today})
|
||||
|
||||
{:ok, updated_media_item} = Media.update_media_item(media_item_one, %{upload_date: today, title: "New title"})
|
||||
|
||||
assert updated_media_item.upload_date_index == 99
|
||||
end
|
||||
end
|
||||
|
||||
describe "schema when testing upload_date_index and source is a playlist" do
|
||||
test "upload_date_index is set to 0 if it's the only video uploaded that day" do
|
||||
upload_date = Date.utc_today()
|
||||
source = source_fixture(%{collection_type: :playlist})
|
||||
media_item = media_item_fixture(%{source_id: source.id, upload_date: upload_date})
|
||||
|
||||
assert media_item.upload_date_index == 0
|
||||
end
|
||||
|
||||
test "upload_date_index is set to 1 if it's the second video uploaded that day" do
|
||||
upload_date = Date.utc_today()
|
||||
source = source_fixture(%{collection_type: :playlist})
|
||||
|
||||
media_item_one = media_item_fixture(%{source_id: source.id, upload_date: upload_date})
|
||||
media_item_two = media_item_fixture(%{source_id: source.id, upload_date: upload_date})
|
||||
|
||||
assert media_item_one.upload_date_index == 0
|
||||
assert media_item_two.upload_date_index == 1
|
||||
end
|
||||
|
||||
test "upload_date_index doesn't increment if the video is uploaded on a different day" do
|
||||
today = Date.utc_today()
|
||||
one_day_ago = Date.add(today, -1)
|
||||
source = source_fixture(%{collection_type: :playlist})
|
||||
|
||||
media_item_new = media_item_fixture(%{source_id: source.id, upload_date: today})
|
||||
media_item_old = media_item_fixture(%{source_id: source.id, upload_date: one_day_ago})
|
||||
|
||||
assert media_item_new.upload_date_index == 0
|
||||
assert media_item_old.upload_date_index == 0
|
||||
end
|
||||
|
||||
test "recomputes upload_date_index if an upload_date is changed...somehow" do
|
||||
today = Date.utc_today()
|
||||
one_day_ago = Date.add(today, -1)
|
||||
source = source_fixture(%{collection_type: :playlist})
|
||||
|
||||
media_item_new = media_item_fixture(%{source_id: source.id, upload_date: today})
|
||||
media_item_old = media_item_fixture(%{source_id: source.id, upload_date: one_day_ago})
|
||||
|
||||
{:ok, updated_media_item} = Media.update_media_item(media_item_old, %{upload_date: today})
|
||||
|
||||
assert media_item_new.upload_date_index == 0
|
||||
assert updated_media_item.upload_date_index == 1
|
||||
end
|
||||
|
||||
test "upload_date_index doesn't increment if the video is for a different source" do
|
||||
today = Date.utc_today()
|
||||
|
||||
source_one = source_fixture(%{collection_type: :playlist})
|
||||
source_two = source_fixture(%{collection_type: :playlist})
|
||||
|
||||
media_item_one = media_item_fixture(%{source_id: source_one.id, upload_date: today})
|
||||
media_item_two = media_item_fixture(%{source_id: source_two.id, upload_date: today})
|
||||
|
||||
assert media_item_one.upload_date_index == 0
|
||||
assert media_item_two.upload_date_index == 0
|
||||
end
|
||||
|
||||
test "upload_date_index doesn't increment if the a video's upload_date is updated but doesn't change" do
|
||||
today = Date.utc_today()
|
||||
source = source_fixture(%{collection_type: :playlist})
|
||||
|
||||
media_item_one = media_item_fixture(%{source_id: source.id, upload_date: today})
|
||||
_media_item_two = media_item_fixture(%{source_id: source.id, upload_date: today})
|
||||
|
||||
{:ok, updated_media_item} = Media.update_media_item(media_item_one, %{upload_date: today, title: "New title"})
|
||||
|
||||
assert updated_media_item.upload_date_index == 0
|
||||
end
|
||||
end
|
||||
|
||||
describe "list_media_items/0" do
|
||||
test "it returns all media_items" do
|
||||
media_item = media_item_fixture()
|
||||
|
|
@ -1052,4 +908,130 @@ defmodule Pinchflat.MediaTest do
|
|||
assert %Ecto.Changeset{} = Media.change_media_item(media_item)
|
||||
end
|
||||
end
|
||||
|
||||
describe "change_media_item/1 when testing upload_date_index and source is a channel" do
|
||||
test "upload_date_index is set to 99 if it's the only video uploaded that day" do
|
||||
source = source_fixture(%{collection_type: :channel})
|
||||
media_item = media_item_fixture(%{source_id: source.id, uploaded_at: now()})
|
||||
|
||||
assert media_item.upload_date_index == 99
|
||||
end
|
||||
|
||||
test "upload_date_index is set to 98 if it's the second video uploaded that day" do
|
||||
source = source_fixture(%{collection_type: :channel})
|
||||
|
||||
media_item_one = media_item_fixture(%{source_id: source.id, uploaded_at: now()})
|
||||
media_item_two = media_item_fixture(%{source_id: source.id, uploaded_at: now()})
|
||||
|
||||
assert media_item_one.upload_date_index == 99
|
||||
assert media_item_two.upload_date_index == 98
|
||||
end
|
||||
|
||||
test "upload_date_index doesn't decrement if the video is uploaded on a different day" do
|
||||
source = source_fixture(%{collection_type: :channel})
|
||||
|
||||
media_item_new = media_item_fixture(%{source_id: source.id, uploaded_at: now()})
|
||||
media_item_old = media_item_fixture(%{source_id: source.id, uploaded_at: now_minus(1, :day)})
|
||||
|
||||
assert media_item_new.upload_date_index == 99
|
||||
assert media_item_old.upload_date_index == 99
|
||||
end
|
||||
|
||||
test "recomputes upload_date_index if an upload_date is changed...somehow" do
|
||||
source = source_fixture(%{collection_type: :channel})
|
||||
|
||||
media_item_new = media_item_fixture(%{source_id: source.id, uploaded_at: now()})
|
||||
media_item_old = media_item_fixture(%{source_id: source.id, uploaded_at: now_minus(1, :day)})
|
||||
|
||||
{:ok, updated_media_item} = Media.update_media_item(media_item_old, %{uploaded_at: now()})
|
||||
|
||||
assert media_item_new.upload_date_index == 99
|
||||
assert updated_media_item.upload_date_index == 98
|
||||
end
|
||||
|
||||
test "upload_date_index doesn't decrement if the video is for a different source" do
|
||||
source_one = source_fixture(%{collection_type: :channel})
|
||||
source_two = source_fixture(%{collection_type: :channel})
|
||||
|
||||
media_item_one = media_item_fixture(%{source_id: source_one.id, uploaded_at: now()})
|
||||
media_item_two = media_item_fixture(%{source_id: source_two.id, uploaded_at: now()})
|
||||
|
||||
assert media_item_one.upload_date_index == 99
|
||||
assert media_item_two.upload_date_index == 99
|
||||
end
|
||||
|
||||
test "upload_date_index doesn't decrement if the a video's upload_date is updated but doesn't change" do
|
||||
source = source_fixture(%{collection_type: :channel})
|
||||
|
||||
media_item_one = media_item_fixture(%{source_id: source.id, uploaded_at: now()})
|
||||
_media_item_two = media_item_fixture(%{source_id: source.id, uploaded_at: now()})
|
||||
|
||||
{:ok, updated_media_item} = Media.update_media_item(media_item_one, %{uploaded_at: now(), title: "New title"})
|
||||
|
||||
assert updated_media_item.upload_date_index == 99
|
||||
end
|
||||
end
|
||||
|
||||
describe "change_media_item/1 when testing upload_date_index and source is a playlist" do
|
||||
test "upload_date_index is set to 0 if it's the only video uploaded that day" do
|
||||
source = source_fixture(%{collection_type: :playlist})
|
||||
media_item = media_item_fixture(%{source_id: source.id, uploaded_at: now()})
|
||||
|
||||
assert media_item.upload_date_index == 0
|
||||
end
|
||||
|
||||
test "upload_date_index is set to 1 if it's the second video uploaded that day" do
|
||||
source = source_fixture(%{collection_type: :playlist})
|
||||
|
||||
media_item_one = media_item_fixture(%{source_id: source.id, uploaded_at: now()})
|
||||
media_item_two = media_item_fixture(%{source_id: source.id, uploaded_at: now()})
|
||||
|
||||
assert media_item_one.upload_date_index == 0
|
||||
assert media_item_two.upload_date_index == 1
|
||||
end
|
||||
|
||||
test "upload_date_index doesn't increment if the video is uploaded on a different day" do
|
||||
source = source_fixture(%{collection_type: :playlist})
|
||||
|
||||
media_item_new = media_item_fixture(%{source_id: source.id, uploaded_at: now()})
|
||||
media_item_old = media_item_fixture(%{source_id: source.id, uploaded_at: now_minus(1, :day)})
|
||||
|
||||
assert media_item_new.upload_date_index == 0
|
||||
assert media_item_old.upload_date_index == 0
|
||||
end
|
||||
|
||||
test "recomputes upload_date_index if an upload_date is changed...somehow" do
|
||||
source = source_fixture(%{collection_type: :playlist})
|
||||
|
||||
media_item_new = media_item_fixture(%{source_id: source.id, uploaded_at: now()})
|
||||
media_item_old = media_item_fixture(%{source_id: source.id, uploaded_at: now_minus(1, :day)})
|
||||
|
||||
{:ok, updated_media_item} = Media.update_media_item(media_item_old, %{upload_date: now()})
|
||||
|
||||
assert media_item_new.upload_date_index == 0
|
||||
assert updated_media_item.upload_date_index == 1
|
||||
end
|
||||
|
||||
test "upload_date_index doesn't increment if the video is for a different source" do
|
||||
source_one = source_fixture(%{collection_type: :playlist})
|
||||
source_two = source_fixture(%{collection_type: :playlist})
|
||||
|
||||
media_item_one = media_item_fixture(%{source_id: source_one.id, uploaded_at: now()})
|
||||
media_item_two = media_item_fixture(%{source_id: source_two.id, uploaded_at: now()})
|
||||
|
||||
assert media_item_one.upload_date_index == 0
|
||||
assert media_item_two.upload_date_index == 0
|
||||
end
|
||||
|
||||
test "upload_date_index doesn't increment if the a video's upload_date is updated but doesn't change" do
|
||||
source = source_fixture(%{collection_type: :playlist})
|
||||
|
||||
media_item_one = media_item_fixture(%{source_id: source.id, uploaded_at: now()})
|
||||
_media_item_two = media_item_fixture(%{source_id: source.id, uploaded_at: now()})
|
||||
|
||||
{:ok, updated_media_item} = Media.update_media_item(media_item_one, %{upload_date: now(), title: "New title"})
|
||||
|
||||
assert updated_media_item.upload_date_index == 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -13,6 +13,11 @@ defmodule Pinchflat.MediaFixtures do
|
|||
def media_item_fixture(attrs \\ %{}) do
|
||||
media_id = Faker.String.base64(12)
|
||||
|
||||
# TODO: remove
|
||||
if attrs[:upload_date] || attrs["upload_date"] do
|
||||
raise "upload_date is not allowed in media_item_fixture. Use uploaded_at instead."
|
||||
end
|
||||
|
||||
{:ok, media_item} =
|
||||
attrs
|
||||
|> Enum.into(%{
|
||||
|
|
|
|||
Loading…
Reference in a new issue