Ensure BoolType maps null values correctly

Fixes #106
This commit is contained in:
Simon Binder 2019-08-19 16:16:34 +02:00
parent 40dafa1f2e
commit 17aabbe446
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 23 additions and 0 deletions

View File

@ -26,16 +26,24 @@ class BoolType extends SqlType<bool> {
@override
bool mapFromDatabaseResponse(response) {
// ignore: avoid_returning_null
if (response == null) return null;
return response != 0;
}
@override
String mapToSqlConstant(bool content) {
if (content == null) {
return 'NULL';
}
return content ? '1' : '0';
}
@override
mapToSqlVariable(bool content) {
if (content == null) {
return null;
}
return content ? 1 : 0;
}
}

View File

@ -0,0 +1,15 @@
import 'package:moor/moor.dart' hide isNull;
import 'package:test/test.dart';
void main() {
test('types map null values to null', () {
final typeSystem = const SqlTypeSystem.withDefaults();
for (var type in typeSystem.types) {
expect(type.mapToSqlVariable(null), isNull,
reason: '$type should map null to null variables');
expect(type.mapFromDatabaseResponse(null), isNull,
reason: '$type should map null response to null value');
}
});
}