Added String & DateTime converter tests
MODIFY
packages/isar_test/test/converter/bool_converter_test.dart
packages/isar_test/test/converter/bool_converter_test.dart
@@ -326,5 +326,34 @@ void main() {
[childObj0],
);
});
+
+ isarTest('Sort by boolEnum', () async {
+ await qEqual(
+ isar.boolModels.where().sortByBoolEnum().tFindAll(),
+ [obj0, obj1, obj2],
+ );
+
+ await qEqual(
+ isar.boolModels.where().sortByBoolEnumDesc().tFindAll(),
+ [obj1, obj2, obj0],
+ );
+
+ await qEqual(
+ isar.childBoolModels.where().sortByBoolEnumDesc().tFindAll(),
+ [childObj0, childObj1, childObj2],
+ );
+ });
+
+ isarTest('Sort by mayBoolEnum', () async {
+ await qEqual(
+ isar.boolModels.where().sortByMaybeBoolEnum().tFindAll(),
+ [obj0, obj2, obj1],
+ );
+
+ await qEqual(
+ isar.boolModels.where().sortByMaybeBoolEnumDesc().tFindAll(),
+ [obj1, obj0, obj2],
+ );
+ });
});
}

ADD
packages/isar_test/test/converter/date_time_converter_test.dart
+import 'package:isar/isar.dart';
+import 'package:test/test.dart';
+
+import '../util/common.dart';
+import '../util/sync_async_helper.dart';
+
+part 'date_time_converter_test.g.dart';
+
+@Collection()
+class DateTimeModel {
+ DateTimeModel({
+ required this.dateTime,
+ required this.nullableDateTime,
+ });
+
+ int id = Isar.autoIncrement;
+
+ @UnixToDateTimeTypeConverter()
+ final DateTime dateTime;
+
+ @UnixToNullableDateTimeTypeConverter()
+ final DateTime? nullableDateTime;
+
+ @override
+ String toString() {
+ return 'DateTimeModel{id: $id, dateTime: $dateTime, nullableDateTime: $nullableDateTime}';
+ }
+
+ @override
+ // ignore: hash_and_equals
+ bool operator ==(Object other) =>
+ identical(this, other) ||
+ other is DateTimeModel &&
+ runtimeType == other.runtimeType &&
+ id == other.id &&
+ dateTime == other.dateTime &&
+ nullableDateTime == other.nullableDateTime;
+}
+
+class UnixToDateTimeTypeConverter extends TypeConverter<int, DateTime> {
+ const UnixToDateTimeTypeConverter();
+
+ @override
+ int fromIsar(DateTime date) => date.millisecondsSinceEpoch ~/ 1000;
+
+ @override
+ DateTime toIsar(int unixTimestamp) {
+ return DateTime.fromMillisecondsSinceEpoch(unixTimestamp * 1000);
+ }
+}
+
+class UnixToNullableDateTimeTypeConverter
+ extends TypeConverter<int?, DateTime?> {
+ const UnixToNullableDateTimeTypeConverter();
+
+ @override
+ int? fromIsar(DateTime? date) {
+ if (date == null) return null;
+ return date.millisecondsSinceEpoch ~/ 1000;
+ }
+
+ @override
+ DateTime? toIsar(int? unixTimestamp) {
+ if (unixTimestamp == null) return null;
+ return DateTime.fromMillisecondsSinceEpoch(unixTimestamp * 1000);
+ }
+}
+
+void main() {
+ group('DateTime converter', () {
+ late Isar isar;
+
+ late DateTimeModel obj0;
+ late DateTimeModel obj1;
+ late DateTimeModel obj2;
+ late DateTimeModel obj3;
+ late DateTimeModel obj4;
+ late DateTimeModel obj5;
+
+ setUp(() async {
+ isar = await openTempIsar([DateTimeModelSchema]);
+
+ obj0 = DateTimeModel(
+ dateTime: DateTime(1980, 1, 12),
+ nullableDateTime: DateTime(1992, 7, 21),
+ );
+ obj1 = DateTimeModel(
+ dateTime: DateTime(1500, 3, 27),
+ nullableDateTime: null,
+ );
+ obj2 = DateTimeModel(
+ dateTime: DateTime(2100, 6, 2),
+ nullableDateTime: null,
+ );
+ obj3 = DateTimeModel(
+ dateTime: DateTime(2010, 12, 13),
+ nullableDateTime: DateTime(1942, 3, 3),
+ );
+ obj4 = DateTimeModel(
+ dateTime: DateTime(-42, 5, 22),
+ nullableDateTime: null,
+ );
+ obj5 = DateTimeModel(
+ dateTime: DateTime(2003, 6, 24),
+ nullableDateTime: DateTime(2200, 3, 10),
+ );
+
+ await isar.tWriteTxn(
+ () => isar.dateTimeModels.tPutAll([obj0, obj1, obj2, obj3, obj4, obj5]),
+ );
+ });
+
+ isarTest('Query by dateTime', () async {
+ await qEqual(
+ isar.dateTimeModels
+ .filter()
+ .dateTimeEqualTo(DateTime(2010, 12, 13))
+ .tFindAll(),
+ [obj3],
+ );
+
+ await qEqual(
+ isar.dateTimeModels
+ .filter()
+ .dateTimeGreaterThan(DateTime(2000))
+ .tFindAll(),
+ [obj2, obj3, obj5],
+ );
+
+ await qEqual(
+ isar.dateTimeModels
+ .filter()
+ .dateTimeBetween(DateTime(1980, 1, 2), DateTime(2005, 1, 2))
+ .tFindAll(),
+ [obj0, obj5],
+ );
+
+ await qEqual(
+ isar.dateTimeModels.filter().dateTimeLessThan(DateTime(0)).tFindAll(),
+ [obj4],
+ );
+ });
+
+ isarTest('Query by nullableDateTime', () async {
+ await qEqual(
+ isar.dateTimeModels.filter().nullableDateTimeIsNull().tFindAll(),
+ [obj1, obj2, obj4],
+ );
+
+ await qEqual(
+ isar.dateTimeModels
+ .filter()
+ .nullableDateTimeGreaterThan(DateTime(2000))
+ .tFindAll(),
+ [obj5],
+ );
+
+ // FIXME: lessThan queries on nullable dates also returns objects where
+ // the field is null
+ await qEqual(
+ isar.dateTimeModels
+ .filter()
+ .nullableDateTimeLessThan(DateTime(1969))
+ .tFindAll(),
+ [obj3],
+ );
+
+ await qEqual(
+ isar.dateTimeModels
+ .filter()
+ .nullableDateTimeBetween(DateTime(1950), DateTime(9999))
+ .tFindAll(),
+ [obj0, obj5],
+ );
+ });
+
+ isarTest('Sort by dateTime', () async {
+ await qEqual(
+ isar.dateTimeModels.where().sortByDateTime().tFindAll(),
+ [obj4, obj1, obj0, obj5, obj3, obj2],
+ );
+
+ await qEqual(
+ isar.dateTimeModels.where().sortByDateTimeDesc().tFindAll(),
+ [obj2, obj3, obj5, obj0, obj1, obj4],
+ );
+ });
+
+ isarTest('Sort by nullableDateTime', () async {
+ await qEqual(
+ isar.dateTimeModels.where().sortByNullableDateTime().tFindAll(),
+ [obj1, obj2, obj4, obj3, obj0, obj5],
+ );
+
+ await qEqual(
+ isar.dateTimeModels.where().sortByNullableDateTimeDesc().tFindAll(),
+ [obj5, obj0, obj3, obj1, obj2, obj4],
+ );
+ });
+ });
+}

