Skip to content

Commit 1c86311

Browse files
committed
test: improve ht_kv_storage_service_test
- Added tests for exception handling - Improved readability
1 parent a3f980f commit 1c86311

File tree

2 files changed

+100
-74
lines changed

2 files changed

+100
-74
lines changed

test/src/ht_kv_storage_service_test.dart

Lines changed: 96 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ const testWriteException = StorageWriteException('testKey', 'testValue');
1010
const testReadException = StorageReadException('testKey');
1111
const testDeleteException = StorageDeleteException('testKey');
1212
const testClearException = StorageClearException();
13-
const testTypeMismatchException =
14-
StorageTypeMismatchException('testKey', String, int);
13+
const testTypeMismatchException = StorageTypeMismatchException(
14+
'testKey',
15+
String,
16+
int,
17+
);
1518
const testKeyNotFoundException = StorageKeyNotFoundException('testKey');
1619

1720
void main() {
@@ -38,8 +41,9 @@ void main() {
3841
value: any(named: 'value'),
3942
),
4043
).thenAnswer((_) async {});
41-
when(() => mockStorageService.readString(key: any(named: 'key')))
42-
.thenAnswer((_) async => null);
44+
when(
45+
() => mockStorageService.readString(key: any(named: 'key')),
46+
).thenAnswer((_) async => null);
4347
when(
4448
() => mockStorageService.writeBool(
4549
key: any(named: 'key'),
@@ -53,40 +57,39 @@ void main() {
5357
),
5458
).thenAnswer((_) async => false); // Return default
5559
when(
56-
() => mockStorageService.readBool(
57-
key: any(named: 'key'),
58-
),
60+
() => mockStorageService.readBool(key: any(named: 'key')),
5961
) // Handle call without defaultValue
60-
.thenAnswer((_) async => false);
62+
.thenAnswer((_) async => false);
6163
when(
6264
() => mockStorageService.writeInt(
6365
key: any(named: 'key'),
6466
value: any(named: 'value'),
6567
),
6668
).thenAnswer((_) async {});
67-
when(() => mockStorageService.readInt(key: any(named: 'key')))
68-
.thenAnswer((_) async => null);
69+
when(
70+
() => mockStorageService.readInt(key: any(named: 'key')),
71+
).thenAnswer((_) async => null);
6972
when(
7073
() => mockStorageService.writeDouble(
7174
key: any(named: 'key'),
7275
value: any(named: 'value'),
7376
),
7477
).thenAnswer((_) async {});
75-
when(() => mockStorageService.readDouble(key: any(named: 'key')))
76-
.thenAnswer((_) async => null);
77-
when(() => mockStorageService.delete(key: any(named: 'key')))
78-
.thenAnswer((_) async {});
78+
when(
79+
() => mockStorageService.readDouble(key: any(named: 'key')),
80+
).thenAnswer((_) async => null);
81+
when(
82+
() => mockStorageService.delete(key: any(named: 'key')),
83+
).thenAnswer((_) async {});
7984
when(() => mockStorageService.clearAll()).thenAnswer((_) async {});
8085
});
8186

8287
// Test calling each method to ensure coverage of the abstract definition
8388
test('writeString can be called', () async {
8489
await mockStorageService.writeString(key: 'testKey', value: 'testValue');
8590
verify(
86-
() => mockStorageService.writeString(
87-
key: 'testKey',
88-
value: 'testValue',
89-
),
91+
() =>
92+
mockStorageService.writeString(key: 'testKey', value: 'testValue'),
9093
).called(1);
9194
});
9295

