Implemented int & double converter tests
ADD
packages/isar_test/test/converter/double_converter_test.dart
+import 'package:isar/isar.dart';
+import 'package:test/test.dart';
+
+import '../util/common.dart';
+import '../util/sync_async_helper.dart';
+
+part 'double_converter_test.g.dart';
+
+@Collection()
+class DoubleModel {
+ DoubleModel(this.value, this.someInt);
+
+ int id = Isar.autoIncrement;
+
+ @ValuesTypeConverter()
+ final Values value;
+
+ @Index(composite: [CompositeIndex('value')])
+ final int someInt;
+
+ @override
+ String toString() {
+ return 'DoubleModel{id: $id, value: $value, someInt: $someInt}';
+ }
+
+ @override
+ // ignore: hash_and_equals
+ bool operator ==(Object other) =>
+ identical(this, other) ||
+ other is DoubleModel &&
+ runtimeType == other.runtimeType &&
+ id == other.id &&
+ value == other.value &&
+ someInt == other.someInt;
+}
+
+enum Values {
+ first(0),
+ second(5123.582134),
+ third(584114559420.110239),
+ fourth(-5812903859.4583),
+ fifth(349.1),
+ sixth(-3659.1);
+
+ const Values(this.value);
+
+ final double value;
+}
+
+class ValuesTypeConverter extends TypeConverter<Values, double> {
+ const ValuesTypeConverter();
+
+ @override
+ Values fromIsar(double value) {
+ return Values.values.firstWhere((element) => element.value == value);
+ }
+
+ @override
+ double toIsar(Values value) => value.value;
+}
+
+void main() {
+ group('Double converter', () {
+ late Isar isar;
+
+ late DoubleModel obj0;
+ late DoubleModel obj1;
+ late DoubleModel obj2;
+ late DoubleModel obj3;
+ late DoubleModel obj4;
+
+ setUp(() async {
+ isar = await openTempIsar([DoubleModelSchema]);
+
+ obj0 = DoubleModel(Values.fifth, 42);
+ obj1 = DoubleModel(Values.first, 0);
+ obj2 = DoubleModel(Values.sixth, -20);
+ obj3 = DoubleModel(Values.first, 12);
+ obj4 = DoubleModel(Values.third, 3);
+
+ await isar.tWriteTxn(
+ () => isar.doubleModels.tPutAll([obj0, obj1, obj2, obj3, obj4]),
+ );
+ });
+
+ tearDown(() => isar.close());
+
+ isarTest('Query by value', () async {
+ await qEqual(
+ isar.doubleModels.filter().valueGreaterThan(Values.first).tFindAll(),
+ [obj0, obj4],
+ );
+
+ await qEqual(
+ isar.doubleModels
+ .filter()
+ .valueBetween(Values.fourth, Values.second)
+ .tFindAll(),
+ [obj0, obj1, obj2, obj3],
+ );
+
+ await qEqual(
+ isar.doubleModels.filter().valueLessThan(Values.fifth).tFindAll(),
+ [obj1, obj2, obj3],
+ );
+
+ await qEqual(
+ isar.doubleModels
+ .filter()
+ .valueBetween(Values.fourth, Values.fifth)
+ .tFindAll(),
+ [obj1, obj2, obj3],
+ );
+
+ await qEqual(
+ isar.doubleModels.filter().valueGreaterThan(Values.third).tFindAll(),
+ [],
+ );
+ });
+
+ isarTest('Query by someIntValue', () async {
+ await qEqual(
+ isar.doubleModels.where().someIntEqualToAnyValue(42).tFindAll(),
+ [obj0],
+ );
+
+ await qEqual(
+ isar.doubleModels
+ .where()
+ .someIntEqualToValueGreaterThan(-20, Values.fourth)
+ .tFindAll(),
+ [obj2],
+ );
+
+ // FIXME: TypeConverter is not called on `Values.first` and `Values.fifth`
+ // in the generated code.
+ //
+ // Current:
+ // lower: [someInt, lowerValue],
+ // upper: [someInt, upperValue],
+ //
+ // Should be:
+ // lower: [someInt, _doubleModelValuesTypeConverter.toIsar(lowerValue)],
+ // upper: [someInt, _doubleModelValuesTypeConverter.toIsar(upperValue)],
+ await qEqual(
+ isar.doubleModels
+ .where()
+ .someIntEqualToValueBetween(12, Values.first, Values.fifth)
+ .tFindAll(),
+ [],
+ );
+ });
+ });
+}

