|
1 | 1 | defmodule Nebulex.Adapters.Cachex.QueryableTest do |
2 | 2 | import Nebulex.CacheCase |
3 | 3 |
|
4 | | - deftests "queryable" do |
| 4 | + deftests do |
5 | 5 | import Nebulex.CacheHelpers |
6 | 6 |
|
7 | 7 | alias Cachex.Query |
8 | 8 |
|
9 | | - test "all", %{cache: cache} do |
10 | | - set1 = cache_put(cache, 1..50) |
11 | | - set2 = cache_put(cache, 51..100) |
| 9 | + describe "all" do |
| 10 | + test "returns all keys in cache", %{cache: cache} do |
| 11 | + set1 = cache_put(cache, 1..50) |
| 12 | + set2 = cache_put(cache, 51..100) |
12 | 13 |
|
13 | | - for x <- 1..100, do: assert(cache.get(x) == x) |
14 | | - expected = set1 ++ set2 |
| 14 | + for x <- 1..100, do: assert(cache.get(x) == x) |
| 15 | + expected = set1 ++ set2 |
15 | 16 |
|
16 | | - assert :lists.usort(cache.all()) == expected |
| 17 | + assert :lists.usort(cache.all()) == expected |
17 | 18 |
|
18 | | - set3 = Enum.to_list(20..60) |
19 | | - :ok = Enum.each(set3, &cache.delete(&1)) |
20 | | - expected = :lists.usort(expected -- set3) |
| 19 | + set3 = Enum.to_list(20..60) |
| 20 | + :ok = Enum.each(set3, &cache.delete(&1)) |
| 21 | + expected = :lists.usort(expected -- set3) |
21 | 22 |
|
22 | | - assert :lists.usort(cache.all()) == expected |
| 23 | + assert :lists.usort(cache.all()) == expected |
| 24 | + end |
23 | 25 | end |
24 | 26 |
|
25 | | - test "stream", %{cache: cache} do |
26 | | - entries = for x <- 1..10, into: %{}, do: {x, x * 2} |
27 | | - assert cache.put_all(entries) == :ok |
| 27 | + describe "stream" do |
| 28 | + @entries for x <- 1..10, into: %{}, do: {x, x * 2} |
| 29 | + |
| 30 | + test "returns all keys in cache", %{cache: cache} do |
| 31 | + :ok = cache.put_all(@entries) |
| 32 | + |
| 33 | + assert nil |
| 34 | + |> cache.stream() |
| 35 | + |> Enum.to_list() |
| 36 | + |> :lists.usort() == Map.keys(@entries) |
| 37 | + end |
| 38 | + |
| 39 | + test "returns all values in cache", %{cache: cache} do |
| 40 | + :ok = cache.put_all(@entries) |
28 | 41 |
|
29 | | - expected = Map.keys(entries) |
| 42 | + assert true |
| 43 | + |> Query.create(:key) |
| 44 | + |> cache.stream(return: :value, page_size: 3) |
| 45 | + |> Enum.to_list() |
| 46 | + |> :lists.usort() == Map.values(@entries) |
| 47 | + end |
30 | 48 |
|
31 | | - assert true |
32 | | - |> Query.create(:key) |
33 | | - |> cache.stream() |
34 | | - |> Enum.to_list() |
35 | | - |> :lists.usort() == expected |
| 49 | + test "returns all key/value pairs in cache", %{cache: cache} do |
| 50 | + :ok = cache.put_all(@entries) |
36 | 51 |
|
37 | | - assert true |
38 | | - |> Query.create(:key) |
39 | | - |> cache.stream(page_size: 3) |
40 | | - |> Enum.to_list() |
41 | | - |> :lists.usort() == expected |
| 52 | + assert true |
| 53 | + |> Query.create(:key) |
| 54 | + |> cache.stream(return: {:key, :value}, page_size: 3) |
| 55 | + |> Enum.to_list() |
| 56 | + |> :lists.usort() == :maps.to_list(@entries) |
| 57 | + end |
| 58 | + |
| 59 | + test "returns what is dictated by the built query", %{cache: cache} do |
| 60 | + :ok = cache.put_all(@entries) |
| 61 | + |
| 62 | + expected = |
| 63 | + :lists.zip3( |
| 64 | + Map.keys(@entries), |
| 65 | + Map.values(@entries), |
| 66 | + List.duplicate(nil, map_size(@entries)) |
| 67 | + ) |
| 68 | + |
| 69 | + assert true |
| 70 | + |> Query.create({:key, :value, :ttl}) |
| 71 | + |> cache.stream(page_size: 3) |
| 72 | + |> Enum.to_list() |
| 73 | + |> :lists.usort() == expected |
| 74 | + end |
42 | 75 |
|
43 | | - assert_raise Nebulex.QueryError, fn -> |
44 | | - :invalid_query |
45 | | - |> cache.stream() |
46 | | - |> Enum.to_list() |
| 76 | + test "raises when query is invalid", %{cache: cache} do |
| 77 | + assert_raise Nebulex.QueryError, fn -> |
| 78 | + :invalid_query |
| 79 | + |> cache.stream() |
| 80 | + |> Enum.to_list() |
| 81 | + end |
47 | 82 | end |
48 | 83 | end |
49 | 84 | end |
|
0 commit comments