| 42 | |
| 43 | func TestLocalDataCache(t *testing.T) { |
| 44 | // Test for Get Method |
| 45 | getCacheDataTests := []getCacheDataTest{ |
| 46 | { |
| 47 | name: "Cache Hit", |
| 48 | cacheData: map[string]string{"hello": "world"}, |
| 49 | key: "hello", |
| 50 | expectedValue: "world", |
| 51 | expectedErr: nil, |
| 52 | }, |
| 53 | { |
| 54 | name: "Cache Miss", |
| 55 | cacheData: map[string]string{}, // Empty cache |
| 56 | key: "missing-key", |
| 57 | expectedValue: "", |
| 58 | expectedErr: cachetypes.ErrCachedDataNotFound, |
| 59 | }, |
| 60 | } |
| 61 | for _, tt := range getCacheDataTests { |
| 62 | t.Run(tt.name, func(t *testing.T) { |
| 63 | cache := NewLocalDataCache[string, string](nil) |
| 64 | cache.data = tt.cacheData |
| 65 | val, err := cache.Get(context.Background(), tt.key) |
| 66 | |
| 67 | if !errors.Is(err, tt.expectedErr) { |
| 68 | t.Errorf("Expected error: %v, Got: %v", tt.expectedErr, err) |
| 69 | } |
| 70 | if !reflect.DeepEqual(val, tt.expectedValue) { |
| 71 | t.Errorf("Expected value: %v, Got: %v", tt.expectedValue, val) |
| 72 | } |
| 73 | }) |
| 74 | } |
| 75 | |
| 76 | // Test for Cache Method |
| 77 | cacheDataTests := []cacheDataTest{ |
| 78 | { |
| 79 | name: "Add New Entry", |
| 80 | cacheData: map[string]string{}, |
| 81 | key: "new-key", |
| 82 | value: "new-value", |
| 83 | expectedError: nil, |
| 84 | }, |
| 85 | { |
| 86 | name: "Overwrite Existing", |
| 87 | cacheData: map[string]string{"existing": "old"}, |
| 88 | key: "existing", |
| 89 | value: "updated", |
| 90 | expectedError: nil, |
| 91 | }, |
| 92 | } |
| 93 | for idx, tt := range cacheDataTests { |
| 94 | t.Run(tt.name, func(t *testing.T) { |
| 95 | cache := NewLocalDataCache[string, string](nil) |
| 96 | cache.data = tt.cacheData |
| 97 | err := cache.Cache(context.Background(), tt.key, cacheDataTests[idx].value) |
| 98 | |
| 99 | if !errors.Is(err, tt.expectedError) { |
| 100 | t.Errorf("Expected error: %v, Got: %v", tt.expectedError, err) |
| 101 | } |