Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Local.get_all with false values #186

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/nebulex/adapters/local.ex
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,10 @@ defmodule Nebulex.Adapters.Local do
adapter_meta = %{adapter_meta | telemetry: Map.get(adapter_meta, :in_span?, false)}

Enum.reduce(keys, %{}, fn key, acc ->
if obj = get(adapter_meta, key, []),
do: Map.put(acc, key, obj),
else: acc
case get(adapter_meta, key, []) do
nil -> acc
obj -> Map.put(acc, key, obj)
end
end)
end

Expand Down
101 changes: 101 additions & 0 deletions test/nebulex/adapters/local_boolean_keys_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
defmodule Nebulex.Adapters.LocalBooleanKeysTest do
use ExUnit.Case, async: true

defmodule ETS do
use Nebulex.Cache,
otp_app: :nebulex,
adapter: Nebulex.Adapters.Local
end

defmodule Shards do
use Nebulex.Cache,
otp_app: :nebulex,
adapter: Nebulex.Adapters.Local
end

alias Nebulex.Adapters.LocalBooleanKeysTest.{ETS, Shards}

setup do
{:ok, ets} = ETS.start_link()
{:ok, shards} = Shards.start_link(backend: :shards)

on_exit(fn ->
:ok = Process.sleep(100)
if Process.alive?(ets), do: ETS.stop()
if Process.alive?(shards), do: Shards.stop()
end)

{:ok, caches: [ETS, Shards]}
end

describe "boolean keys" do
test "get and get_all", %{caches: caches} do
for_all_caches(caches, fn cache ->
:ok = cache.put_all(a: true, b: false)

assert cache.get(:a) == true
assert cache.get(:b) == false

assert cache.get_all([:a, :b]) == %{a: true, b: false}
end)
end

test "take", %{caches: caches} do
for_all_caches(caches, fn cache ->
:ok = cache.put_all(a: true, b: false)

assert cache.take(:a) == true
assert cache.take(:b) == false

assert cache.get_all([:a, :b]) == %{}
end)
end

test "delete true value", %{caches: caches} do
for_all_caches(caches, fn cache ->
:ok = cache.put(:a, true)

assert cache.get(:a) == true
assert cache.delete(:a)
assert cache.get(:a) == nil
end)
end

test "delete false value", %{caches: caches} do
for_all_caches(caches, fn cache ->
:ok = cache.put(:a, false)

assert cache.get(:a) == false
assert cache.delete(:a)
assert cache.get(:a) == nil
end)
end

test "put_new", %{caches: caches} do
for_all_caches(caches, fn cache ->
assert cache.put_new(:a, true)
:ok = cache.put(:a, false)
refute cache.put_new(:a, false)

assert cache.get(:a) == false
end)
end

test "has_key?", %{caches: caches} do
for_all_caches(caches, fn cache ->
:ok = cache.put(:a, true)

assert cache.has_key?(:a)
refute cache.has_key?(:b)
end)
end
end

## Helpers

defp for_all_caches(caches, fun) do
Enum.each(caches, fn cache ->
fun.(cache)
end)
end
end