Fix NPE when parsing a real type

Fixes #32
This commit is contained in:
Simon Binder 2019-06-11 14:49:45 +02:00
parent 87e39c7ad4
commit acbcc6bb58
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 36 additions and 2 deletions

View File

@ -116,10 +116,15 @@ class RealType extends SqlType<double> {
const RealType();
@override
double mapFromDatabaseResponse(response) => (response as num).toDouble();
double mapFromDatabaseResponse(response) => (response as num)?.toDouble();
@override
String mapToSqlConstant(num content) => content.toString();
String mapToSqlConstant(num content) {
if (content == null) {
return 'NULL';
}
return content.toString();
}
@override
mapToSqlVariable(num content) => content;

View File

@ -0,0 +1,29 @@
import 'package:moor/moor.dart' as moor;
import 'package:test_api/test_api.dart';
void main() {
final type = const moor.RealType();
group('RealType', () {
test('can be read from floating point values returned by sql', () {
expect(type.mapFromDatabaseResponse(3.1234), 3.1234);
});
test('can read null value from sql', () {
expect(type.mapFromDatabaseResponse(null), isNull);
});
test('can be mapped to sql constants', () {
expect(type.mapToSqlConstant(1.123), '1.123');
});
test('can be mapped to variables', () {
expect(type.mapToSqlVariable(1.123), 1.123);
});
test('map null to null', () {
expect(type.mapToSqlConstant(null), 'NULL');
expect(type.mapToSqlVariable(null), null);
});
});
}