Update test cases

This commit is contained in:
thomashii@dukefirehawk.com 2021-05-05 13:01:25 +08:00
parent 005e819fa9
commit 50f8ba23da
4 changed files with 58 additions and 38 deletions

View file

@ -37,7 +37,7 @@
* Migrated angel_orm_generator to 4.0.0 (0/0 tests passed)
* Migrated angel_migration_runner to 3.0.0 (0/0 tests passed)
* Migrated angel_orm_test to 3.0.0 (0/0 tests passed)
* Migrated angel_orm_postgres to 3.0.0 (27/54 tests passed)
* Migrated angel_orm_postgres to 3.0.0 (30/54 tests passed)
* Update orm-sdk-2.12.x boilerplate (in progress) <= Milestone 2

View file

@ -19,9 +19,12 @@ hasMapTests(FutureOr<QueryExecutor> Function() createExecutor,
query.values
..value = {'foo': 'bar'}
..list = ['1', 2, 3.0];
var model = await (query.insert(executor) as FutureOr<HasMap>);
print(model.toJson());
expect(model, HasMap(value: {'foo': 'bar'}, list: ['1', 2, 3.0]));
var modelOpt = await (query.insert(executor));
expect(modelOpt.isPresent, true);
modelOpt.ifPresent((model) {
print(model.toJson());
expect(model, HasMap(value: {'foo': 'bar'}, list: ['1', 2, 3.0]));
});
});
test('update', () async {
@ -29,11 +32,13 @@ hasMapTests(FutureOr<QueryExecutor> Function() createExecutor,
query.values
..value = {'foo': 'bar'}
..list = ['1', 2, 3.0];
var model = await (query.insert(executor) as FutureOr<HasMap>);
print(model.toJson());
query = HasMapQuery()..values.copyFrom(model);
expect(await query.updateOne(executor), model);
var modelOpt = await (query.insert(executor));
expect(modelOpt.isPresent, true);
modelOpt.ifPresent((model) async {
print(model.toJson());
query = HasMapQuery()..values.copyFrom(model);
expect(await query.updateOne(executor), model);
});
});
group('query', () {
@ -73,7 +78,7 @@ hasMapTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('property equals', () async {
var query = HasMapQuery()..where!.value['foo'].asString!.equals('bar');
var query = HasMapQuery()..where!.value['foo'].asString?.equals('bar');
expect(await query.get(executor), [initialValue]);
query = HasMapQuery()..where!.value['foo'].asString!.equals('baz');

View file

@ -20,11 +20,14 @@ hasOneTests(FutureOr<QueryExecutor> Function() createExecutor,
test('sets to null if no child', () async {
print(LegQuery().compile(Set()));
var query = LegQuery()..where!.id.equals(int.parse(originalLeg!.id!));
var leg = await (query.getOne(executor) as FutureOr<Leg>);
print(leg.toJson());
expect(leg.name, originalLeg!.name);
expect(leg.id, originalLeg!.id);
expect(leg.foot, isNull);
var legOpt = await (query.getOne(executor));
expect(legOpt.isPresent, true);
legOpt.ifPresent((leg) {
print(leg.toJson());
expect(leg.name, originalLeg?.name);
expect(leg.id, originalLeg?.id);
expect(leg.foot, isNull);
});
});
test('can fetch one foot', () async {
@ -32,14 +35,19 @@ hasOneTests(FutureOr<QueryExecutor> Function() createExecutor,
..values.legId = int.parse(originalLeg!.id!)
..values.nToes = 5.64;
var legQuery = LegQuery()..where!.id.equals(int.parse(originalLeg!.id!));
var foot = await (footQuery.insert(executor) as FutureOr<Foot>);
var leg = await (legQuery.getOne(executor) as FutureOr<Leg>);
expect(leg.name, originalLeg!.name);
expect(leg.id, originalLeg!.id);
expect(leg.foot, isNotNull);
expect(leg.foot!.id, foot.id);
expect(leg.foot!.nToes, foot.nToes);
var footOpt = await (footQuery.insert(executor));
var legOpt = await (legQuery.getOne(executor));
expect(footOpt.isPresent, true);
expect(legOpt.isPresent, true);
legOpt.ifPresent((leg) {
expect(leg.name, originalLeg!.name);
expect(leg.id, originalLeg!.id);
footOpt.ifPresent((foot) {
expect(leg.foot, isNotNull);
expect(leg.foot!.id, foot.id);
expect(leg.foot!.nToes, foot.nToes);
});
});
});
test('only fetches one foot even if there are multiple', () async {
@ -47,13 +55,19 @@ hasOneTests(FutureOr<QueryExecutor> Function() createExecutor,
..values.legId = int.parse(originalLeg!.id!)
..values.nToes = 24;
var legQuery = LegQuery()..where!.id.equals(int.parse(originalLeg!.id!));
var foot = await (footQuery.insert(executor) as FutureOr<Foot>);
var leg = await (legQuery.getOne(executor) as FutureOr<Leg>);
expect(leg.name, originalLeg!.name);
expect(leg.id, originalLeg!.id);
expect(leg.foot, isNotNull);
expect(leg.foot!.id, foot.id);
expect(leg.foot!.nToes, foot.nToes);
var footOpt = await (footQuery.insert(executor));
var legOpt = await (legQuery.getOne(executor));
expect(footOpt.isPresent, true);
expect(legOpt.isPresent, true);
legOpt.ifPresent((leg) {
expect(leg.name, originalLeg!.name);
expect(leg.id, originalLeg!.id);
expect(leg.foot, isNotNull);
footOpt.ifPresent((foot) {
expect(leg.foot!.id, foot.id);
expect(leg.foot!.nToes, foot.nToes);
});
});
});
test('sets foot on update', () async {

View file

@ -104,9 +104,10 @@ class TreeQuery extends Query<Tree, TreeQueryWhere> {
updatedAt: (row[2] as DateTime?),
rings: (row[3] as int?));
if (row.length > 4) {
model = model.copyWith(fruits: [
FruitQuery.parseRow(row.skip(4).take(5).toList()).firstOrNull
]);
var modelOpt = FruitQuery.parseRow(row.skip(4).take(5).toList());
modelOpt.ifPresent((m) {
model = model.copyWith(fruits: [m]);
});
}
return Optional.ofNullable(model);
}
@ -362,8 +363,8 @@ class Tree extends _Tree {
this.createdAt,
this.updatedAt,
this.rings,
List<_Fruit?>? fruits})
: this.fruits = List.unmodifiable(fruits ?? []);
List<_Fruit> fruits = const []})
: this.fruits = List.unmodifiable(fruits);
/// A unique identifier corresponding to this item.
@override
@ -388,13 +389,13 @@ class Tree extends _Tree {
DateTime? createdAt,
DateTime? updatedAt,
int? rings,
List<_Fruit?>? fruits}) {
List<_Fruit> fruits = const []}) {
return Tree(
id: id ?? this.id,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
rings: rings ?? this.rings,
fruits: fruits ?? this.fruits);
fruits: fruits);
}
bool operator ==(other) {
@ -527,7 +528,7 @@ class TreeSerializer extends Codec<Tree, Map> {
fruits: map['fruits'] is Iterable
? List.unmodifiable(((map['fruits'] as Iterable).whereType<Map>())
.map(FruitSerializer.fromMap))
: null);
: []);
}
static Map<String, dynamic> toMap(_Tree model) {