Skip to content

Commit

Permalink
test: Update the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinVignal authored and simolus3 committed Jan 22, 2024
1 parent b2054b9 commit 059108f
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions drift_dev/test/analysis/resolver/dart/type_converter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ class Users extends Table {
TextColumn get foo => text().map(withoutJson())();
TextColumn get bar => text().map(withJson())();
}
''',
'a|lib/json_nullability.dart': '''
import 'package:drift/drift.dart';
JsonTypeConverter<Dart, Sql> tc<Dart, Sql>() => throw 'stub';
class Users extends Table {
TextColumn get wrongSqlType => text().map(tc<int, int>())();
TextColumn get illegalNull => text().map(tc<String, String?>())();
TextColumn get illegalNonNull => text().map(tc<String?, String>()).nullable()();
TextColumn get implicitlyNullAware => text().map(tc<String, String>()).nullable()();
}
''',
'a|lib/nullability.dart': '''
import 'package:drift/drift.dart';
Expand Down Expand Up @@ -71,6 +83,30 @@ CREATE TABLE users (
return testWith('package:a/json.dart');
});

test('warns about type issues around json converters', () async {
final result = await state.driver
.fullyAnalyze(Uri.parse('package:a/json_nullability.dart'));
final table = result.analyzedElements.whereType<DriftTable>().single;

expect(
result.allErrors,
[
isDriftError(contains('must accept String')).withSpan('tc<int, int>()'),
isDriftError(contains('has a type converter with a nullable SQL type'))
.withSpan('tc<String, String?>()'),
isDriftError(allOf([
contains('This column is nullable'),
contains(
'Try wrapping the converter in `JsonTypeConverter2.asNullable`',
)
])).withSpan('tc<String?, String>()'),
],
);

final implicitlyNullAware = table.columns[3];
expect(implicitlyNullAware.typeConverter?.canBeSkippedForNulls, isTrue);
});

test('warns about type issues around converters', () async {
final result = await state.driver
.fullyAnalyze(Uri.parse('package:a/nullability.dart'));
Expand All @@ -82,8 +118,12 @@ CREATE TABLE users (
isDriftError(contains('must accept String')).withSpan('tc<int, int>()'),
isDriftError(contains('has a type converter with a nullable SQL type'))
.withSpan('tc<String, String?>()'),
isDriftError(contains('This column is nullable'))
.withSpan('tc<String?, String>()'),
isDriftError(allOf([
contains('This column is nullable'),
contains(
'Try wrapping the converter in `NullAwareTypeConverter.wrap`',
)
])).withSpan('tc<String?, String>()'),
],
);

Expand Down

0 comments on commit 059108f

Please sign in to comment.