Skip to content

Commit 870ddc4

Browse files
committed
test: JsonCacheWrap, JsonCacheMem and JsonCacheFake.
Closes #22
1 parent 5730204 commit 870ddc4

File tree

8 files changed

+116
-12
lines changed

8 files changed

+116
-12
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
<!-- @todo #1 Describe initial release. -->
10+
## [0.1.0]
11+
12+
### Added
13+
14+
- JsonCache interface and the JsonFake, JsonWrap, and JsonMem implementations.
1115

1216
## [0.0.1]
17+
18+
- structural organization:
19+
- linter setup
20+
- CI/CD pipelines
21+
- dependencies
22+
- README file
23+
- CHANGELOG file

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ Rultor.com](https://www.rultor.com/b/dartoos-dev/json_cache)](https://www.rultor
2828

2929
## Overview
3030

31-
**Json Cache** is an object-oriented layer on top of local storage packages that
31+
**Json Cache** is an object-oriented package to cache user data locally in json.
32+
It can also be thought of as a layer on top of Flutter's local storage packages
33+
like [sharable_preferences](https://pub.dev/packages/shared_preferences)that
3234
unifies them as an elegant caching API.
3335

36+
3437
In addition, this package gives developers great flexibility by providing a set
3538
of classes that can be selected and grouped in various combinations to meet
3639
specific cache requirements.

analysis_options.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ linter:
1313
sort_constructors_first: true
1414
# Good packages document everything
1515
public_member_api_docs: true
16+
# Always await.
17+
unawaited_futures: true
1618
always_declare_return_types: true
1719
cancel_subscriptions: true
1820
close_sinks: true

lib/src/json_cache.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ abstract class JsonCache {
77
/// previous data there.
88
Future<void> refresh(String key, Map<String, dynamic> data);
99

10-
/// Removes data from cache at [key] and returns it or returns null if there
11-
/// is no data at [key].
10+
/// Removes data from cache at [key]. It returns the removed data or null if
11+
/// no removal was performed — there was no data at [key].
1212
Future<Map<String, dynamic>?> erase(String key);
1313

1414
/// Retrieves either the data at [key] or null if a cache miss occurs.

lib/src/json_cache_fake.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class JsonCacheFake implements JsonCache {
1313
/// in-memory storage.
1414
final Map<String, Map<String, dynamic>?> _memory;
1515

16-
static final Map<String, Map<String, dynamic>> _shrMem = {};
16+
static late final Map<String, Map<String, dynamic>?> _shrMem = {};
1717

1818
/// Clears the internal map.
1919
@override

lib/src/json_cache_mem.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class JsonCacheMem implements JsonCache {
3737
final ReadWriteMutex _mutex;
3838

3939
/// in-memory shared storage.
40-
static final Map<String, Map<String, dynamic>> _shrMem = {};
40+
static late final Map<String, Map<String, dynamic>?> _shrMem = {};
4141

4242
/// shared mutex.
43-
static final _shrMutex = ReadWriteMutex();
43+
static late final _shrMutex = ReadWriteMutex();
4444

4545
/// Frees up storage space in both the level2 cache and in-memory cache.
4646
@override

test/json_cache_mem_test.dart

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,54 @@ import 'package:json_cache/json_cache.dart';
33

44
void main() {
55
group('JsonCacheMem', () {
6-
test('clear', () async {
6+
const profKey = 'profile';
7+
const Map<String, dynamic> profData = <String, dynamic>{
8+
'id': 1,
9+
'name': 'John Due'
10+
};
11+
group('clear, recover and refresh', () {
12+
test('default ctor', () async {
13+
final JsonCacheMem inMemCache = JsonCacheMem(JsonCacheFake());
14+
await inMemCache.refresh(profKey, profData);
15+
final result = await inMemCache.recover(profKey);
16+
expect(profData, result);
17+
await inMemCache.clear();
18+
final mustBeNull = await inMemCache.recover(profKey);
19+
expect(mustBeNull, isNull);
20+
});
21+
test('mem ctor', () async {
22+
final Map<String, Map<String, dynamic>?> copy = {
23+
profKey: Map<String, dynamic>.of(profData)
24+
};
25+
final inMemCache = JsonCacheMem.mem(JsonCacheFake.mem(copy), copy);
26+
final result = await inMemCache.recover(profKey);
27+
expect(result, profData);
28+
await inMemCache.clear();
29+
expect(copy.isEmpty, true);
30+
final mustBeNull = await inMemCache.recover(profKey);
31+
expect(mustBeNull, isNull);
32+
});
33+
});
34+
35+
test('erase', () async {
36+
const profKey = 'profile';
37+
const prefsKey = 'preferences';
38+
final prof = <String, dynamic>{'id': 1, 'name': 'John Due'};
39+
final prefs = <String, dynamic>{
40+
'preferences': <String, dynamic>{
41+
'theme': 'dark',
42+
'notifications': {'enabled': true}
43+
}
44+
};
745
final Map<String, Map<String, dynamic>> data = {
8-
'profile': <String, dynamic>{'id': 1, 'name': 'John Due'}
46+
profKey: prof,
47+
prefsKey: prefs
948
};
10-
await JsonCacheMem.mem(JsonCacheFake.mem(data), data).clear();
11-
expect(data.isEmpty, true);
49+
expect(data.containsKey(profKey), true);
50+
expect(data.containsKey(prefsKey), true);
51+
await JsonCacheMem.mem(JsonCacheFake.mem(data), data).erase(prefsKey);
52+
expect(data.containsKey(prefsKey), false);
53+
expect(data.containsKey(profKey), true);
1254
});
1355
});
1456
}

test/json_cache_wrap_test.dart

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,51 @@
11
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:json_cache/json_cache.dart';
3+
4+
class JsonCacheFakeWrap extends JsonCacheWrap {
5+
JsonCacheFakeWrap() : super(JsonCacheFake());
6+
}
27

38
void main() {
4-
group('JsonCacheWrap', () {});
9+
group('JsonCacheWrap', () {
10+
const profKey = 'profile';
11+
const Map<String, dynamic> profData = <String, dynamic>{
12+
'id': 1,
13+
'name': 'John Due'
14+
};
15+
group('clear, recover and refresh', () {
16+
test('default ctor', () async {
17+
final JsonCache wrap = JsonCacheFakeWrap();
18+
await wrap.refresh(profKey, profData);
19+
final result = await wrap.recover(profKey);
20+
expect(profData, result);
21+
await wrap.clear();
22+
final mustBeNull = await wrap.recover(profKey);
23+
expect(mustBeNull, isNull);
24+
});
25+
});
26+
27+
test('erase', () async {
28+
const profKey = 'profile';
29+
const prefsKey = 'preferences';
30+
final prof = <String, dynamic>{'id': 1, 'name': 'John Due'};
31+
final prefs = <String, dynamic>{
32+
'preferences': <String, dynamic>{
33+
'theme': 'dark',
34+
'notifications': {'enabled': true}
35+
}
36+
};
37+
final Map<String, Map<String, dynamic>> data = {
38+
profKey: prof,
39+
prefsKey: prefs
40+
};
41+
expect(data.containsKey(profKey), true);
42+
expect(data.containsKey(prefsKey), true);
43+
final wrap = JsonCacheFakeWrap();
44+
await wrap.refresh(prefsKey, prefs);
45+
final erasedPrefs = await wrap.erase(prefsKey);
46+
expect(erasedPrefs, prefs);
47+
final mustBeNull = await wrap.recover(prefsKey);
48+
expect(mustBeNull, isNull);
49+
});
50+
});
551
}

0 commit comments

Comments
 (0)