diff --git a/lib/lightning/collections.ex b/lib/lightning/collections.ex index dc821203a9..f98e5ac956 100644 --- a/lib/lightning/collections.ex +++ b/lib/lightning/collections.ex @@ -2,6 +2,8 @@ defmodule Lightning.Collections do @moduledoc """ Access to collections of unique key-value pairs shared across multiple workflows. """ + import Ecto.Query + alias Lightning.Collections.Collection alias Lightning.Collections.Item alias Lightning.Repo @@ -23,12 +25,16 @@ defmodule Lightning.Collections do end end - @spec get(String.t(), String.t()) :: {:ok, Item.t()} | {:error, :not_found} + @spec get(String.t(), String.t()) :: Item.t() def get(col_name, key) do - case Repo.get_by(Item, collection_name: col_name, key: key) do - nil -> {:error, :not_found} - item -> {:ok, item} - end + Repo.get_by(Item, collection_name: col_name, key: key) + end + + @spec get_all(String.t()) :: [Item.t()] + def get_all(col_name) do + query_all = from(i in Item, where: i.collection_name == ^col_name) + + Repo.all(query_all) end @spec put(String.t(), String.t(), String.t()) :: diff --git a/test/lightning/collections_test.exs b/test/lightning/collections_test.exs index 9527a7213a..24201341cf 100644 --- a/test/lightning/collections_test.exs +++ b/test/lightning/collections_test.exs @@ -55,21 +55,44 @@ defmodule Lightning.CollectionsTest do %{key: key, value: value, collection: %{name: name}} = insert(:collection_item) |> Repo.preload(:collection) - assert {:ok, %Item{key: ^key, value: ^value}} = Collections.get(name, key) + assert %Item{key: ^key, value: ^value} = Collections.get(name, key) end - test "returns an :error if the collection does not exist" do + test "returns nil if the item key does not exist" do + collection = insert(:collection) + + refute Collections.get(collection.name, "nonexistent") + end + + test "returns nil if the collection does not exist" do insert(:collection_item, key: "existing_key") - assert {:error, :not_found} = - Collections.get("nonexistent", "existing_key") + refute Collections.get("nonexistent", "existing_key") end + end - test "returns nil if the item key does not exist" do + describe "get_all/1" do + test "returns all items for the given collection" do collection = insert(:collection) + items = insert_list(11, :collection_item, collection: collection) - assert {:error, :not_found} = - Collections.get(collection.name, "nonexistent") + assert items == + Collections.get_all(collection.name) + |> Repo.preload(collection: :project) + + assert Enum.count(items, & &1.key) == 11 + end + + test "returns empty list when collection is empty" do + collection = insert(:collection) + + assert [] = Collections.get_all(collection.name) + end + + test "returns empty list when the collection doesn't exist" do + insert(:collection_item, key: "existing_key") + + assert [] = Collections.get_all("nonexistent") end end @@ -121,7 +144,7 @@ defmodule Lightning.CollectionsTest do assert {:ok, %{key: ^key}} = Collections.delete(collection.name, key) - assert {:error, :not_found} = Collections.get(collection.name, key) + refute Collections.get(collection.name, key) end test "returns an :error if the collection does not exist" do