Got media tests running

This commit is contained in:
Kieran Eglin 2024-05-28 12:17:08 -07:00
parent 306d19e653
commit e5155c7a83
No known key found for this signature in database
GPG key ID: 193984967FCF432D
2 changed files with 31 additions and 27 deletions

View file

@ -38,7 +38,11 @@ defmodule Pinchflat.Media.MediaQuery do
def upload_date_matches(other_date), do: dynamic([mi], fragment("date(?) = date(?)", mi.uploaded_at, ^other_date)) def upload_date_matches(other_date), do: dynamic([mi], fragment("date(?) = date(?)", mi.uploaded_at, ^other_date))
def upload_date_after_source_cutoff do def upload_date_after_source_cutoff do
dynamic([mi, source], is_nil(source.download_cutoff_date) or mi.upload_date >= source.download_cutoff_date) dynamic(
[mi, source],
is_nil(source.download_cutoff_date) or
fragment("date(?) >= ?", mi.uploaded_at, source.download_cutoff_date)
)
end end
def format_matching_profile_preference do def format_matching_profile_preference do
@ -85,12 +89,12 @@ defmodule Pinchflat.Media.MediaQuery do
def past_redownload_delay do def past_redownload_delay do
dynamic( dynamic(
[mi, source, media_profile], [mi, source, media_profile],
# Returns media items where the upload_date is at least redownload_delay_days ago AND # Returns media items where the uploaded_at is at least redownload_delay_days ago AND
# downloaded_at minus the redownload_delay_days is before the upload date # downloaded_at minus the redownload_delay_days is before the upload date
fragment(""" fragment("""
IFNULL(redownload_delay_days, 0) > 0 AND IFNULL(redownload_delay_days, 0) > 0 AND
DATETIME('now', '-' || redownload_delay_days || ' day') > upload_date AND DATETIME('now', '-' || redownload_delay_days || ' day') > uploaded_at AND
DATETIME(media_downloaded_at, '-' || redownload_delay_days || ' day') < upload_date DATETIME(media_downloaded_at, '-' || redownload_delay_days || ' day') < uploaded_at
""") """)
) )
end end

View file

