diff options
author | Hubert Chathi <hubert@uhoreg.ca> | 2019-10-06 09:21:43 -0400 |
---|---|---|
committer | Hubert Chathi <hubert@uhoreg.ca> | 2019-10-06 09:22:10 -0400 |
commit | b1ac9384a8c84e72bd42f30893b7c523f22b2049 (patch) | |
tree | 8b982d4234337c742efabcd1d48c87fe7af822cd | |
parent | add support for filters (diff) |
add test for Ets storage (and fix some issues)
-rw-r--r-- | lib/polyjuice/client/storage/ets.ex | 8 | ||||
-rw-r--r-- | test/polyjuice/client/storage/ets_test.exs | 54 |
2 files changed, 58 insertions, 4 deletions
diff --git a/lib/polyjuice/client/storage/ets.ex b/lib/polyjuice/client/storage/ets.ex index e3a6e0c..f8a2b03 100644 --- a/lib/polyjuice/client/storage/ets.ex +++ b/lib/polyjuice/client/storage/ets.ex @@ -49,25 +49,25 @@ defmodule Polyjuice.Client.Storage.Ets do def set_filter_id(%{table: table}, filter, id) when is_map(filter) and is_binary(id) do {:ok, json} = Polyjuice.Util.JSON.canonical_json(filter) hash = :crypto.hash(:sha256, json) - :dets.insert(table, {"filter_" <> hash, id}) + :ets.insert(table, {"filter_" <> hash, id}) end def get_filter_id(%{table: table}, filter) do {:ok, json} = Polyjuice.Util.JSON.canonical_json(filter) hash = :crypto.hash(:sha256, json) - case :dets.lookup(table, "filter_" <> hash) do + case :ets.lookup(table, "filter_" <> hash) do [{_, id}] -> id _ -> nil end end def kv_put(%{table: table}, key, value) when is_binary(key) do - :dets.insert(table, {"kv_" <> key, value}) + :ets.insert(table, {"kv_" <> key, value}) end def kv_get(%{table: table}, key, default \\ nil) when is_binary(key) do - case :dets.lookup(table, "kv_" <> key) do + case :ets.lookup(table, "kv_" <> key) do [{_, value}] -> value _ -> default end diff --git a/test/polyjuice/client/storage/ets_test.exs b/test/polyjuice/client/storage/ets_test.exs new file mode 100644 index 0000000..115c757 --- /dev/null +++ b/test/polyjuice/client/storage/ets_test.exs @@ -0,0 +1,54 @@ +# Copyright 2019 Hubert Chathi <hubert@uhoreg.ca> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Storage.EtsTest do + use ExUnit.Case + alias Polyjuice.Client.Storage + alias Polyjuice.Client.Filter + + test "ets" do + ets = Storage.Ets.open() + + assert Storage.get_sync_token(ets) == nil + + Storage.set_sync_token(ets, "token") + assert Storage.get_sync_token(ets) == "token" + + Storage.set_sync_token(ets, "token2") + assert Storage.get_sync_token(ets) == "token2" + + assert Storage.get_filter_id(ets, %{}) == nil + + Storage.set_filter_id(ets, %{}, "filterid1") + assert Storage.get_filter_id(ets, %{}) == "filterid1" + + assert Storage.get_filter_id(ets, Filter.lazy_loading()) == nil + + Storage.set_filter_id(ets, Filter.lazy_loading(), "filterid2") + assert Storage.get_filter_id(ets, Filter.lazy_loading()) == "filterid2" + assert Storage.get_filter_id(ets, %{}) == "filterid1" + + assert Storage.kv_get(ets, "key1") == nil + assert Storage.kv_get(ets, "key1", :default) == :default + + Storage.kv_put(ets, "key1", "value1") + assert Storage.kv_get(ets, "key1") == "value1" + + Storage.kv_put(ets, "key2", "value2") + assert Storage.kv_get(ets, "key2") == "value2" + assert Storage.kv_get(ets, "key1") == "value1" + + Storage.close(ets) + end +end |