Skip to content

Commit

Permalink
Include HGETALL and make get and get_all similar to Ecto returns
Browse files Browse the repository at this point in the history
  • Loading branch information
jyeshe committed Oct 9, 2024
1 parent 77c5728 commit d1cf44a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
16 changes: 11 additions & 5 deletions lib/lightning/collections.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()) ::
Expand Down
39 changes: 31 additions & 8 deletions test/lightning/collections_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d1cf44a

Please sign in to comment.