From fd0f96cd924cbdf8312ffa75f153ce74185c4580 Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 11 Aug 2026 22:02:34 +0200 Subject: [PATCH] add cache tests --- test/chesstrainer/cache_test.exs | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/chesstrainer/cache_test.exs diff --git a/test/chesstrainer/cache_test.exs b/test/chesstrainer/cache_test.exs new file mode 100644 index 0000000..1de00d8 --- /dev/null +++ b/test/chesstrainer/cache_test.exs @@ -0,0 +1,39 @@ +defmodule Chesstrainer.CacheTest do + use ExUnit.Case, async: true + + alias Chesstrainer.Cache + + setup do + table = :"test_#{System.unique_integer([:positive])}" + {:ok, pid} = Cache.start_link(table) + on_exit(fn -> if Process.alive?(pid), do: GenServer.stop(pid) end) + {:ok, %{pid: pid, table: table}} + end + + describe "ChessTrainer.Cache function tests" do + test "cache is started", %{pid: pid, table: table} do + assert is_pid(pid) + assert 0 = Cache.size(table) + end + + test "put/3", %{table: table} do + assert :ok = Cache.put(table, "key1", "value1") + assert {:ok, "value1"} = Cache.get(table, "key1") + assert 1 = Cache.size(table) + + assert :ok = Cache.put(table, "key2", "value2") + assert {:ok, "value2"} = Cache.get(table, "key2") + assert 2 = Cache.size(table) + end + + test "delete/2", %{table: table} do + assert :ok = Cache.put(table, "key1", "value1") + assert :ok = Cache.put(table, "key2", "value2") + assert 2 = Cache.size(table) + + assert :ok = Cache.delete(table, "key1") + assert 1 = Cache.size(table) + assert :error = Cache.get(table, "key1") + end + end +end