@ -145,7 +145,7 @@ defmodule Pinchflat.MediaTest do
media_item = media_item =
media_item_fixture(%{ media_item_fixture(%{
source_id: source.id, source_id: source.id,
upload_date: now_minus(6, :days), uploaded_at: now_minus(6, :days),
media_downloaded_at: now_minus(5, :days) media_downloaded_at: now_minus(5, :days)
}) })
@ -156,7 +156,7 @@ defmodule Pinchflat.MediaTest do
media_item = media_item =
media_item_fixture(%{ media_item_fixture(%{
source_id: source.id, source_id: source.id,
upload_date: now_minus(20, :days), uploaded_at: now_minus(20, :days),
media_downloaded_at: now_minus(19, :days) media_downloaded_at: now_minus(19, :days)
}) })
@ -167,7 +167,7 @@ defmodule Pinchflat.MediaTest do
_media_item = _media_item =
media_item_fixture(%{ media_item_fixture(%{
source_id: source.id, source_id: source.id,
upload_date: now_minus(5, :days), uploaded_at: now_minus(5, :days),
media_downloaded_at: nil media_downloaded_at: nil
}) })
@ -178,7 +178,7 @@ defmodule Pinchflat.MediaTest do
_media_item = _media_item =
media_item_fixture(%{ media_item_fixture(%{
source_id: source.id, source_id: source.id,
upload_date: now_minus(5, :days), uploaded_at: now_minus(5, :days),
media_downloaded_at: now(), media_downloaded_at: now(),
prevent_download: true prevent_download: true
}) })
@ -190,7 +190,7 @@ defmodule Pinchflat.MediaTest do
_media_item = _media_item =
media_item_fixture(%{ media_item_fixture(%{
source_id: source.id, source_id: source.id,
upload_date: now_minus(5, :days), uploaded_at: now_minus(5, :days),
media_downloaded_at: now(), media_downloaded_at: now(),
culled_at: now() culled_at: now()
}) })
@ -202,7 +202,7 @@ defmodule Pinchflat.MediaTest do
_media_item = _media_item =
media_item_fixture(%{ media_item_fixture(%{
source_id: source.id, source_id: source.id,
upload_date: now_minus(3, :days), uploaded_at: now_minus(3, :days),
media_downloaded_at: now_minus(3, :days) media_downloaded_at: now_minus(3, :days)
}) })
@ -213,7 +213,7 @@ defmodule Pinchflat.MediaTest do
_media_item = _media_item =
media_item_fixture(%{ media_item_fixture(%{
source_id: source.id, source_id: source.id,
upload_date: now_minus(5, :days), uploaded_at: now_minus(5, :days),
media_downloaded_at: now(), media_downloaded_at: now(),
media_redownloaded_at: now() media_redownloaded_at: now()
}) })
@ -221,12 +221,12 @@ defmodule Pinchflat.MediaTest do
assert Media.list_redownloadable_media_items() == [] assert Media.list_redownloadable_media_items() == []
end end
test "does not return media items that were first downloaded well after the upload_date", %{source: source} do test "does not return media items that were first downloaded well after the uploaded_at", %{source: source} do
_media_item = _media_item =
media_item_fixture(%{ media_item_fixture(%{
source_id: source.id, source_id: source.id,
media_downloaded_at: now(), media_downloaded_at: now(),
upload_date: now_minus(20, :days) uploaded_at: now_minus(20, :days)
}) })
assert Media.list_redownloadable_media_items() == [] assert Media.list_redownloadable_media_items() == []
@ -237,7 +237,7 @@ defmodule Pinchflat.MediaTest do
media_item_fixture(%{ media_item_fixture(%{
source_id: source.id, source_id: source.id,
media_downloaded_at: now(), media_downloaded_at: now(),
upload_date: now_minus(2, :days) uploaded_at: now_minus(2, :days)
}) })
assert Media.list_redownloadable_media_items() == [] assert Media.list_redownloadable_media_items() == []
@ -250,7 +250,7 @@ defmodule Pinchflat.MediaTest do
_media_item = _media_item =
media_item_fixture(%{ media_item_fixture(%{
source_id: source.id, source_id: source.id,
upload_date: now_minus(6, :days), uploaded_at: now_minus(6, :days),
media_downloaded_at: now_minus(5, :days) media_downloaded_at: now_minus(5, :days)
}) })
@ -408,9 +408,9 @@ defmodule Pinchflat.MediaTest do
source = source_fixture(%{download_cutoff_date: now_minus(1, :day)}) source = source_fixture(%{download_cutoff_date: now_minus(1, :day)})
_old_media_item = _old_media_item =
media_item_fixture(%{source_id: source.id, media_filepath: nil, upload_date: now_minus(2, :days)}) media_item_fixture(%{source_id: source.id, media_filepath: nil, uploaded_at: now_minus(2, :days)})
new_media_item = media_item_fixture(%{source_id: source.id, media_filepath: nil, upload_date: now()}) new_media_item = media_item_fixture(%{source_id: source.id, media_filepath: nil, uploaded_at: now()})
assert Media.list_pending_media_items_for(source) == [new_media_item] assert Media.list_pending_media_items_for(source) == [new_media_item]
end end
@ -419,9 +419,9 @@ defmodule Pinchflat.MediaTest do
source = source_fixture(%{download_cutoff_date: nil}) source = source_fixture(%{download_cutoff_date: nil})
old_media_item = old_media_item =
media_item_fixture(%{source_id: source.id, media_filepath: nil, upload_date: now_minus(2, :days)}) media_item_fixture(%{source_id: source.id, media_filepath: nil, uploaded_at: now_minus(2, :days)})
new_media_item = media_item_fixture(%{source_id: source.id, media_filepath: nil, upload_date: now()}) new_media_item = media_item_fixture(%{source_id: source.id, media_filepath: nil, uploaded_at: now()})
assert Media.list_pending_media_items_for(source) == [old_media_item, new_media_item] assert Media.list_pending_media_items_for(source) == [old_media_item, new_media_item]
end end
@ -486,21 +486,21 @@ defmodule Pinchflat.MediaTest do
test "returns true if there is a cutoff date before the media's upload date" do test "returns true if there is a cutoff date before the media's upload date" do
source = source_fixture(%{download_cutoff_date: now_minus(2, :days)}) source = source_fixture(%{download_cutoff_date: now_minus(2, :days)})
media_item = media_item_fixture(%{source_id: source.id, media_filepath: nil, upload_date: now_minus(1, :day)}) media_item = media_item_fixture(%{source_id: source.id, media_filepath: nil, uploaded_at: now_minus(1, :day)})
assert Media.pending_download?(media_item) assert Media.pending_download?(media_item)
end end
test "returns false if there is a cutoff date after the media's upload date" do test "returns false if there is a cutoff date after the media's upload date" do
source = source_fixture(%{download_cutoff_date: now_minus(1, :day)}) source = source_fixture(%{download_cutoff_date: now_minus(1, :day)})
media_item = media_item_fixture(%{source_id: source.id, media_filepath: nil, upload_date: now_minus(2, :days)}) media_item = media_item_fixture(%{source_id: source.id, media_filepath: nil, uploaded_at: now_minus(2, :days)})
refute Media.pending_download?(media_item) refute Media.pending_download?(media_item)
end end
test "returns true if there is no cutoff date" do test "returns true if there is no cutoff date" do
source = source_fixture(%{download_cutoff_date: nil}) source = source_fixture(%{download_cutoff_date: nil})
media_item = media_item_fixture(%{source_id: source.id, media_filepath: nil, upload_date: now_minus(1, :day)}) media_item = media_item_fixture(%{source_id: source.id, media_filepath: nil, uploaded_at: now_minus(1, :day)})
assert Media.pending_download?(media_item) assert Media.pending_download?(media_item)
end end
@ -611,7 +611,7 @@ defmodule Pinchflat.MediaTest do
media_filepath: "/video/#{Faker.File.file_name(:video)}", media_filepath: "/video/#{Faker.File.file_name(:video)}",
source_id: source_fixture().id, source_id: source_fixture().id,
original_url: "https://www.youtube.com/channel/#{Faker.String.base64(12)}", original_url: "https://www.youtube.com/channel/#{Faker.String.base64(12)}",
upload_date: Date.utc_today() uploaded_at: now()
} }
assert {:ok, %MediaItem{} = media_item} = Media.create_media_item(valid_attrs) assert {:ok, %MediaItem{} = media_item} = Media.create_media_item(valid_attrs)
@ -628,7 +628,7 @@ defmodule Pinchflat.MediaTest do
media_filepath: "/video/#{Faker.File.file_name(:video)}", media_filepath: "/video/#{Faker.File.file_name(:video)}",
source_id: source_fixture().id, source_id: source_fixture().id,
original_url: "https://www.youtube.com/channel/#{Faker.String.base64(12)}", original_url: "https://www.youtube.com/channel/#{Faker.String.base64(12)}",
upload_date: Date.utc_today() uploaded_at: now()
} }
assert {:ok, %MediaItem{} = media_item} = Media.create_media_item(valid_attrs) assert {:ok, %MediaItem{} = media_item} = Media.create_media_item(valid_attrs)
@ -643,7 +643,7 @@ defmodule Pinchflat.MediaTest do
media_filepath: "/video/#{Faker.File.file_name(:video)}", media_filepath: "/video/#{Faker.File.file_name(:video)}",
source_id: source_fixture().id, source_id: source_fixture().id,
original_url: "https://www.youtube.com/channel/#{Faker.String.base64(12)}", original_url: "https://www.youtube.com/channel/#{Faker.String.base64(12)}",
upload_date: Date.utc_today(), uploaded_at: now(),
uuid: "some-uuid" uuid: "some-uuid"
} }
@ -1006,7 +1006,7 @@ defmodule Pinchflat.MediaTest do
media_item_new = media_item_fixture(%{source_id: source.id, uploaded_at: now()}) 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)}) 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()}) {:ok, updated_media_item} = Media.update_media_item(media_item_old, %{uploaded_at: now()})
assert media_item_new.upload_date_index == 0 assert media_item_new.upload_date_index == 0
assert updated_media_item.upload_date_index == 1 assert updated_media_item.upload_date_index == 1
@ -1029,7 +1029,7 @@ defmodule Pinchflat.MediaTest do
media_item_one = media_item_fixture(%{source_id: source.id, uploaded_at: now()}) 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()}) _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"}) {:ok, updated_media_item} = Media.update_media_item(media_item_one, %{uploaded_at: now(), title: "New title"})
assert updated_media_item.upload_date_index == 0 assert updated_media_item.upload_date_index == 0
end end