Updated test cases

This commit is contained in:
thomashii@dukefirehawk.com 2021-05-05 14:26:23 +08:00
parent 50f8ba23da
commit 6e8f6aea9e
5 changed files with 94 additions and 43 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 (30/54 tests passed)
* Migrated angel_orm_postgres to 3.0.0 (35/54 tests passed)
* Update orm-sdk-2.12.x boilerplate (in progress) <= Milestone 2

View file

@ -16,8 +16,11 @@ edgeCaseTests(FutureOr<QueryExecutor> Function() createExecutor,
test('can create object with no id', () async {
var query = UnorthodoxQuery()..values.name = 'Hey';
var model = await query.insert(executor);
expect(model, Unorthodox(name: 'Hey'));
var modelOpt = await query.insert(executor);
expect(modelOpt.isPresent, true);
modelOpt.ifPresent((model) {
expect(model, Unorthodox(name: 'Hey'));
});
});
group('relations on non-model', () {
@ -25,15 +28,22 @@ edgeCaseTests(FutureOr<QueryExecutor> Function() createExecutor,
setUp(() async {
var query = UnorthodoxQuery()..values.name = 'Hey';
unorthodox = (await query.insert(executor)).value;
var unorthodoxOpt = await query.insert(executor);
unorthodoxOpt.ifPresent((value) {
unorthodox = value;
});
});
test('belongs to', () async {
var query = WeirdJoinQuery()..values.joinName = unorthodox!.name;
var model = await (query.insert(executor) as FutureOr<WeirdJoin>);
print(model.toJson());
expect(model.id, isNotNull); // Postgres should set this.
expect(model.unorthodox, unorthodox);
var modelOpt = await query.insert(executor);
expect(modelOpt.isPresent, true);
modelOpt.ifPresent((model) {
print(model.toJson());
expect(model.id, isNotNull); // Postgres should set this.
expect(model.unorthodox, unorthodox);
});
});
group('layered', () {
@ -42,36 +52,53 @@ edgeCaseTests(FutureOr<QueryExecutor> Function() createExecutor,
setUp(() async {
var wjQuery = WeirdJoinQuery()..values.joinName = unorthodox!.name;
weirdJoin = (await wjQuery.insert(executor)).value;
var gbQuery = SongQuery()
..values.weirdJoinId = weirdJoin!.id
..values.title = 'Girl Blue';
girlBlue = (await gbQuery.insert(executor)).value;
var weirdJoinOpt = await wjQuery.insert(executor);
//weirdJoin = (await wjQuery.insert(executor)).value;
weirdJoinOpt.ifPresent((value1) async {
weirdJoin = value1;
var gbQuery = SongQuery()
..values.weirdJoinId = value1.id
..values.title = 'Girl Blue';
var girlBlueOpt = await gbQuery.insert(executor);
girlBlueOpt.ifPresent((value2) {
girlBlue = value2;
});
});
});
test('has one', () async {
var query = WeirdJoinQuery()..where!.id.equals(weirdJoin!.id!);
var wj = await (query.getOne(executor) as FutureOr<WeirdJoin>);
print(wj.toJson());
expect(wj.song, girlBlue);
var wjOpt = await query.getOne(executor);
expect(wjOpt.isPresent, true);
wjOpt.ifPresent((wj) {
print(wj.toJson());
expect(wj.song, girlBlue);
});
});
test('has many', () async {
var numbas = <Numba?>[];
var numbas = <Numba>[];
for (int i = 0; i < 15; i++) {
var query = NumbaQuery()
..values.parent = weirdJoin!.id
..values.i = i;
var model = await query.insert(executor);
numbas.add(model.value);
var modelObj = await query.insert(executor);
expect(modelObj.isPresent, true);
modelObj.ifPresent((model) {
numbas.add(model);
});
}
var query = WeirdJoinQuery()..where!.id.equals(weirdJoin!.id!);
var wj = await (query.getOne(executor) as FutureOr<WeirdJoin>);
print(wj.toJson());
expect(wj.numbas, numbas);
var wjObj = await query.getOne(executor);
expect(wjObj.isPresent, true);
wjObj.ifPresent((wj) {
print(wj.toJson());
expect(wj.numbas, numbas);
});
});
test('many to many', () async {
@ -84,10 +111,13 @@ edgeCaseTests(FutureOr<QueryExecutor> Function() createExecutor,
await pivotQuery.insert(executor);
fooQuery = FooQuery()..where!.bar.equals('baz');
var foo = await (fooQuery.getOne(executor) as FutureOr<Foo>);
print(foo.toJson());
print(weirdJoin!.toJson());
expect(foo.weirdJoins![0].id, weirdJoin!.id);
var fooObj = await fooQuery.getOne(executor);
expect(fooObj.isPresent, true);
fooObj.ifPresent((foo) {
print(foo.toJson());
print(weirdJoin!.toJson());
expect(foo.weirdJoins![0].id, weirdJoin!.id);
});
});
});
});

View file

@ -14,8 +14,11 @@ enumAndNestedTests(FutureOr<QueryExecutor> Function() createExecutor,
test('insert', () async {
var query = HasCarQuery()..values.type = CarType.sedan;
var result = await (query.insert(executor) as FutureOr<HasCar>);
expect(result.type, CarType.sedan);
var resultOpt = await (query.insert(executor));
expect(resultOpt.isPresent, true);
resultOpt.ifPresent((result) {
expect(result.type, CarType.sedan);
});
});
group('query', () {

View file

@ -43,38 +43,55 @@ hasManyTests(FutureOr<QueryExecutor> Function() createExecutor,
var bananaQuery = FruitQuery()
..values.treeId = treeId
..values.commonName = 'Banana';
apple = (await appleQuery.insert(executor)).value;
banana = (await bananaQuery.insert(executor)).value;
var appleOpt = await appleQuery.insert(executor);
var bananaOpt = await bananaQuery.insert(executor);
appleOpt.ifPresent((a) {
apple = a;
});
bananaOpt.ifPresent((a) {
banana = a;
});
});
test('can fetch any children', () async {
var query = TreeQuery()..where!.id.equals(treeId);
var tree = await (query.getOne(executor) as FutureOr<Tree>);
verify(tree);
var treeOpt = await (query.getOne(executor));
expect(treeOpt.isPresent, true);
treeOpt.ifPresent((tree) {
verify(tree);
});
});
test('sets on update', () async {
var tq = TreeQuery()
..where!.id.equals(treeId)
..values.rings = 24;
var tree = await (tq.updateOne(executor) as FutureOr<Tree>);
verify(tree);
expect(tree.rings, 24);
var treeOpt = await (tq.updateOne(executor));
expect(treeOpt.isPresent, true);
treeOpt.ifPresent((tree) {
verify(tree);
expect(tree.rings, 24);
});
});
test('sets on delete', () async {
var tq = TreeQuery()..where!.id.equals(treeId);
var tree = await (tq.deleteOne(executor) as FutureOr<Tree>);
verify(tree);
var treeOpt = await (tq.deleteOne(executor));
expect(treeOpt.isPresent, true);
treeOpt.ifPresent((tree) {
verify(tree);
});
});
test('returns empty on false subquery', () async {
var tq = TreeQuery()
..where!.id.equals(treeId)
..fruits!.where!.commonName.equals('Kiwi');
var tree = await (tq.getOne(executor) as FutureOr<Tree>);
expect(tree.fruits, isEmpty);
var treeOpt = await (tq.getOne(executor));
expect(treeOpt.isPresent, true);
treeOpt.ifPresent((tree) {
expect(tree.fruits, isEmpty);
});
});
});
}

View file

@ -659,9 +659,10 @@ class FooQuery extends Query<Foo, FooQueryWhere> {
}
var model = Foo(bar: (row[0] as String?));
if (row.length > 1) {
model = model.copyWith(weirdJoins: [
WeirdJoinQuery.parseRow(row.skip(1).take(2).toList()).value
]);
var weirdOpt = WeirdJoinQuery.parseRow(row.skip(1).take(2).toList());
weirdOpt.ifPresent((value) {
model = model.copyWith(weirdJoins: [value]);
});
}
return Optional.ofNullable(model);
}