2017-06-17 01:26:19 +00:00
|
|
|
import 'package:test/test.dart';
|
2017-12-07 07:08:32 +00:00
|
|
|
import 'models/author.dart';
|
2017-06-17 01:26:19 +00:00
|
|
|
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
|
|
|
|
|
|
|
main() {
|
|
|
|
var deathlyHallows = new Book(
|
|
|
|
id: '0',
|
|
|
|
author: 'J.K. Rowling',
|
|
|
|
title: 'Harry Potter and the Deathly Hallows',
|
|
|
|
description: 'The 7th book.',
|
|
|
|
pageCount: 759,
|
|
|
|
updatedAt: new DateTime.now());
|
|
|
|
var serializedDeathlyHallows = deathlyHallows.toJson();
|
|
|
|
print('Deathly Hallows: $serializedDeathlyHallows');
|
|
|
|
|
|
|
|
var jkRowling = new Author(
|
|
|
|
id: '1',
|
|
|
|
name: 'J.K. Rowling',
|
|
|
|
age: 51,
|
|
|
|
books: [deathlyHallows],
|
|
|
|
newestBook: deathlyHallows);
|
2018-02-28 01:51:42 +00:00
|
|
|
Map serializedJkRowling = AuthorSerializer.toMap(jkRowling);
|
|
|
|
Map deathlyHallowsMap = BookSerializer.toMap(deathlyHallows);
|
2017-06-17 01:26:19 +00:00
|
|
|
print('J.K. Rowling: $serializedJkRowling');
|
|
|
|
|
2018-02-28 01:51:42 +00:00
|
|
|
var library = new Library(collection: {deathlyHallowsIsbn: deathlyHallows});
|
|
|
|
var serializedLibrary = LibrarySerializer.toMap(library);
|
2017-06-17 01:26:19 +00:00
|
|
|
print('Library: $serializedLibrary');
|
|
|
|
|
|
|
|
group('serialization', () {
|
|
|
|
test('serialization sets proper fields', () {
|
|
|
|
expect(serializedDeathlyHallows['id'], deathlyHallows.id);
|
|
|
|
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'],
|
2017-06-17 01:26:19 +00:00
|
|
|
deathlyHallows.updatedAt.toIso8601String());
|
|
|
|
});
|
|
|
|
|
|
|
|
test('heeds @Alias', () {
|
|
|
|
expect(serializedDeathlyHallows['page_count'], deathlyHallows.pageCount);
|
|
|
|
expect(serializedDeathlyHallows.keys, isNot(contains('pageCount')));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('heeds @exclude', () {
|
|
|
|
expect(serializedJkRowling.keys, isNot(contains('secret')));
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group('deserialization', () {
|
|
|
|
test('deserialization sets proper fields', () {
|
|
|
|
var book = new Book.fromJson(deathlyHallowsMap);
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
group('nested @serializable', () {
|
|
|
|
var author = new Author.fromJson(serializedJkRowling);
|
|
|
|
|
|
|
|
test('nested @serializable class is deserialized', () {
|
|
|
|
var newestBook = author.newestBook;
|
|
|
|
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', () {
|
|
|
|
var lib = new Library.fromJson(serializedLibrary);
|
|
|
|
expect(lib.collection, allOf(isNotEmpty, hasLength(1)));
|
2018-02-28 01:51:42 +00:00
|
|
|
expect(lib.collection.keys.first, deathlyHallowsIsbn);
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|