platform/angel_serialize_generator/test/book_test.dart

143 lines
5.1 KiB
Dart
Raw Normal View History

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,
2018-03-05 06:18:46 +00:00
notModels: [1.0, 3.0],
2017-06-17 01:26:19 +00:00
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')));
});
2018-03-05 06:18:46 +00:00
test('standard list', () {
expect(serializedDeathlyHallows['not_models'], deathlyHallows.notModels);
});
2017-06-17 01:26:19 +00:00
test('heeds @exclude', () {
expect(serializedJkRowling.keys, isNot(contains('secret')));
});
test('heeds canDeserialize', () {
var map = new Map.from(serializedJkRowling)..['obscured'] = 'foo';
var author = AuthorSerializer.fromMap(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);
});
2017-06-17 01:26:19 +00:00
group('deserialization', () {
test('deserialization sets proper fields', () {
2018-02-28 02:10:43 +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', () {
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', () {
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);
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);
});
});
});
}