@@ -97,8 +100,9 @@ void main() {
97100

98101
test('writeBool can be called', () async {
99102
await mockStorageService.writeBool(key: 'testKey', value: true);
100-
verify(() => mockStorageService.writeBool(key: 'testKey', value: true))
101-
.called(1);
103+
verify(
104+
() => mockStorageService.writeBool(key: 'testKey', value: true),
105+
).called(1);
102106
});
103107

104108
test('readBool can be called with default', () async {
@@ -115,8 +119,9 @@ void main() {
115119

116120
test('writeInt can be called', () async {
117121
await mockStorageService.writeInt(key: 'testKey', value: 123);
118-
verify(() => mockStorageService.writeInt(key: 'testKey', value: 123))
119-
.called(1);
122+
verify(
123+
() => mockStorageService.writeInt(key: 'testKey', value: 123),
124+
).called(1);
120125
});
121126

122127
test('readInt can be called', () async {
@@ -126,8 +131,9 @@ void main() {
126131

127132
test('writeDouble can be called', () async {
128133
await mockStorageService.writeDouble(key: 'testKey', value: 1.23);
129-
verify(() => mockStorageService.writeDouble(key: 'testKey', value: 1.23))
130-
.called(1);
134+
verify(
135+
() => mockStorageService.writeDouble(key: 'testKey', value: 1.23),
136+
).called(1);
131137
});
132138

133139
test('readDouble can be called', () async {
@@ -163,25 +169,29 @@ void main() {
163169
});
164170

165171
test('readString throws StorageReadException on failure', () async {
166-
when(() => mockStorageService.readString(key: any(named: 'key')))
167-
.thenThrow(testReadException);
172+
when(
173+
() => mockStorageService.readString(key: any(named: 'key')),
174+
).thenThrow(testReadException);
168175

169176
expect(
170177
() => mockStorageService.readString(key: 'testKey'),
171178
throwsA(isA<StorageReadException>()),
172179
);
173180
});
174181

175-
test('readString throws StorageTypeMismatchException on type mismatch',
176-
() async {
177-
when(() => mockStorageService.readString(key: any(named: 'key')))
178-
.thenThrow(testTypeMismatchException);
182+
test(
183+
'readString throws StorageTypeMismatchException on type mismatch',
184+
() async {
185+
when(
186+
() => mockStorageService.readString(key: any(named: 'key')),
187+
).thenThrow(testTypeMismatchException);
179188

180-
expect(
181-
() => mockStorageService.readString(key: 'testKey'),
182-
throwsA(isA<StorageTypeMismatchException>()),
183-
);
184-
});
189+
expect(
190+
() => mockStorageService.readString(key: 'testKey'),
191+
throwsA(isA<StorageTypeMismatchException>()),
192+
);
193+
},
194+
);
185195

186196
test('writeBool throws StorageWriteException on failure', () async {
187197
when(
@@ -198,25 +208,29 @@ void main() {
198208
});
199209

200210
test('readBool throws StorageReadException on failure', () async {
201-
when(() => mockStorageService.readBool(key: any(named: 'key')))
202-
.thenThrow(testReadException);
211+
when(
212+
() => mockStorageService.readBool(key: any(named: 'key')),
213+
).thenThrow(testReadException);
203214

204215
expect(
205216
() => mockStorageService.readBool(key: 'testKey'),
206217
throwsA(isA<StorageReadException>()),
207218
);
208219
});
209220

210-
test('readBool throws StorageTypeMismatchException on type mismatch',
211-
() async {
212-
when(() => mockStorageService.readBool(key: any(named: 'key')))
213-
.thenThrow(testTypeMismatchException);
221+
test(
222+
'readBool throws StorageTypeMismatchException on type mismatch',
223+
() async {
224+
when(
225+
() => mockStorageService.readBool(key: any(named: 'key')),
226+
).thenThrow(testTypeMismatchException);
214227

215-
expect(
216-
() => mockStorageService.readBool(key: 'testKey'),
217-
throwsA(isA<StorageTypeMismatchException>()),
218-
);
219-
});
228+
expect(
229+
() => mockStorageService.readBool(key: 'testKey'),
230+
throwsA(isA<StorageTypeMismatchException>()),
231+
);
232+
},
233+
);
220234

221235
test('writeInt throws StorageWriteException on failure', () async {
222236
when(
@@ -233,25 +247,29 @@ void main() {
233247
});
234248

235249
test('readInt throws StorageReadException on failure', () async {
236-
when(() => mockStorageService.readInt(key: any(named: 'key')))
237-
.thenThrow(testReadException);
250+
when(
251+
() => mockStorageService.readInt(key: any(named: 'key')),
252+
).thenThrow(testReadException);
238253

239254
expect(
240255
() => mockStorageService.readInt(key: 'testKey'),
241256
throwsA(isA<StorageReadException>()),
242257
);
243258
});
244259

245-
test('readInt throws StorageTypeMismatchException on type mismatch',
246-
() async {
247-
when(() => mockStorageService.readInt(key: any(named: 'key')))
248-
.thenThrow(testTypeMismatchException);
260+
test(
261+
'readInt throws StorageTypeMismatchException on type mismatch',
262+
() async {
263+
when(
264+
() => mockStorageService.readInt(key: any(named: 'key')),
265+
).thenThrow(testTypeMismatchException);
249266

250-
expect(
251-
() => mockStorageService.readInt(key: 'testKey'),
252-
throwsA(isA<StorageTypeMismatchException>()),
253-
);
254-
});
267+
expect(
268+
() => mockStorageService.readInt(key: 'testKey'),
269+
throwsA(isA<StorageTypeMismatchException>()),
270+
);
271+
},
272+
);
255273

256274
test('writeDouble throws StorageWriteException on failure', () async {
257275
when(
@@ -268,29 +286,34 @@ void main() {
268286
});
269287

270288
test('readDouble throws StorageReadException on failure', () async {
271-
when(() => mockStorageService.readDouble(key: any(named: 'key')))
272-
.thenThrow(testReadException);
289+
when(
290+
() => mockStorageService.readDouble(key: any(named: 'key')),
291+
).thenThrow(testReadException);
273292

274293
expect(
275294
() => mockStorageService.readDouble(key: 'testKey'),
276295
throwsA(isA<StorageReadException>()),
277296
);
278297
});
279298

280-
test('readDouble throws StorageTypeMismatchException on type mismatch',
281-
() async {
282-
when(() => mockStorageService.readDouble(key: any(named: 'key')))
283-
.thenThrow(testTypeMismatchException);
299+
test(
300+
'readDouble throws StorageTypeMismatchException on type mismatch',
301+
() async {
302+
when(
303+
() => mockStorageService.readDouble(key: any(named: 'key')),
304+
).thenThrow(testTypeMismatchException);
284305

285-
expect(
286-
() => mockStorageService.readDouble(key: 'testKey'),
287-
throwsA(isA<StorageTypeMismatchException>()),
288-
);
289-
});
306+
expect(
307+
() => mockStorageService.readDouble(key: 'testKey'),
308+
throwsA(isA<StorageTypeMismatchException>()),
309+
);
310+
},
311+
);
290312

291313
test('delete throws StorageDeleteException on failure', () async {
292-
when(() => mockStorageService.delete(key: any(named: 'key')))
293-
.thenThrow(testDeleteException);
314+
when(
315+
() => mockStorageService.delete(key: any(named: 'key')),
316+
).thenThrow(testDeleteException);
294317

295318
expect(
296319
() => mockStorageService.delete(key: 'testKey'),
@@ -300,8 +323,9 @@ void main() {
300323

301324
test('delete might throw StorageKeyNotFoundException', () async {
302325
// Testing the possibility as per documentation comment
303-
when(() => mockStorageService.delete(key: any(named: 'key')))
304-
.thenThrow(testKeyNotFoundException);
326+
when(
327+
() => mockStorageService.delete(key: any(named: 'key')),
328+
).thenThrow(testKeyNotFoundException);
305329

306330
expect(
307331
() => mockStorageService.delete(key: 'testKey'),

test/src/storage_exceptions_test.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,10 @@ void main() {
157157
});
158158

159159
test('can be instantiated with custom message', () {
160-
const exception =
161-
StorageKeyNotFoundException(testKey, message: testMessage);
160+
const exception = StorageKeyNotFoundException(
161+
testKey,
162+
message: testMessage,
163+
);
162164
expect(exception.key, testKey);
163165
expect(exception.message, testMessage);
164166
expect(exception.cause, isNull);

0 commit comments

Comments
 (0)