2020-11-18 08:07:05 -08:00
|
|
|
//@dart=2.9
|
2019-12-27 09:17:45 -08:00
|
|
|
import 'package:moor/moor.dart';
|
2019-11-12 12:16:48 -08:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
import 'data/tables/todos.dart';
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
test('data classes can be serialized', () {
|
|
|
|
final entry = TodoEntry(
|
|
|
|
id: 13,
|
|
|
|
title: 'Title',
|
|
|
|
content: 'Content',
|
|
|
|
targetDate: DateTime.now(),
|
|
|
|
);
|
|
|
|
|
|
|
|
final serialized = entry.toJsonString();
|
|
|
|
final deserialized = TodoEntry.fromJsonString(serialized);
|
|
|
|
|
|
|
|
expect(deserialized, equals(deserialized));
|
|
|
|
});
|
2019-12-27 09:17:45 -08:00
|
|
|
|
2020-01-31 01:28:01 -08:00
|
|
|
test('can deserialize ints as doubles', () {
|
|
|
|
final entry = TableWithoutPKData.fromJson({
|
|
|
|
'notReallyAnId': 3,
|
|
|
|
'someFloat': 4,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(entry,
|
|
|
|
TableWithoutPKData(notReallyAnId: 3, someFloat: 4, custom: null));
|
|
|
|
});
|
|
|
|
|
2019-12-27 09:17:45 -08:00
|
|
|
test('default serializer can be overridden globally', () {
|
|
|
|
final old = moorRuntimeOptions.defaultSerializer;
|
|
|
|
moorRuntimeOptions.defaultSerializer = _MySerializer();
|
|
|
|
|
|
|
|
final entry = TodoEntry(
|
|
|
|
id: 13,
|
|
|
|
title: 'Title',
|
|
|
|
content: 'Content',
|
|
|
|
category: 3,
|
|
|
|
targetDate: DateTime.now(),
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
entry.toJson(),
|
|
|
|
{
|
|
|
|
'id': 'foo',
|
|
|
|
'title': 'foo',
|
|
|
|
'content': 'foo',
|
|
|
|
'category': 'foo',
|
|
|
|
'target_date': 'foo',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
moorRuntimeOptions.defaultSerializer = old;
|
|
|
|
});
|
2020-03-27 03:52:00 -07:00
|
|
|
|
|
|
|
test('can serialize and deserialize blob columns', () {
|
|
|
|
final user = User(
|
|
|
|
id: 3,
|
|
|
|
name: 'Username',
|
|
|
|
isAwesome: true,
|
|
|
|
profilePicture: Uint8List.fromList([1, 2, 3, 4]),
|
|
|
|
creationTime: DateTime.now(),
|
|
|
|
);
|
|
|
|
|
|
|
|
final recovered = User.fromJsonString(user.toJsonString());
|
|
|
|
|
|
|
|
// Note: Some precision is lost when serializing DateTimes, so we're using
|
|
|
|
// custom expects instead of expect(recovered, user)
|
|
|
|
expect(recovered.id, user.id);
|
|
|
|
expect(recovered.name, user.name);
|
|
|
|
expect(recovered.isAwesome, user.isAwesome);
|
|
|
|
expect(recovered.profilePicture, user.profilePicture);
|
|
|
|
});
|
2020-05-06 09:34:49 -07:00
|
|
|
|
2020-05-08 11:53:58 -07:00
|
|
|
test('generated data classes can be converted to companions', () {
|
2020-05-12 12:47:11 -07:00
|
|
|
final entry = Category(
|
|
|
|
id: 3,
|
|
|
|
description: 'description',
|
|
|
|
priority: CategoryPriority.low,
|
|
|
|
);
|
2020-05-08 11:53:58 -07:00
|
|
|
final companion = entry.toCompanion(false);
|
|
|
|
|
|
|
|
expect(companion.runtimeType, CategoriesCompanion);
|
|
|
|
expect(
|
|
|
|
companion,
|
|
|
|
equals(CategoriesCompanion.insert(
|
|
|
|
description: 'description',
|
|
|
|
id: const Value(3),
|
2020-05-12 12:47:11 -07:00
|
|
|
priority: const Value(CategoryPriority.low),
|
2020-05-08 11:53:58 -07:00
|
|
|
)),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('data classes can be converted to companions with null to absent', () {
|
2020-06-25 11:03:05 -07:00
|
|
|
final entry = PureDefault(txt: null);
|
2020-05-08 11:53:58 -07:00
|
|
|
|
|
|
|
expect(entry.toCompanion(false),
|
2020-06-25 11:03:05 -07:00
|
|
|
const PureDefaultsCompanion(txt: Value(null)));
|
2020-05-08 11:53:58 -07:00
|
|
|
expect(entry.toCompanion(true), const PureDefaultsCompanion());
|
|
|
|
});
|
|
|
|
|
2020-05-06 09:34:49 -07:00
|
|
|
test('companions support hash and equals', () {
|
|
|
|
const first = CategoriesCompanion(description: Value('foo'));
|
|
|
|
final equalToFirst = CategoriesCompanion.insert(description: 'foo');
|
|
|
|
const different = CategoriesCompanion(description: Value('bar'));
|
|
|
|
|
|
|
|
expect(first.hashCode, equalToFirst.hashCode);
|
|
|
|
expect(first, equals(equalToFirst));
|
|
|
|
|
2020-06-08 09:17:04 -07:00
|
|
|
expect(first.hashCode, isNot(equals(different.hashCode)));
|
2020-05-06 09:34:49 -07:00
|
|
|
expect(first, isNot(equals(different)));
|
2020-06-08 09:17:04 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
group('value: hash and ==:', () {
|
|
|
|
test('equal when values are same', () {
|
|
|
|
const first = Value(0);
|
|
|
|
const equalToFirst = Value(0);
|
|
|
|
const different = Value(1);
|
|
|
|
|
|
|
|
expect(first.hashCode, equalToFirst.hashCode);
|
|
|
|
expect(first, equals(equalToFirst));
|
|
|
|
|
|
|
|
expect(first.hashCode, isNot(equals(different.hashCode)));
|
|
|
|
expect(first, isNot(equals(different)));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('equal when value is absent and generic is different', () {
|
|
|
|
const first = Value<int>.absent();
|
|
|
|
const equalToFirst = Value<String>.absent();
|
|
|
|
|
|
|
|
expect(first.hashCode, equalToFirst.hashCode);
|
|
|
|
expect(first, equals(equalToFirst));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('equal when value is null and generic is different', () {
|
|
|
|
const first = Value<int>(null);
|
|
|
|
const equalToFirst = Value<String>(null);
|
|
|
|
|
|
|
|
expect(first.hashCode, equals(equalToFirst.hashCode));
|
|
|
|
expect(first, equals(equalToFirst));
|
|
|
|
});
|
2020-06-08 09:36:06 -07:00
|
|
|
|
|
|
|
test("don't equal when one value is absent and the other one is null", () {
|
|
|
|
const first = Value.absent();
|
|
|
|
const different = Value(null);
|
|
|
|
|
|
|
|
expect(first.hashCode, isNot(equals(different.hashCode)));
|
|
|
|
expect(first, isNot(equals(different)));
|
|
|
|
});
|
2020-05-06 09:34:49 -07:00
|
|
|
});
|
2019-12-27 09:17:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
class _MySerializer extends ValueSerializer {
|
|
|
|
@override
|
|
|
|
T fromJson<T>(dynamic json) {
|
|
|
|
throw StateError('Should not be called');
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
dynamic toJson<T>(T value) {
|
|
|
|
return 'foo';
|
|
|
|
}
|
2019-11-12 12:16:48 -08:00
|
|
|
}
|