platform/packages/serialize/serialize_generator/test/book_test.dart

162 lines
5.6 KiB
Dart
Raw Normal View History

2017-06-17 01:26:19 +00:00
import 'package:test/test.dart';
import 'models/book.dart';
2018-02-28 01:51:42 +00:00
const String deathlyHallowsIsbn = '0-545-01022-5';
2017-06-17 01:26:19 +00:00
2021-05-15 14:37:52 +00:00
void main() {
2019-04-30 15:44:01 +00:00
var deathlyHallows = Book(
2017-06-17 01:26:19 +00:00
id: '0',
author: 'J.K. Rowling',
title: 'Harry Potter and the Deathly Hallows',
description: 'The 7th book.',
pageCount: 759,
2018-03-05 06:18:46 +00:00
notModels: [1.0, 3.0],
2019-04-30 15:44:01 +00:00
updatedAt: DateTime.now());
2017-06-17 01:26:19 +00:00
var serializedDeathlyHallows = deathlyHallows.toJson();
2019-04-08 15:00:04 +00:00
print('Deathly Hallows: $deathlyHallows');
2017-06-17 01:26:19 +00:00
2019-04-30 15:44:01 +00:00
var jkRowling = Author(
2017-06-17 01:26:19 +00:00
id: '1',
name: 'J.K. Rowling',
age: 51,
books: [deathlyHallows],
newestBook: deathlyHallows);
2021-05-15 14:37:52 +00:00
var serializedJkRowling = authorSerializer.encode(jkRowling);
var deathlyHallowsMap = bookSerializer.encode(deathlyHallows);
2019-04-08 15:00:04 +00:00
print('J.K. Rowling: $jkRowling');
2017-06-17 01:26:19 +00:00
2019-04-30 15:44:01 +00:00
var library = Library(collection: {deathlyHallowsIsbn: deathlyHallows});
2018-02-28 01:51:42 +00:00
var serializedLibrary = LibrarySerializer.toMap(library);
2019-04-08 15:00:04 +00:00
print('Library: $library');
2017-06-17 01:26:19 +00:00
group('serialization', () {
test('serialization sets proper fields', () {
2022-02-27 04:16:31 +00:00
expect(serializedDeathlyHallows['id'], deathlyHallows.id);
2017-06-17 01:26:19 +00:00
expect(serializedDeathlyHallows['author'], deathlyHallows.author);
expect(
serializedDeathlyHallows['description'], deathlyHallows.description);
expect(serializedDeathlyHallows['page_count'], deathlyHallows.pageCount);
2017-06-20 22:13:04 +00:00
expect(serializedDeathlyHallows['created_at'], isNull);
expect(serializedDeathlyHallows['updated_at'],
2022-02-27 04:16:31 +00:00
deathlyHallows.updatedAt?.toIso8601String());
2017-06-17 01:26:19 +00:00
});
2019-07-04 18:03:20 +00:00
test('can be mutated', () {
var b = deathlyHallows.copyWith();
b.author = 'Hey';
expect(b.author, 'Hey');
2022-02-27 04:16:31 +00:00
expect(b.toJson()[BookFields.author], 'Hey');
2019-07-04 18:03:20 +00:00
});
2017-06-17 01:26:19 +00:00
test('heeds @Alias', () {
2022-02-27 04:16:31 +00:00
expect(serializedDeathlyHallows['page_count'], deathlyHallows.pageCount);
2017-06-17 01:26:19 +00:00
expect(serializedDeathlyHallows.keys, isNot(contains('pageCount')));
});
2018-03-05 06:18:46 +00:00
test('standard list', () {
2022-02-27 04:16:31 +00:00
expect(serializedDeathlyHallows['not_models'], deathlyHallows.notModels);
2018-03-05 06:18:46 +00:00
});
2017-06-17 01:26:19 +00:00
test('heeds @exclude', () {
expect(serializedJkRowling.keys, isNot(contains('secret')));
});
test('heeds canDeserialize', () {
2019-04-30 15:44:01 +00:00
var map = Map.from(serializedJkRowling)..['obscured'] = 'foo';
2021-05-15 14:37:52 +00:00
var author = authorSerializer.decode(map);
expect(author.obscured, 'foo');
});
test('heeds canSerialize', () {
expect(serializedJkRowling.keys, isNot(contains('obscured')));
});
2017-06-17 01:26:19 +00:00
test('nested @serializable class is serialized', () {
expect(serializedJkRowling['newest_book'], deathlyHallowsMap);
});
test('list of nested @serializable class is serialized', () {
expect(serializedJkRowling['books'], [deathlyHallowsMap]);
});
test('map with @serializable class as second key is serialized', () {
expect(serializedLibrary['collection'],
2018-02-28 01:51:42 +00:00
{deathlyHallowsIsbn: deathlyHallowsMap});
2017-06-17 01:26:19 +00:00
});
});
2018-03-09 12:43:17 +00:00
test('fields', () {
expect(BookFields.author, 'author');
expect(BookFields.notModels, 'not_models');
expect(BookFields.camelCaseString, 'camelCase');
});
2018-05-13 18:02:47 +00:00
test('equals', () {
expect(jkRowling.copyWith(), jkRowling);
expect(deathlyHallows.copyWith(), deathlyHallows);
expect(library.copyWith(), library);
});
2018-06-27 05:51:21 +00:00
test('custom method', () {
expect(jkRowling.customMethod, 'hey!');
});
2018-05-15 19:50:12 +00:00
test('required fields fromMap', () {
expect(() => AuthorSerializer.fromMap({}), throwsFormatException);
});
test('required fields toMap', () {
2019-04-30 15:44:01 +00:00
var author = Author(name: null, age: 24);
2018-05-15 19:50:12 +00:00
expect(() => author.toJson(), throwsFormatException);
});
2017-06-17 01:26:19 +00:00
group('deserialization', () {
test('deserialization sets proper fields', () {
2022-02-27 04:16:31 +00:00
var book = BookSerializer.fromMap(deathlyHallowsMap);
2017-06-17 01:26:19 +00:00
expect(book.id, deathlyHallows.id);
expect(book.author, deathlyHallows.author);
expect(book.description, deathlyHallows.description);
expect(book.pageCount, deathlyHallows.pageCount);
2018-03-05 06:18:46 +00:00
expect(book.notModels, deathlyHallows.notModels);
2017-06-17 01:26:19 +00:00
expect(book.createdAt, isNull);
expect(book.updatedAt, deathlyHallows.updatedAt);
});
group('nested @serializable', () {
2018-02-28 02:10:43 +00:00
var author = AuthorSerializer.fromMap(serializedJkRowling);
2017-06-17 01:26:19 +00:00
test('nested @serializable class is deserialized', () {
2021-05-02 06:02:08 +00:00
var newestBook = author.newestBook!;
2017-06-17 01:26:19 +00:00
expect(newestBook, isNotNull);
expect(newestBook.id, deathlyHallows.id);
expect(newestBook.pageCount, deathlyHallows.pageCount);
expect(newestBook.updatedAt, deathlyHallows.updatedAt);
});
test('list of nested @serializable class is deserialized', () {
expect(author.books, allOf(isList, isNotEmpty, hasLength(1)));
var book = author.books.first;
expect(book.id, deathlyHallows.id);
expect(book.author, deathlyHallows.author);
expect(book.description, deathlyHallows.description);
expect(book.pageCount, deathlyHallows.pageCount);
expect(book.createdAt, isNull);
expect(book.updatedAt, deathlyHallows.updatedAt);
});
test('map with @serializable class as second key is deserialized', () {
2018-02-28 02:10:43 +00:00
var lib = LibrarySerializer.fromMap(serializedLibrary);
2017-06-17 01:26:19 +00:00
expect(lib.collection, allOf(isNotEmpty, hasLength(1)));
2018-02-28 01:51:42 +00:00
expect(lib.collection.keys.first, deathlyHallowsIsbn);
2021-05-02 06:02:08 +00:00
var book = lib.collection[deathlyHallowsIsbn]!;
2017-06-17 01:26:19 +00:00
expect(book.id, deathlyHallows.id);
expect(book.author, deathlyHallows.author);
expect(book.description, deathlyHallows.description);
expect(book.pageCount, deathlyHallows.pageCount);
expect(book.createdAt, isNull);
expect(book.updatedAt, deathlyHallows.updatedAt);
});
});
});
}