ADD
packages/isar_test/test/converter/int_converter_test.dart
+import 'package:isar/isar.dart';
+import 'package:test/test.dart';
+
+import '../util/common.dart';
+import '../util/sync_async_helper.dart';
+
+part 'int_converter_test.g.dart';
+
+@Collection()
+class IntModel {
+ IntModel({
+ required this.offsettedInt,
+ required this.day,
+ required this.otherDay,
+ });
+
+ int id = Isar.autoIncrement;
+
+ @OffsettedIntTypeConverter()
+ final int offsettedInt;
+
+ @Index(composite: [CompositeIndex('otherDay')])
+ @DaysTypeConverter()
+ final Days day;
+
+ @DaysTypeConverter()
+ final Days otherDay;
+
+ @override
+ String toString() {
+ return 'IntModel{id: $id, offsettedInt: $offsettedInt, day: $day}';
+ }
+
+ @override
+ // ignore: hash_and_equals
+ bool operator ==(Object other) {
+ return identical(this, other) ||
+ other is IntModel &&
+ runtimeType == other.runtimeType &&
+ id == other.id &&
+ offsettedInt == other.offsettedInt &&
+ day == other.day;
+ }
+}
+
+class OffsettedIntTypeConverter extends TypeConverter<int, int> {
+ const OffsettedIntTypeConverter();
+
+ @override
+ int fromIsar(int value) => value - 42;
+
+ @override
+ int toIsar(int value) => value + 42;
+}
+
+enum Days {
+ sunday(0),
+ monday(1),
+ tuesday(2),
+ wednesday(3),
+ thursday(4),
+ friday(5),
+ saturday(6);
+
+ const Days(this.value);
+
+ final int value;
+}
+
+class DaysTypeConverter extends TypeConverter<Days, int> {
+ const DaysTypeConverter();
+
+ @override
+ Days fromIsar(int value) {
+ return Days.values.firstWhere((element) => element.value == value);
+ }
+
+ @override
+ int toIsar(Days day) => day.value;
+}
+
+void main() {
+ group('Int converter', () {
+ late Isar isar;
+
+ late IntModel obj0;
+ late IntModel obj1;
+ late IntModel obj2;
+ late IntModel obj3;
+ late IntModel obj4;
+ late IntModel obj5;
+ late IntModel obj6;
+ late IntModel obj7;
+
+ setUp(() async {
+ isar = await openTempIsar([IntModelSchema]);
+
+ obj0 = IntModel(
+ offsettedInt: 0,
+ day: Days.wednesday,
+ otherDay: Days.sunday,
+ );
+ obj1 = IntModel(
+ offsettedInt: 24,
+ day: Days.saturday,
+ otherDay: Days.monday,
+ );
+ obj2 = IntModel(
+ offsettedInt: 42,
+ day: Days.thursday,
+ otherDay: Days.tuesday,
+ );
+ obj3 = IntModel(
+ offsettedInt: -1024,
+ day: Days.saturday,
+ otherDay: Days.wednesday,
+ );
+ obj4 = IntModel(
+ offsettedInt: 2048,
+ day: Days.monday,
+ otherDay: Days.thursday,
+ );
+ obj5 = IntModel(
+ offsettedInt: -0,
+ day: Days.sunday,
+ otherDay: Days.friday,
+ );
+ obj6 = IntModel(
+ offsettedInt: 5673873209,
+ day: Days.saturday,
+ otherDay: Days.saturday,
+ );
+ obj7 = IntModel(
+ offsettedInt: 40,
+ day: Days.friday,
+ otherDay: Days.saturday,
+ );
+
+ await isar.tWriteTxn(
+ () => isar.intModels.tPutAll([
+ obj0,
+ obj1,
+ obj2,
+ obj3,
+ obj4,
+ obj5,
+ obj6,
+ obj7,
+ ]),
+ );
+ });
+
+ tearDown(() => isar.close());
+
+ isarTest('Query by offsettedInt', () async {
+ await qEqual(
+ isar.intModels
+ .filter()
+ .offsettedIntBetween(
+ 0,
+ 42,
+ includeLower: false,
+ includeUpper: false,
+ )
+ .tFindAll(),
+ [obj1, obj7],
+ );
+
+ await qEqual(
+ isar.intModels.filter().offsettedIntLessThan(1).tFindAll(),
+ [obj0, obj3, obj5],
+ );
+
+ await qEqual(
+ isar.intModels.filter().offsettedIntEqualTo(42).tFindAll(),
+ [obj2],
+ );
+
+ await qEqual(
+ isar.intModels.filter().offsettedIntGreaterThan(7).tFindAll(),
+ [obj1, obj2, obj4, obj6, obj7],
+ );
+ });
+
+ isarTest('Query by day', () async {
+ await qEqual(
+ isar.intModels.filter().dayEqualTo(Days.saturday).tFindAll(),
+ [obj1, obj3, obj6],
+ );
+
+ await qEqual(
+ isar.intModels.filter().dayBetween(Days.monday, Days.friday).tFindAll(),
+ [obj0, obj2, obj4, obj7],
+ );
+
+ await qEqual(
+ isar.intModels.filter().dayLessThan(Days.tuesday).tFindAll(),
+ [obj4, obj5],
+ );
+
+ await qEqual(
+ isar.intModels.filter().dayGreaterThan(Days.wednesday).tFindAll(),
+ [obj1, obj2, obj3, obj6, obj7],
+ );
+
+ await qEqual(
+ isar.intModels
+ .filter()
+ .dayGreaterThan(Days.monday)
+ .and()
+ .dayLessThan(Days.friday)
+ .and()
+ .dayGreaterThan(Days.wednesday)
+ .tFindAll(),
+ [obj2],
+ );
+ });
+
+ isarTest('Query by dayOtherDay index', () async {
+ await qEqual(
+ isar.intModels
+ .where()
+ .dayOtherDayEqualTo(Days.saturday, Days.saturday)
+ .tFindAll(),
+ [obj6],
+ );
+
+ await qEqual(
+ isar.intModels
+ .where()
+ .dayGreaterThanAnyOtherDay(Days.wednesday)
+ .tFindAll(),
+ [obj2, obj7, obj1, obj3, obj6],
+ );
+
+ await qEqual(
+ isar.intModels
+ .where()
+ .dayEqualToOtherDayNotEqualTo(Days.saturday, Days.wednesday)
+ .tFindAll(),
+ [obj1, obj6],
+ );
+ });
+ });
+}

Implemented int & double converter tests