MODIFY
packages/isar_test/test/converter/double_converter_test.dart
packages/isar_test/test/converter/double_converter_test.dart
@@ -149,6 +149,22 @@ void main() {
.tFindAll(),
[],
);
+
+ await qEqual(
+ isar.doubleModels
+ .where()
+ .someIntEqualToValueLessThan(0, Values.first)
+ .tFindAll(),
+ [],
+ );
+
+ await qEqual(
+ isar.doubleModels
+ .where()
+ .someIntEqualToValueLessThan(0, Values.second)
+ .tFindAll(),
+ [obj1],
+ );
});
});
}

MODIFY
packages/isar_test/test/converter/int_converter_test.dart
packages/isar_test/test/converter/int_converter_test.dart
@@ -12,6 +12,7 @@ class IntModel {
required this.offsettedInt,
required this.day,
required this.otherDay,
+ required this.nullableDay,
});
int id = Isar.autoIncrement;
@@ -26,9 +27,13 @@ class IntModel {
@DaysTypeConverter()
final Days otherDay;
+ @NullableDaysTypeConverter()
+ @Size32()
+ final Days? nullableDay;
+
@override
String toString() {
- return 'IntModel{id: $id, offsettedInt: $offsettedInt, day: $day}';
+ return 'IntModel{id: $id, offsettedInt: $offsettedInt, day: $day, otherDay: $otherDay, nullableDay: $nullableDay}';
}
@override
@@ -39,7 +44,9 @@ class IntModel {
runtimeType == other.runtimeType &&
id == other.id &&
offsettedInt == other.offsettedInt &&
- day == other.day;
+ day == other.day &&
+ otherDay == other.otherDay &&
+ nullableDay == other.nullableDay;
}
}
@@ -79,6 +86,19 @@ class DaysTypeConverter extends TypeConverter<Days, int> {
int toIsar(Days day) => day.value;
}
+class NullableDaysTypeConverter extends TypeConverter<Days?, int?> {
+ const NullableDaysTypeConverter();
+
+ @override
+ Days? fromIsar(int? value) {
+ if (value == null) return null;
+ return Days.values.firstWhere((element) => element.value == value);
+ }
+
+ @override
+ int? toIsar(Days? day) => day?.value;
+}
+
void main() {
group('Int converter', () {
late Isar isar;
@@ -99,41 +119,49 @@ void main() {
offsettedInt: 0,
day: Days.wednesday,
otherDay: Days.sunday,
+ nullableDay: null,
);
obj1 = IntModel(
offsettedInt: 24,
day: Days.saturday,
otherDay: Days.monday,
+ nullableDay: Days.tuesday,
);
obj2 = IntModel(
offsettedInt: 42,
day: Days.thursday,
otherDay: Days.tuesday,
+ nullableDay: Days.sunday,
);
obj3 = IntModel(
offsettedInt: -1024,
day: Days.saturday,
otherDay: Days.wednesday,
+ nullableDay: Days.friday,
);
obj4 = IntModel(
offsettedInt: 2048,
day: Days.monday,
otherDay: Days.thursday,
+ nullableDay: null,
);
obj5 = IntModel(
offsettedInt: -0,
day: Days.sunday,
otherDay: Days.friday,
+ nullableDay: null,
);
obj6 = IntModel(
offsettedInt: 5673873209,
day: Days.saturday,
otherDay: Days.saturday,
+ nullableDay: Days.wednesday,
);
obj7 = IntModel(
offsettedInt: 40,
day: Days.friday,
otherDay: Days.saturday,
+ nullableDay: null,
);
await isar.tWriteTxn(
@@ -241,5 +269,25 @@ void main() {
[obj1, obj6],
);
});
+
+ isarTest('Query by nullableDay', () async {
+ await qEqual(
+ isar.intModels.filter().nullableDayIsNull().tFindAll(),
+ [obj0, obj4, obj5, obj7],
+ );
+
+ await qEqual(
+ isar.intModels
+ .filter()
+ .nullableDayGreaterThan(Days.wednesday)
+ .tFindAll(),
+ [obj3],
+ );
+
+ await qEqual(
+ isar.intModels.filter().nullableDayEqualTo(Days.saturday).tFindAll(),
+ [],
+ );
+ });
});
}

