github.com/grafviktor/keep-my-secret@v0.9.10-0.20230908165355-19f35cce90e5/internal/keycache/data_key_cache_test.go (about)

     1  package keycache
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/grafviktor/keep-my-secret/internal/constant"
     8  )
     9  
    10  func TestSingletonDataKeyCache(t *testing.T) {
    11  	// Create a new singleton instance
    12  	cache := GetInstance()
    13  
    14  	// Test setting and getting data key
    15  	login := "testuser"
    16  	key := "testkey"
    17  
    18  	cache.Set(login, key)
    19  
    20  	result, err := cache.Get(login)
    21  	if err != nil {
    22  		t.Errorf("Expected no error, but got an error: %v", err)
    23  	}
    24  
    25  	if result != key {
    26  		t.Errorf("Expected data key %s, but got %s", key, result)
    27  	}
    28  
    29  	// Test getting a non-existent data key
    30  	nonExistentLogin := "nonexistentuser"
    31  	_, err = cache.Get(nonExistentLogin)
    32  
    33  	if !errors.Is(err, constant.ErrNotFound) {
    34  		t.Errorf("Expected ErrNotFound, but got %v", err)
    35  	}
    36  }