Skip to content

Commit 3d33c4d

Browse files
committed
feat: 'keys' method to list all cache keys
Closes #152
1 parent eb8004f commit 3d33c4d

10 files changed

+96
-82
lines changed

test/flutter_secure_storage_mock.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,14 @@ class FlutterSecureStorageMock implements FlutterSecureStorage {
9090
WebOptions? webOptions,
9191
MacOsOptions? mOptions,
9292
WindowsOptions? wOptions,
93-
}) {
94-
// TODO: implement readAll
95-
throw UnimplementedError();
93+
}) async {
94+
// final jsonObj = await _fakeCache.value(key);
95+
final keys = await _fakeCache.keys();
96+
final data = <String, String>{};
97+
for (final key in keys) {
98+
data[key] = json.encode(await _fakeCache.value(key));
99+
}
100+
return data;
96101
}
97102

98103
@override

test/json_cache_fake_test.dart

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import 'package:json_cache/json_cache.dart';
44
void main() {
55
group('JsonCacheFake', () {
66
const profKey = 'profile';
7-
const profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
87
const prefKey = 'preferences';
8+
const profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
99
const prefData = <String, dynamic>{
1010
'theme': 'dark',
1111
'notifications': {'enabled': true},
@@ -52,11 +52,6 @@ void main() {
5252
});
5353

5454
test('contains', () async {
55-
final profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
56-
final prefData = <String, dynamic>{
57-
'theme': 'dark',
58-
'notifications': {'enabled': true},
59-
};
6055
final fake = JsonCacheFake();
6156
// update data
6257
await fake.refresh(profKey, profData);
@@ -71,11 +66,6 @@ void main() {
7166
expect(await fake.contains('a key'), false);
7267
});
7368
test('keys', () async {
74-
final profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
75-
final prefData = <String, dynamic>{
76-
'theme': 'dark',
77-
'notifications': {'enabled': true},
78-
};
7969
final fake = JsonCacheFake();
8070
// update data
8171
await fake.refresh(profKey, profData);

test/json_cache_flutter_secure_storage_test.dart

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ import 'flutter_secure_storage_mock.dart';
55

66
void main() {
77
group('JsonCacheFlutterSecureStorage', () {
8+
const profKey = 'profile';
9+
const prefKey = 'preferences';
10+
const profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
11+
const prefData = <String, dynamic>{
12+
'theme': 'dark',
13+
'notifications': {'enabled': true},
14+
};
815
test('clear, value, refresh', () async {
916
final secStorageMock = FlutterSecureStorageMock();
1017
final JsonCacheFlutterSecureStorage flutterSecureCache =
1118
JsonCacheFlutterSecureStorage(secStorageMock);
12-
const profKey = 'profile';
13-
const profData = <String, Object>{'id': 1, 'name': 'John Due'};
1419
await flutterSecureCache.refresh(profKey, profData);
1520
expect(secStorageMock.writeInvokations, 1);
1621

@@ -27,13 +32,6 @@ void main() {
2732
});
2833

2934
test('contains', () async {
30-
const profKey = 'profile';
31-
const prefKey = 'preferences';
32-
final profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
33-
final prefData = <String, dynamic>{
34-
'theme': 'dark',
35-
'notifications': {'enabled': true},
36-
};
3735
final secStorageMock = FlutterSecureStorageMock();
3836
final JsonCacheFlutterSecureStorage flutterSecureCache =
3937
JsonCacheFlutterSecureStorage(secStorageMock);
@@ -52,17 +50,20 @@ void main() {
5250
await flutterSecureCache.remove(prefKey);
5351
expect(await flutterSecureCache.contains(prefKey), false);
5452
});
53+
test('keys', () async {
54+
final secStorageMock = FlutterSecureStorageMock();
55+
final JsonCacheFlutterSecureStorage flutterSecureCache =
56+
JsonCacheFlutterSecureStorage(secStorageMock);
57+
// update data
58+
await flutterSecureCache.refresh(profKey, profData);
59+
await flutterSecureCache.refresh(prefKey, prefData);
60+
61+
expect(await flutterSecureCache.keys(), [profKey, prefKey]);
62+
});
5563
test('remove', () async {
5664
final secStorageMock = FlutterSecureStorageMock();
5765
final JsonCacheFlutterSecureStorage flutterSecureCache =
5866
JsonCacheFlutterSecureStorage(secStorageMock);
59-
const profKey = 'profile';
60-
const prefKey = 'preferences';
61-
final profData = <String, Object>{'id': 1, 'name': 'John Due'};
62-
final prefData = <String, Object>{
63-
'theme': 'dark',
64-
'notifications': {'enabled': true},
65-
};
6667
await flutterSecureCache.refresh(profKey, profData);
6768
await flutterSecureCache.refresh(prefKey, prefData);
6869
expect(secStorageMock.writeInvokations, 2);

test/json_cache_hive_test.dart

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ void main() {
1111
await tearDownTestHive();
1212
});
1313
group('JsonCacheHive', () {
14+
const profKey = 'profile';
15+
const prefKey = 'preferences';
16+
const profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
17+
const prefData = <String, dynamic>{
18+
'theme': 'dark',
19+
'notifications': {'enabled': true},
20+
};
1421
test('clear, value, refresh', () async {
1522
final box = await Hive.openBox<String>('test-clear-value-refresh');
16-
const profKey = 'profile';
17-
const profData = <String, Object>{'id': 1, 'name': 'John Due'};
1823
final JsonCacheHive hiveCache = JsonCacheHive(box);
1924
await hiveCache.refresh(profKey, profData);
2025
var prof = await hiveCache.value(profKey);
@@ -25,13 +30,6 @@ void main() {
2530
});
2631

2732
test('contains', () async {
28-
const profKey = 'profile';
29-
const prefKey = 'preferences';
30-
final profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
31-
final prefData = <String, dynamic>{
32-
'theme': 'dark',
33-
'notifications': {'enabled': true},
34-
};
3533
final box = await Hive.openBox<String>('test-contains-method');
3634
final JsonCacheHive hiveCache = JsonCacheHive(box);
3735
// update data
@@ -49,14 +47,17 @@ void main() {
4947
await hiveCache.remove(prefKey);
5048
expect(await hiveCache.contains(prefKey), false);
5149
});
50+
51+
test('keys', () async {
52+
final box = await Hive.openBox<String>('test-contains-method');
53+
final JsonCacheHive hiveCache = JsonCacheHive(box);
54+
// update data
55+
await hiveCache.refresh(profKey, profData);
56+
await hiveCache.refresh(prefKey, prefData);
57+
58+
expect(await hiveCache.keys(), [prefKey, profKey]);
59+
});
5260
test('remove', () async {
53-
const profKey = 'profile';
54-
const prefKey = 'preferences';
55-
final profData = <String, Object>{'id': 1, 'name': 'John Due'};
56-
final prefData = <String, Object>{
57-
'theme': 'dark',
58-
'notifications': {'enabled': true},
59-
};
6061
final box = await Hive.openBox<String>('test-remove');
6162
final hiveCache = JsonCacheHive(box);
6263
await hiveCache.refresh(profKey, profData);

test/json_cache_local_storage_test.dart

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import 'fake_local_storage.dart';
55

66
void main() {
77
group('JsonCacheLocalStorage', () {
8+
const profKey = 'profile';
9+
const profData = <String, Object>{'id': 1, 'name': 'John Due'};
10+
const prefData = <String, dynamic>{
11+
'theme': 'dark',
12+
'notifications': {'enabled': true},
13+
};
814
test('clear, recover and refresh', () async {
9-
const profKey = 'profile';
10-
const profData = <String, Object>{'id': 1, 'name': 'John Due'};
1115
final JsonCacheLocalStorage jsonCache = _fakeInstance;
1216
await jsonCache.refresh(profKey, profData);
1317
var prof = await jsonCache.value(profKey);
@@ -18,13 +22,7 @@ void main() {
1822
});
1923

2024
test('contains', () async {
21-
const profKey = 'profile';
2225
const prefKey = 'preferences';
23-
final profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
24-
final prefData = <String, dynamic>{
25-
'theme': 'dark',
26-
'notifications': {'enabled': true},
27-
};
2826
final JsonCacheLocalStorage jsonCache = _fakeInstance;
2927
// update data
3028
await jsonCache.refresh(profKey, profData);
@@ -41,14 +39,18 @@ void main() {
4139
await jsonCache.remove(prefKey);
4240
expect(await jsonCache.contains(prefKey), false);
4341
});
42+
43+
test('keys', () async {
44+
const prefKey = 'preferences';
45+
final JsonCacheLocalStorage jsonCache = _fakeInstance;
46+
// update data
47+
await jsonCache.refresh(profKey, profData);
48+
await jsonCache.refresh(prefKey, prefData);
49+
50+
expect(await jsonCache.keys(), [profKey, prefKey]);
51+
});
4452
test('remove', () async {
45-
const profKey = 'profile';
4653
const prefKey = 'preferences';
47-
final profData = <String, Object>{'id': 1, 'name': 'John Due'};
48-
final prefData = <String, Object>{
49-
'theme': 'dark',
50-
'notifications': {'enabled': true},
51-
};
5254
final JsonCacheLocalStorage jsonCache = _fakeInstance;
5355
await jsonCache.refresh(profKey, profData);
5456
await jsonCache.refresh(prefKey, prefData);

test/json_cache_mem_test.dart

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ void main() {
111111
});
112112
});
113113
test('contains', () async {
114-
final profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
115-
final prefData = <String, dynamic>{
116-
'theme': 'dark',
117-
'notifications': {'enabled': true},
118-
};
119114
final memCache = JsonCacheMem();
120115
// update data
121116
await memCache.refresh(profKey, profData);
@@ -131,11 +126,6 @@ void main() {
131126
expect(await memCache.contains('a key'), false);
132127
});
133128
test('keys', () async {
134-
final profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
135-
final prefData = <String, dynamic>{
136-
'theme': 'dark',
137-
'notifications': {'enabled': true},
138-
};
139129
final memCache = JsonCacheMem();
140130
// update data
141131
await memCache.refresh(profKey, profData);

test/json_cache_safe_local_storage_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ void main() {
123123
},
124124
);
125125

126+
test('keys', () async {
127+
// update data
128+
await jsonCacheSafeLocalStorage.refresh(profKey, profData);
129+
await jsonCacheSafeLocalStorage.refresh(prefKey, prefData);
130+
131+
expect(await jsonCacheSafeLocalStorage.keys(), [profKey, prefKey]);
132+
});
126133
test(
127134
'clear',
128135
() async {

test/json_cache_shared_preferences_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ void main() {
77
group('JsonCacheSharedPreferences', () {
88
const profKey = 'profile';
99
const prefKey = 'preferences';
10-
final profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
11-
final prefData = <String, dynamic>{
10+
const profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
11+
const prefData = <String, dynamic>{
1212
'theme': 'dark',
1313
'notifications': {'enabled': true},
1414
};

test/json_cache_try_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ void main() {
4242
// Check if the interaction occurred only once.
4343
verify(() => jsonCacheMock.contains('aKey')).called(1);
4444
});
45+
test('keys should throw "JsonCacheException"', () async {
46+
final JsonCacheTry jsonCacheTry = JsonCacheTry(jsonCacheMock);
47+
// Stub the 'contains' method.
48+
when(() => jsonCacheMock.keys()).thenThrow(Exception('Cache Failure'));
49+
// Verify no interactions have occurred.
50+
verifyNever(() => jsonCacheMock.keys());
51+
// Interact with the jsonCacheTry instance.
52+
expect(
53+
() async => jsonCacheTry.keys(),
54+
throwsA(const TypeMatcher<JsonCacheException>()),
55+
);
56+
// Check if the interaction occurred only once.
57+
verify(() => jsonCacheMock.keys()).called(1);
58+
});
4559
test('refresh should throw "JsonCacheException"', () async {
4660
final JsonCacheTry jsonCacheTry = JsonCacheTry(jsonCacheMock);
4761
// Stub the 'refresh' method.

test/json_cache_wrap_test.dart

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ void main() {
99
group('JsonCacheWrap', () {
1010
const profKey = 'profile';
1111
const prefKey = 'preferences';
12+
const profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
13+
const prefData = <String, dynamic>{
14+
'theme': 'dark',
15+
'notifications': {'enabled': true},
16+
};
1217
group('clear, value and refresh', () {
1318
test('default ctor', () async {
1419
const Map<String, dynamic> data = <String, dynamic>{
@@ -25,11 +30,6 @@ void main() {
2530
});
2631
});
2732
test('contains', () async {
28-
final profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
29-
final prefData = <String, dynamic>{
30-
'theme': 'dark',
31-
'notifications': {'enabled': true},
32-
};
3333
final wrap = JsonCacheTestWrap();
3434

3535
// update data
@@ -45,12 +45,16 @@ void main() {
4545
expect(await wrap.contains(prefKey), false);
4646
expect(await wrap.contains('a key'), false);
4747
});
48+
test('keys', () async {
49+
final wrap = JsonCacheTestWrap();
50+
51+
// update data
52+
await wrap.refresh(profKey, profData);
53+
await wrap.refresh(prefKey, prefData);
54+
55+
expect(await wrap.keys(), [profKey, prefKey]);
56+
});
4857
test('remove', () async {
49-
final profData = <String, dynamic>{'id': 1, 'name': 'John Due'};
50-
final prefData = <String, dynamic>{
51-
'theme': 'dark',
52-
'notifications': {'enabled': true},
53-
};
5458
final wrap = JsonCacheTestWrap();
5559
await wrap.refresh(profKey, profData);
5660
await wrap.refresh(prefKey, prefData);

0 commit comments

Comments
 (0)