ADD
packages/isar_test/test/converter/string_converter_test.dart
+import 'package:isar/isar.dart';
+import 'package:test/test.dart';
+
+import '../util/common.dart';
+import '../util/sync_async_helper.dart';
+
+part 'string_converter_test.g.dart';
+
+@Collection()
+class StringModel {
+ StringModel({
+ required this.authProvider,
+ required this.number,
+ required this.nullableNumber,
+ });
+
+ final id = Isar.autoIncrement;
+
+ @AuthProvidersTypeConverter()
+ final AuthProviders authProvider;
+
+ @NumStringifierTypeConverter()
+ @Index()
+ final num number;
+
+ @NullableNumStringifierTypeConverter()
+ final num? nullableNumber;
+
+ @override
+ String toString() {
+ return 'StringModel{id: $id, authProvider: $authProvider, number: $number, nullableNumber: $nullableNumber}';
+ }
+
+ @override
+ bool operator ==(Object other) =>
+ identical(this, other) ||
+ other is StringModel &&
+ runtimeType == other.runtimeType &&
+ id == other.id &&
+ authProvider == other.authProvider &&
+ number == other.number &&
+ nullableNumber == other.nullableNumber;
+
+ @override
+ int get hashCode =>
+ id.hashCode ^
+ authProvider.hashCode ^
+ number.hashCode ^
+ nullableNumber.hashCode;
+}
+
+enum AuthProviders {
+ password('password'),
+ google('google.com'),
+ facebook('facebook.com'),
+ apple('apple.com'),
+ github('github.com'),
+ none(null);
+
+ const AuthProviders(this.value);
+
+ final String? value;
+}
+
+class AuthProvidersTypeConverter extends TypeConverter<AuthProviders, String?> {
+ const AuthProvidersTypeConverter();
+
+ @override
+ AuthProviders fromIsar(String? value) {
+ return AuthProviders.values.firstWhere(
+ (provider) => provider.value == value,
+ );
+ }
+
+ @override
+ String? toIsar(AuthProviders provider) => provider.value;
+}
+
+class NumStringifierTypeConverter extends TypeConverter<num, String> {
+ const NumStringifierTypeConverter();
+
+ @override
+ num fromIsar(String value) => num.parse(value);
+
+ @override
+ String toIsar(num value) => value.toString();
+}
+
+class NullableNumStringifierTypeConverter extends TypeConverter<num?, String?> {
+ const NullableNumStringifierTypeConverter();
+
+ @override
+ num? fromIsar(String? value) {
+ if (value == null) return null;
+ return num.parse(value);
+ }
+
+ @override
+ String? toIsar(num? value) => value?.toString();
+}
+
+void main() {
+ group('String converter', () {
+ late Isar isar;
+
+ late StringModel obj0;
+ late StringModel obj1;
+ late StringModel obj2;
+ late StringModel obj3;
+ late StringModel obj4;
+ late StringModel obj5;
+
+ setUp(() async {
+ isar = await openTempIsar([StringModelSchema]);
+
+ obj0 = StringModel(
+ authProvider: AuthProviders.github,
+ number: 1,
+ nullableNumber: null,
+ );
+ obj1 = StringModel(
+ authProvider: AuthProviders.facebook,
+ number: 42.12,
+ nullableNumber: 123,
+ );
+ obj2 = StringModel(
+ authProvider: AuthProviders.none,
+ number: 0,
+ nullableNumber: null,
+ );
+ obj3 = StringModel(
+ authProvider: AuthProviders.password,
+ number: 1024,
+ nullableNumber: null,
+ );
+ obj4 = StringModel(
+ authProvider: AuthProviders.apple,
+ number: 0.5,
+ nullableNumber: 92,
+ );
+ obj5 = StringModel(
+ authProvider: AuthProviders.github,
+ number: -2048,
+ nullableNumber: 42.42,
+ );
+
+ await isar.tWriteTxn(
+ () => isar.stringModels.tPutAll([obj0, obj1, obj2, obj3, obj4, obj5]),
+ );
+ });
+
+ tearDown(() => isar.close());
+
+ // FIXME: This seems to be an issue with the generator, that generates
+ // methods which don't support nullable string as argument
+ //
+ // test/converter/string_converter_test.g.dart:608:64:
+ // Error: The argument type 'String?' can't be assigned to the parameter
+ // type 'String' because 'String?' is nullable and 'String' isn't.
+ //
+ isarTest('Query by authProvider', () async {
+ await qEqual(
+ isar.stringModels
+ .filter()
+ .authProviderEqualTo(AuthProviders.github)
+ .tFindAll(),
+ [obj0, obj5],
+ );
+ });
+
+ isarTest('Query by number index', () async {
+ await qEqual(
+ isar.stringModels
+ .where()
+ .numberEqualTo(1)
+ .or()
+ .numberEqualTo(-2048)
+ .tFindAll(),
+ [obj0, obj5],
+ );
+ });
+
+ // TODO(jtplouffe): implement more tests once the generator issues are fixed
+ });
+}

Added String & DateTime converter tests