Write unit tests for the custom serializer

This commit is contained in:
Simon Binder 2019-06-09 12:12:56 +02:00
parent 9241bbccc1
commit 71e3f4549a
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
1 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,79 @@
import 'package:moor/moor.dart';
import 'package:moor/src/runtime/data_class.dart';
import 'package:test_api/test_api.dart';
import 'data/tables/todos.dart';
final someDate = DateTime(2019, 06, 08);
final someTodoEntry = TodoEntry(
id: 3,
title: 'a title',
content: null,
targetDate: someDate,
category: 3,
);
final regularSerialized = {
'id': 3,
'title': 'a title',
'content': null,
'target_date': someDate.millisecondsSinceEpoch,
'category': 3,
};
final customSerialized = {
'id': 3,
'title': 'a title',
'content': 'set to null',
'target_date': someDate.toIso8601String(),
'category': 3,
};
class CustomSerializer extends ValueSerializer {
@override
T fromJson<T>(json) {
if (T == DateTime) {
return DateTime.parse(json.toString()) as T;
} else if (json == 'set to null') {
return null;
} else {
return json as T;
}
}
@override
toJson<T>(T value) {
if (T == DateTime) {
return (value as DateTime).toIso8601String();
} else if (value == null) {
return 'set to null';
} else {
return value;
}
}
}
void main() {
group('serialization', () {
test('with defaults', () {
expect(someTodoEntry.toJson(), equals(regularSerialized));
});
test('with custom serializer', () {
expect(someTodoEntry.toJson(serializer: CustomSerializer()),
equals(customSerialized));
});
});
group('deserialization', () {
test('with defaults', () {
expect(TodoEntry.fromJson(regularSerialized), equals(someTodoEntry));
});
test('with custom serializer', () {
expect(
TodoEntry.fromJson(customSerialized, serializer: CustomSerializer()),
equals(someTodoEntry));
});
});
}