Skip to content

Commit

Permalink
Merge pull request #92 from maartenvanvliet/minor_doc_improvements
Browse files Browse the repository at this point in the history
Minor doc improvements
  • Loading branch information
benwilson512 authored May 4, 2020
2 parents 8a4888c + 1d8c538 commit 5dc66b9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
12 changes: 9 additions & 3 deletions lib/dataloader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down Expand Up @@ -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).
Expand All @@ -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])
Expand Down
18 changes: 15 additions & 3 deletions lib/dataloader/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/dataloader/kv.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/dataloader/ecto_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/dataloader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 5dc66b9

Please sign in to comment.