diff --git a/lib/dataloader.ex b/lib/dataloader.ex index 4559da0..8a0bbea 100644 --- a/lib/dataloader.ex +++ b/lib/dataloader.ex @@ -239,7 +239,14 @@ defmodule Dataloader do end defp get_source(loader, source_name) do - loader.sources[source_name] || raise "Source does not exist: #{inspect(source_name)}" + loader.sources[source_name] || + raise """ + Source does not exist: #{inspect(source_name)} + + Registered sources are: + + #{inspect(Enum.map(loader.sources, fn {source, _} -> source end))} + """ end @doc """ @@ -319,8 +326,6 @@ defmodule Dataloader do end @doc """ - This function is depreacted in favour of `async_safely/3` - This used to be used by both the `Dataloader` module for running multiple source queries concurrently, and the `KV` and `Ecto` sources to actually run separate batch fetches (e.g. for `Posts` and `Users` at the same time). @@ -331,6 +336,7 @@ defmodule Dataloader do Please use `async_safely/3` instead of this for fetching data from sources """ + @doc deprecated: "Use async_safely/3 instead" @spec pmap(list(), fun(), keyword()) :: map() def pmap(items, fun, opts \\ []) do async_safely(__MODULE__, :run_tasks, [items, fun, opts]) diff --git a/lib/dataloader/ecto.ex b/lib/dataloader/ecto.ex index 69438cf..b9f511d 100644 --- a/lib/dataloader/ecto.ex +++ b/lib/dataloader/ecto.ex @@ -265,7 +265,7 @@ if Code.ensure_loaded?(Ecto) do @type t :: %__MODULE__{ repo: Ecto.Repo.t(), query: query_fun, - repo_opts: Keyword.t(), + repo_opts: repo_opts, batches: map, results: map, default_params: map, @@ -316,6 +316,14 @@ if Code.ensure_loaded?(Ecto) do Default implementation for loading a batch. Handles looking up records by column """ + @spec run_batch( + repo :: Ecto.Repo.t(), + queryable :: Ecto.Queryable.t(), + query :: Ecto.Query.t(), + col :: any, + inputs :: [any], + repo_opts :: repo_opts + ) :: [any] def run_batch(repo, _queryable, query, col, inputs, repo_opts) do results = load_rows(col, inputs, query, repo, repo_opts) grouped_results = group_results(results, col) @@ -482,8 +490,12 @@ if Code.ensure_loaded?(Ecto) do {:primary, col, value} -> {{:queryable, self(), queryable, :one, col, opts}, value, value} - _ -> - raise "cardinality required unless using primary key" + {:not_primary, col, _value} -> + raise """ + Cardinality required unless using primary key + + The non-primary key column specified was: #{inspect(col)} + """ end end diff --git a/lib/dataloader/kv.ex b/lib/dataloader/kv.ex index 9b04889..d9940a7 100644 --- a/lib/dataloader/kv.ex +++ b/lib/dataloader/kv.ex @@ -80,8 +80,8 @@ defmodule Dataloader.KV do defp fetched?(results, batch_key, id) do case results do - %{^batch_key => %{^id => {:error, _}}} -> false - %{^batch_key => %{^id => _}} -> true + %{^batch_key => %{^id => {:error, _}}} -> false + %{^batch_key => %{^id => _}} -> true _ -> false end end diff --git a/test/dataloader/ecto_test.exs b/test/dataloader/ecto_test.exs index 460561e..9830a98 100644 --- a/test/dataloader/ecto_test.exs +++ b/test/dataloader/ecto_test.exs @@ -289,7 +289,7 @@ defmodule Dataloader.EctoTest do Dataloader.load(loader, Test, {User, %{foo: :bar}}, username: 1) end) - assert message =~ "cardinality" + assert message =~ "Cardinality" end test "works with has many through", %{loader: loader} do diff --git a/test/dataloader_test.exs b/test/dataloader_test.exs index 9115231..7ff1f16 100644 --- a/test/dataloader_test.exs +++ b/test/dataloader_test.exs @@ -53,6 +53,23 @@ defmodule DataloaderTest do end end + describe "unknown sources" do + test "load/3 for unknown source returns error", %{loader: loader} do + assert_raise RuntimeError, ~r/Source does not exist/, fn -> + loader |> Dataloader.load(:bogus, :users, "ben") + end + end + + test "get/3 for unknown source returns error", %{loader: loader} do + assert_raise RuntimeError, ~r/Source does not exist/, fn -> + loader + |> Dataloader.load(:test, :users, "ben") + |> Dataloader.run() + |> Dataloader.get(:bogus, :users, "ben") + end + end + end + describe "get methods when configured to raise an error" do test "get/4 returns a value when successful", %{loader: loader} do result =