From f914b3e3915f09c714a31c5903fa2dcde4a795d9 Mon Sep 17 00:00:00 2001 From: EdsonJunio Date: Wed, 22 Oct 2025 15:17:03 -0300 Subject: [PATCH] test(cache): ensure cache expiration, delete, and overwrite behaviors work correctly --- cache/client_test.go | 68 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/cache/client_test.go b/cache/client_test.go index 0e2b58e..b59ebe7 100644 --- a/cache/client_test.go +++ b/cache/client_test.go @@ -1 +1,67 @@ -package cache_test +package cache + +import ( + "testing" + "time" +) + +func TestPutAndGet(t *testing.T) { + c := NewCacheClient(2 * time.Second) + + ok := c.Put("city", "Curitiba") + if !ok { + t.Fatalf("expected Put to return true, but got false") + } + + val, found := c.Get("city") + if !found { + t.Fatalf("expected to find the item, but did not") + } + + if val != "Curitiba" { + t.Errorf("expected 'Curitiba', got '%v'", val) + } +} + +func TestExpiration(t *testing.T) { + c := NewCacheClient(500 * time.Millisecond) + + c.Put("temp", 25) + time.Sleep(600 * time.Millisecond) + _, found := c.Get("temp") + if found { + t.Error("expected item to expire, but it was still found") + } +} + +func TestDelete(t *testing.T) { + c := NewCacheClient(2 * time.Second) + + c.Put("wind", "10 km/h") + + ok := c.Delete("wind") + if !ok { + t.Fatalf("expected Delete to return true, but got false") + } + + _, found := c.Get("wind") + if found { + t.Error("expected item to be deleted, but it still exists") + } +} + +func TestOverwrite(t *testing.T) { + c := NewCacheClient(2 * time.Second) + + c.Put("city", "Curitiba") + c.Put("city", "Londrina") + + val, found := c.Get("city") + if !found { + t.Fatal("expected to find the item, but did not") + } + + if val != "Londrina" { + t.Errorf("expected 'Londrina', got '%v'", val) + } +}