Skip to content

Commit

Permalink
update sandbox hash_set_many behaviour to be consistent (#24)
Browse files Browse the repository at this point in the history
* update sandbox behavior for consistency

* add tests

* add tests for map arguments
  • Loading branch information
cylkdev committed Mar 5, 2024
1 parent 19aa0b0 commit 26c9d8f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/cache/sandbox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ defmodule Cache.Sandbox do

if ttl do
command_resps =
Enum.map(keys_fields_values, fn {_, fields_values} -> length(fields_values) end)
Enum.map(keys_fields_values, fn {_, fields_values} -> enum_length(fields_values) end)

expiry_resps = Enum.map(keys_fields_values, fn _ -> 1 end)
{:ok, command_resps ++ expiry_resps}
Expand Down Expand Up @@ -268,4 +268,7 @@ defmodule Cache.Sandbox do
end
end)
end

defp enum_length(m) when is_map(m), do: m |> Map.to_list() |> enum_length()
defp enum_length(l), do: length(l)
end
21 changes: 20 additions & 1 deletion test/cache_sandbox_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ defmodule CacheSandboxTest do
:ok = TestCache.json_set(json_test_key, @json_test_value)

%{key: json_test_key}

end

describe "sandboxing caches" do
Expand Down Expand Up @@ -114,4 +113,24 @@ end
assert {:ok, "some value"} === TestCache.json_get(key, ["."])
end
end

describe "&hash_set_many/2" do
test "returns ok tuple when ttl is passed", %{key: key} do
assert {:ok, [1, 1]} = TestCache.hash_set_many([{key, [{"some_key", "some_value"}]}], 1_000)
end

test "returns ok atom when ttl is not passed", %{key: key} do
assert :ok = TestCache.hash_set_many([{key, [{"some_key", "some_value"}]}])
end

test "accepts map and list values", %{key: key} do
assert {:ok, [1, 1]} = TestCache.hash_set_many([{key, [{"some_key", "some_value"}]}], 1_000)

assert {:ok, [1, 1]} = TestCache.hash_set_many([{key, %{"some_key" => "some_value"}}], 1_000)

assert {:ok, [1, 1]} = TestCache.hash_set_many(%{key => [{"some_key", "some_value"}]}, 1_000)

assert {:ok, [1, 1]} = TestCache.hash_set_many(%{key => %{"some_key" => "some_value"}}, 1_000)
end
end
end

0 comments on commit 26c9d8f

Please sign in to comment.