Skip to content

Commit

Permalink
add bad document test
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Nov 10, 2024
1 parent 28ac856 commit 2d9c89e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/hexdocs/search/typesense.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Hexdocs.Search.Typesense do

%{"success" => false, "error" => error, "document" => document} ->
Logger.error(
"Failed to index search item for #{package} #{version}: #{inspect(document)}: #{inspect(error)}"
"Failed to index search item for #{package} #{version} for document #{inspect(document)}: #{inspect(error)}"
)
end
end)
Expand Down
36 changes: 36 additions & 0 deletions test/hexdocs/search_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ defmodule Hexdocs.SearchTest do
end)

assert log =~ "[info] Failed to find search data for #{package} 1.0.0"
assert typesense_search(%{"q" => package, "query_by" => "package"}) == []
end

test "logs an error message if search_data.js file has unexpected format", %{package: package} do
Expand All @@ -134,6 +135,7 @@ defmodule Hexdocs.SearchTest do

log = capture_log(fn -> run_upload(package, "1.0.0", files) end)
assert log =~ "[error] Unexpected search_data format for #{package} 1.0.0"
assert typesense_search(%{"q" => package, "query_by" => "package"}) == []
end

test "logs an error message if search_data.json cannot be decoded", %{package: package} do
Expand All @@ -146,6 +148,8 @@ defmodule Hexdocs.SearchTest do

assert log =~
"[error] Failed to decode search data json for #{package} 1.0.0: unexpected end of input at position 10"

assert typesense_search(%{"q" => package, "query_by" => "package"}) == []
end

test "logs an error message if search_data has empty items", %{package: package} do
Expand All @@ -158,6 +162,8 @@ defmodule Hexdocs.SearchTest do

assert log =~
"[error] Failed to extract search items and proglang from search data for #{package} 1.0.0"

assert typesense_search(%{"q" => package, "query_by" => "package"}) == []
end

test "logs an error message if search_data has no items", %{package: package} do
Expand All @@ -170,6 +176,36 @@ defmodule Hexdocs.SearchTest do

assert log =~
"[error] Failed to extract search items and proglang from search data for #{package} 1.0.0"

assert typesense_search(%{"q" => package, "query_by" => "package"}) == []
end

test "logs errors when indexing incomplete search items", %{package: package} do
files = [
{"index.html", "contents"},
{"dist/search_data-0F918FFD.js",
"""
searchData={"items":[\
{"type":"whatever"},\
{"type":"function","title":"Example.test/4","doc":"does example things","ref":"Example.html#test/4"},\
{"type":"module","title":"Example","doc":"example text","ref":"Example.html"}\
],"content_type":"text/markdown","producer":{"name":"ex_doc","version":[48,46,51,52,46,50]},\
"proglang":"elixir"}\
"""}
]

log = capture_log(fn -> run_upload(package, "1.0.0", files) end)

assert log =~ "[error] Failed to index search item for #{package} 1.0.0 for document "
assert log =~ "Field `doc` has been declared in the schema, but is not found in the document."

# the valid documents should still be indexed
assert [_, _] =
typesense_search(%{
"q" => "example",
"query_by" => "title",
"filter" => "proglang:elixir"
})
end

defp queue_put_message(key) do
Expand Down

0 comments on commit 2d9c89e

Please sign in to comment.