-
Notifications
You must be signed in to change notification settings - Fork 401
/
storage_backend_js_test.dart
236 lines (200 loc) · 7.3 KB
/
storage_backend_js_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
@TestOn('browser')
import 'dart:async' show Future;
import 'dart:html';
import 'dart:indexed_db';
import 'dart:typed_data';
import 'package:hive/hive.dart';
import 'package:hive/src/backend/storage_backend_js.dart';
import 'package:hive/src/binary/binary_writer_impl.dart';
import 'package:hive/src/binary/frame.dart';
import 'package:hive/src/box/change_notifier.dart';
import 'package:hive/src/box/keystore.dart';
import 'package:hive/src/crypto_helper.dart';
import 'package:test/test.dart';
import 'frames.dart';
StorageBackendJs _getBackend({
Database db,
bool lazy = false,
CryptoHelper crypto,
TypeRegistry registry,
}) {
return StorageBackendJs(db, lazy, crypto, registry);
}
Future<Database> _openDb() async {
return await window.indexedDB.open('testBox', version: 1,
onUpgradeNeeded: (e) {
var db = e.target.result as Database;
if (!db.objectStoreNames.contains('box')) {
db.createObjectStore('box');
}
});
}
ObjectStore _getStore(Database db) {
return db.transaction('box', 'readwrite').objectStore('box');
}
Future<Database> _getDbWith(Map<String, dynamic> content) async {
var db = await _openDb();
var store = _getStore(db);
await store.clear();
content.forEach((k, v) => store.put(v, k));
return db;
}
void main() {
group('StorageBackendJs', () {
test('.path', () {
expect(_getBackend().path, null);
});
group('.encodeValue()', () {
test('primitive', () {
var values = [
null, 11, 17.25, true, 'hello', //
[11, 12, 13], [17.25, 17.26], [true, false], ['str1', 'str2'] //
];
var backend = _getBackend();
for (var value in values) {
expect(backend.encodeValue(Frame('key', value)), value);
}
var bytes = Uint8List.fromList([1, 2, 3]);
var buffer = backend.encodeValue(Frame('key', bytes)) as ByteBuffer;
expect(Uint8List.view(buffer), [1, 2, 3]);
});
test('crypto', () {
var backend = StorageBackendJs(null, false, testCrypto, testRegistry);
var i = 0;
for (var frame in testFrames) {
var buffer = backend.encodeValue(frame) as ByteBuffer;
var bytes = Uint8List.view(buffer);
expect(bytes.sublist(28),
[0x90, 0xA9, ...frameValuesBytesEncrypted[i]].sublist(28));
i++;
}
});
group('non primitive', () {
test('map', () {
var frame = Frame(0, {
'key': Uint8List.fromList([1, 2, 3]),
'otherKey': null
});
var backend = StorageBackendJs(null, false, null);
var encoded =
Uint8List.view(backend.encodeValue(frame) as ByteBuffer);
var writer = BinaryWriterImpl(null);
frame.encodeValue(writer, null);
expect(encoded, [0x90, 0xA9, ...writer.toBytes()]);
});
test('bytes which start with signature', () {
var frame = Frame(0, Uint8List.fromList([0x90, 0xA9, 1, 2, 3]));
var backend = _getBackend();
var encoded =
Uint8List.view(backend.encodeValue(frame) as ByteBuffer);
var writer = BinaryWriterImpl(null);
frame.encodeValue(writer, null);
expect(encoded, [0x90, 0xA9, ...writer.toBytes()]);
});
});
});
group('.decodeValue()', () {
test('primitive', () {
var backend = _getBackend();
expect(backend.decodeValue(null), null);
expect(backend.decodeValue(11), 11);
expect(backend.decodeValue(17.25), 17.25);
expect(backend.decodeValue(true), true);
expect(backend.decodeValue('hello'), 'hello');
expect(backend.decodeValue([11, 12, 13]), [11, 12, 13]);
expect(backend.decodeValue([17.25, 17.26]), [17.25, 17.26]);
var bytes = Uint8List.fromList([1, 2, 3]);
expect(backend.decodeValue(bytes.buffer), [1, 2, 3]);
});
test('crypto', () {
var crypto = CryptoHelper(Uint8List.fromList(List.filled(32, 1)));
var backend = _getBackend(crypto: crypto, registry: testRegistry);
var i = 0;
for (var testFrame in testFrames) {
var bytes = [0x90, 0xA9, ...frameValuesBytesEncrypted[i]];
var value = backend.decodeValue(Uint8List.fromList(bytes).buffer);
expect(value, testFrame.value);
i++;
}
});
test('non primitive', () {
var backend = _getBackend(registry: testRegistry);
for (var testFrame in testFrames) {
var bytes = backend.encodeValue(testFrame);
var value = backend.decodeValue(bytes);
expect(value, testFrame.value);
}
});
});
test('.getKeys()', () async {
var db = await _getDbWith({'key1': 1, 'key2': 2, 'key3': 3});
var backend = _getBackend(db: db);
expect(await backend.getKeys(), ['key1', 'key2', 'key3']);
});
test('.getValues()', () async {
var db = await _getDbWith({'key1': 1, 'key2': null, 'key3': 3});
var backend = _getBackend(db: db);
expect(await backend.getValues(), [1, null, 3]);
});
group('.initialize()', () {
test('not lazy', () async {
var db = await _getDbWith({'key1': 1, 'key2': null, 'key3': 3});
var backend = _getBackend(db: db);
var keystore = Keystore(null, ChangeNotifier());
expect(await backend.initialize(null, keystore), 0);
expect(keystore.frames, [
Frame('key1', 1),
Frame('key2', null),
Frame('key3', 3),
]);
});
test('lazy', () async {
var db = await _getDbWith({'key1': 1, 'key2': null, 'key3': 3});
var backend = _getBackend(db: db, lazy: true);
var keystore = Keystore(null, ChangeNotifier());
expect(await backend.initialize(null, keystore), 0);
expect(keystore.frames, [
Frame.lazy('key1'),
Frame.lazy('key2'),
Frame.lazy('key3'),
]);
});
});
test('.readValue()', () async {
var db = await _getDbWith({'key1': 1, 'key2': null, 'key3': 3});
var backend = _getBackend(db: db);
expect(await backend.readValue(Frame('key1', null)), 1);
expect(await backend.readValue(Frame('key2', null)), null);
});
test('.writeFrames()', () async {
var db = await _getDbWith({});
var backend = _getBackend(db: db);
var frames = [Frame('key1', 123), Frame('key2', null)];
await backend.writeFrames(frames);
expect(frames, [Frame('key1', 123), Frame('key2', null)]);
expect(await backend.getKeys(), ['key1', 'key2']);
await backend.writeFrames([Frame.deleted('key1')]);
expect(await backend.getKeys(), ['key2']);
});
test('.compact()', () async {
var db = await _getDbWith({});
var backend = _getBackend(db: db);
expect(
() async => await backend.compact({}),
throwsUnsupportedError,
);
});
test('.clear()', () async {
var db = await _getDbWith({'key1': 1, 'key2': 2, 'key3': 3});
var backend = _getBackend(db: db);
await backend.clear();
expect(await backend.getKeys(), []);
});
test('.close()', () async {
var db = await _getDbWith({'key1': 1, 'key2': 2, 'key3': 3});
var backend = _getBackend(db: db);
await backend.close();
await expectLater(() async => await backend.getKeys(), throwsA(anything));
});
});
}