Search v1 (#19)
* Adds description to media items; hooks it up to indexing/media downloading * Added a search method using postgres fulltext search * Hooked up search functionality to the search form * Added persistence to the search form when on search page
This commit is contained in:
@@ -36,6 +36,12 @@ defmodule Pinchflat.MediaClient.Backends.YtDlp.MediaParserTest do
|
||||
assert result.title == "Trying to Wheelie Without the Rear Brake"
|
||||
end
|
||||
|
||||
test "it extracts the description", %{metadata: metadata} do
|
||||
result = Parser.parse_for_media_item(metadata)
|
||||
|
||||
assert is_binary(result.description)
|
||||
end
|
||||
|
||||
test "it returns the metadata as a map", %{metadata: metadata} do
|
||||
result = Parser.parse_for_media_item(metadata)
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ defmodule Pinchflat.MediaClient.Backends.YtDlp.VideoCollectionTest do
|
||||
test "it passes the expected default args" do
|
||||
expect(YtDlpRunnerMock, :run, fn _url, opts, ot ->
|
||||
assert opts == [:simulate, :skip_download]
|
||||
assert ot == "%(.{id,title,was_live,original_url})j"
|
||||
assert ot == "%(.{id,title,was_live,original_url,description})j"
|
||||
|
||||
{:ok, ""}
|
||||
end)
|
||||
|
||||
@@ -47,7 +47,7 @@ defmodule Pinchflat.MediaClient.SourceDetailsTest do
|
||||
test "it passes the expected arguments to the backend" do
|
||||
expect(YtDlpRunnerMock, :run, fn @channel_url, opts, ot ->
|
||||
assert opts == [:simulate, :skip_download]
|
||||
assert ot == "%(.{id,title,was_live,original_url})j"
|
||||
assert ot == "%(.{id,title,was_live,original_url,description})j"
|
||||
|
||||
{:ok, ""}
|
||||
end)
|
||||
|
||||
@@ -60,7 +60,7 @@ defmodule Pinchflat.MediaClient.VideoDownloaderTest do
|
||||
test "it sets the media_downloaded_at", %{media_item: media_item} do
|
||||
assert media_item.media_downloaded_at == nil
|
||||
assert {:ok, updated_media_item} = VideoDownloader.download_for_media_item(media_item)
|
||||
assert DateTime.diff(DateTime.utc_now(), updated_media_item.media_downloaded_at) < 1
|
||||
assert DateTime.diff(DateTime.utc_now(), updated_media_item.media_downloaded_at) < 2
|
||||
end
|
||||
|
||||
test "it extracts the title", %{media_item: media_item} do
|
||||
@@ -68,6 +68,11 @@ defmodule Pinchflat.MediaClient.VideoDownloaderTest do
|
||||
assert updated_media_item.title == "Trying to Wheelie Without the Rear Brake"
|
||||
end
|
||||
|
||||
test "it extracts the description", %{media_item: media_item} do
|
||||
assert {:ok, updated_media_item} = VideoDownloader.download_for_media_item(media_item)
|
||||
assert is_binary(updated_media_item.description)
|
||||
end
|
||||
|
||||
test "it extracts the media_filepath", %{media_item: media_item} do
|
||||
assert media_item.media_filepath == nil
|
||||
assert {:ok, updated_media_item} = VideoDownloader.download_for_media_item(media_item)
|
||||
|
||||
@@ -183,6 +183,37 @@ defmodule Pinchflat.MediaTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "search/1" do
|
||||
setup do
|
||||
media_item =
|
||||
media_item_fixture(%{
|
||||
title: "The quick brown fox",
|
||||
description: "jumps over the lazy dog"
|
||||
})
|
||||
|
||||
{:ok, %{media_item_id: media_item.id}}
|
||||
end
|
||||
|
||||
test "searches based on title", %{media_item_id: media_item_id} do
|
||||
assert [%{id: ^media_item_id}] = Media.search("quick")
|
||||
end
|
||||
|
||||
test "searches based on description", %{media_item_id: media_item_id} do
|
||||
assert [%{id: ^media_item_id}] = Media.search("lazy")
|
||||
end
|
||||
|
||||
test "adds a matching_search_term attribute with the relevant text" do
|
||||
assert [res] = Media.search("quick")
|
||||
assert String.contains?(res.matching_search_term, "The [PF_HIGHLIGHT]quick[/PF_HIGHLIGHT] brown fox")
|
||||
end
|
||||
|
||||
test "optionally lets you specify a limit" do
|
||||
media_item_fixture(%{title: "The small gray dog"})
|
||||
|
||||
assert [_] = Media.search("dog", limit: 1)
|
||||
end
|
||||
end
|
||||
|
||||
describe "get_media_item!/1" do
|
||||
test "it returns the media_item with given id" do
|
||||
media_item = media_item_fixture()
|
||||
|
||||
@@ -63,6 +63,7 @@ defmodule Pinchflat.Tasks.SourceTasksTest do
|
||||
assert Enum.count(media_items) == 3
|
||||
assert ["video1", "video2", "video3"] == Enum.map(media_items, & &1.media_id)
|
||||
assert ["Video 1", "Video 2", "Video 3"] == Enum.map(media_items, & &1.title)
|
||||
assert ["desc1", "desc2", "desc3"] == Enum.map(media_items, & &1.description)
|
||||
assert Enum.all?(media_items, fn mi -> mi.original_url end)
|
||||
assert Enum.all?(media_items, fn %MediaItem{} -> true end)
|
||||
end
|
||||
@@ -110,7 +111,7 @@ defmodule Pinchflat.Tasks.SourceTasksTest do
|
||||
SourceTasks.index_media_items(source)
|
||||
source = Repo.reload!(source)
|
||||
|
||||
assert DateTime.diff(DateTime.utc_now(), source.last_indexed_at) < 1
|
||||
assert DateTime.diff(DateTime.utc_now(), source.last_indexed_at) < 2
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -27,4 +27,18 @@ defmodule Pinchflat.Utils.StringUtilsTest do
|
||||
assert String.length(StringUtils.random_string(64)) == 64
|
||||
end
|
||||
end
|
||||
|
||||
describe "truncate/2" do
|
||||
test "truncates a string to the given length and adds ..." do
|
||||
assert StringUtils.truncate("hello world", 5) == "hello..."
|
||||
end
|
||||
|
||||
test "breaks on a word boundary" do
|
||||
assert StringUtils.truncate("hello world", 7) == "hello..."
|
||||
end
|
||||
|
||||
test "does not truncate a string shorter than the given length" do
|
||||
assert StringUtils.truncate("hello", 10) == "hello"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
defmodule PinchflatWeb.SearchControllerTest do
|
||||
use PinchflatWeb.ConnCase
|
||||
|
||||
describe "show search" do
|
||||
test "renders the page", %{conn: conn} do
|
||||
conn = get(conn, ~p"/search")
|
||||
assert html_response(conn, 200) =~ "Results"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -36,19 +36,22 @@ defmodule Pinchflat.MediaSourceFixtures do
|
||||
id: "video1",
|
||||
title: "Video 1",
|
||||
original_url: "https://example.com/video1",
|
||||
was_live: false
|
||||
was_live: false,
|
||||
description: "desc1"
|
||||
},
|
||||
%{
|
||||
id: "video2",
|
||||
title: "Video 2",
|
||||
original_url: "https://example.com/video2",
|
||||
was_live: true
|
||||
was_live: true,
|
||||
description: "desc2"
|
||||
},
|
||||
%{
|
||||
id: "video3",
|
||||
title: "Video 3",
|
||||
original_url: "https://example.com/video3",
|
||||
was_live: false
|
||||
was_live: false,
|
||||
description: "desc3"
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user