platform/angel_orm_test/lib/src/standalone_test.dart

192 lines
6.3 KiB
Dart
Raw Normal View History

2019-04-08 16:58:41 +00:00
import 'dart:async';
2017-06-24 21:21:32 +00:00
import 'package:angel_orm/angel_orm.dart';
2017-06-18 04:19:05 +00:00
import 'package:test/test.dart';
import 'models/car.dart';
2018-12-01 19:03:43 +00:00
final DateTime y2k = new DateTime.utc(2000, 1, 1);
2017-06-18 04:19:05 +00:00
2019-04-08 16:58:41 +00:00
standaloneTests(FutureOr<QueryExecutor> Function() createExecutor,
{FutureOr<void> Function(QueryExecutor) close}) {
close ??= (_) => null;
2017-06-18 04:19:05 +00:00
test('to where', () {
var query = new CarQuery();
query.where
2018-12-03 16:50:43 +00:00
..familyFriendly.isTrue
2018-12-01 19:03:43 +00:00
..recalledAt.lessThanOrEqualTo(y2k, includeTime: false);
var whereClause = query.where.compile(tableName: 'cars');
2017-06-18 04:19:05 +00:00
print('Where clause: $whereClause');
2017-06-24 21:21:32 +00:00
expect(whereClause,
2018-12-03 16:50:43 +00:00
'cars.family_friendly = TRUE AND cars.recalled_at <= \'2000-01-01\'');
2017-06-18 04:19:05 +00:00
});
2017-06-24 21:21:32 +00:00
test('parseRow', () {
2018-12-03 16:50:43 +00:00
var row = [0, 'Mazda', 'CX9', true, y2k, y2k, y2k];
2017-06-24 21:21:32 +00:00
print(row);
2018-12-01 19:03:43 +00:00
var car = new CarQuery().deserialize(row);
2017-06-18 04:19:05 +00:00
print(car.toJson());
2017-06-24 21:21:32 +00:00
expect(car.id, '0');
expect(car.make, 'Mazda');
expect(car.description, 'CX9');
expect(car.familyFriendly, true);
2018-12-01 19:03:43 +00:00
expect(y2k.toIso8601String(), startsWith(car.recalledAt.toIso8601String()));
expect(y2k.toIso8601String(), startsWith(car.createdAt.toIso8601String()));
expect(y2k.toIso8601String(), startsWith(car.updatedAt.toIso8601String()));
2017-06-24 21:21:32 +00:00
});
2017-07-09 16:53:35 +00:00
group('queries', () {
2019-04-08 16:58:41 +00:00
QueryExecutor executor;
2017-07-09 16:53:35 +00:00
setUp(() async {
2019-04-08 16:58:41 +00:00
executor = await createExecutor();
2017-07-09 16:53:35 +00:00
});
2019-04-08 16:58:41 +00:00
tearDown(() => close(executor));
2017-07-09 16:53:35 +00:00
group('selects', () {
test('select all', () async {
2019-04-08 16:58:41 +00:00
var cars = await new CarQuery().get(executor);
2017-07-09 16:53:35 +00:00
expect(cars, []);
});
group('with data', () {
Car ferrari;
setUp(() async {
2018-12-03 16:50:43 +00:00
var query = new CarQuery();
query.values
..make = 'Ferrari東'
..description = 'Vroom vroom!'
..familyFriendly = false;
2019-04-08 16:58:41 +00:00
ferrari = await query.insert(executor);
2017-07-09 16:53:35 +00:00
});
test('where clause is applied', () async {
2018-12-03 16:50:43 +00:00
var query = new CarQuery()..where.familyFriendly.isTrue;
2019-04-08 16:58:41 +00:00
var cars = await query.get(executor);
2017-07-09 16:53:35 +00:00
expect(cars, isEmpty);
2018-12-03 16:50:43 +00:00
var sportsCars = new CarQuery()..where.familyFriendly.isFalse;
2019-04-08 16:58:41 +00:00
cars = await sportsCars.get(executor);
2018-12-01 19:03:43 +00:00
print(cars.map((c) => c.toJson()));
2017-07-09 16:53:35 +00:00
var car = cars.first;
expect(car.make, ferrari.make);
expect(car.description, ferrari.description);
expect(car.familyFriendly, ferrari.familyFriendly);
expect(car.recalledAt, isNull);
});
2017-07-14 23:03:39 +00:00
test('union', () async {
2019-04-03 09:57:27 +00:00
var query1 = new CarQuery()..where.make.like('%Fer%');
2018-12-03 16:50:43 +00:00
var query2 = new CarQuery()..where.familyFriendly.isTrue;
2017-07-14 23:03:39 +00:00
var query3 = new CarQuery()..where.description.equals('Submarine');
2018-12-03 16:50:43 +00:00
var union = query1.union(query2).unionAll(query3);
2019-01-27 04:14:20 +00:00
print(union.compile(Set()));
2019-04-08 16:58:41 +00:00
var cars = await union.get(executor);
2017-07-14 23:03:39 +00:00
expect(cars, hasLength(1));
});
test('or clause', () async {
2017-07-09 16:53:35 +00:00
var query = new CarQuery()
2019-04-03 09:57:27 +00:00
..where.make.like('Fer%')
2018-12-03 16:50:43 +00:00
..orWhere((where) => where
..familyFriendly.isTrue
..make.equals('Honda'));
2019-01-27 04:14:20 +00:00
print(query.compile(Set()));
2019-04-08 16:58:41 +00:00
var cars = await query.get(executor);
2017-07-14 23:03:39 +00:00
expect(cars, hasLength(1));
});
test('limit obeyed', () async {
2018-12-01 19:03:43 +00:00
var query = new CarQuery()..limit(0);
2019-01-27 04:14:20 +00:00
print(query.compile(Set()));
2019-04-08 16:58:41 +00:00
var cars = await query.get(executor);
2017-07-09 16:53:35 +00:00
expect(cars, isEmpty);
});
test('get one', () async {
2018-12-03 13:41:14 +00:00
var id = int.parse(ferrari.id);
var query = new CarQuery()..where.id.equals(id);
2019-04-08 16:58:41 +00:00
var car = await query.getOne(executor);
2018-12-03 13:41:14 +00:00
expect(car, ferrari);
2017-07-09 16:53:35 +00:00
});
test('delete one', () async {
2018-12-03 13:41:14 +00:00
var id = int.parse(ferrari.id);
var query = new CarQuery()..where.id.equals(id);
2019-04-08 16:58:41 +00:00
var car = await query.deleteOne(executor);
2017-07-09 16:53:35 +00:00
expect(car.toJson(), ferrari.toJson());
2019-04-08 16:58:41 +00:00
var cars = await new CarQuery().get(executor);
2017-07-09 16:53:35 +00:00
expect(cars, isEmpty);
});
2017-07-10 21:49:00 +00:00
test('delete stream', () async {
2018-12-03 13:41:14 +00:00
var query = new CarQuery()
2018-12-03 16:50:43 +00:00
..where.make.equals('Ferrari東')
..orWhere((w) => w.familyFriendly.isTrue);
2019-01-27 04:14:20 +00:00
print(query.compile(Set(), preamble: 'DELETE FROM "cars"'));
2018-12-03 16:50:43 +00:00
2019-04-08 16:58:41 +00:00
var cars = await query.delete(executor);
2017-07-09 16:53:35 +00:00
expect(cars, hasLength(1));
expect(cars.first.toJson(), ferrari.toJson());
});
2017-07-10 21:49:00 +00:00
test('update', () async {
2018-12-03 16:50:43 +00:00
var query = new CarQuery()
..where.id.equals(int.parse(ferrari.id))
..values.make = 'Hyundai';
2019-04-08 16:58:41 +00:00
var cars = await query.update(executor);
2017-07-10 21:49:00 +00:00
expect(cars, hasLength(1));
expect(cars.first.make, 'Hyundai');
});
test('update car', () async {
2018-12-01 19:03:43 +00:00
var cloned = ferrari.copyWith(make: 'Angel');
2018-12-03 16:50:43 +00:00
var query = new CarQuery()..values.copyFrom(cloned);
2019-04-08 16:58:41 +00:00
var car = await query.updateOne(executor);
2017-07-10 21:49:00 +00:00
print(car.toJson());
expect(car.toJson(), cloned.toJson());
});
2017-07-09 16:53:35 +00:00
});
});
test('insert', () async {
var recalledAt = new DateTime.now();
2018-12-03 16:50:43 +00:00
var query = new CarQuery();
2018-12-03 23:13:11 +00:00
var now = new DateTime.now();
2018-12-03 16:50:43 +00:00
query.values
..make = 'Honda'
..description = 'Hello'
..familyFriendly = true
2018-12-03 23:13:11 +00:00
..recalledAt = recalledAt
..createdAt = now
..updatedAt = now;
2019-04-08 16:58:41 +00:00
var car = await query.insert(executor);
2017-07-09 16:53:35 +00:00
expect(car.id, isNotNull);
expect(car.make, 'Honda');
expect(car.description, 'Hello');
expect(car.familyFriendly, isTrue);
2018-12-01 19:03:43 +00:00
expect(dateYmdHms.format(car.recalledAt), dateYmdHms.format(recalledAt));
2017-07-09 16:53:35 +00:00
expect(car.createdAt, allOf(isNotNull, equals(car.updatedAt)));
});
2017-07-10 21:49:00 +00:00
test('insert car', () async {
var recalledAt = new DateTime.now();
var beetle = new Car(
make: 'Beetle',
description: 'Herbie',
familyFriendly: true,
recalledAt: recalledAt);
2018-12-03 16:50:43 +00:00
var query = new CarQuery()..values.copyFrom(beetle);
2019-04-08 16:58:41 +00:00
var car = await query.insert(executor);
2017-07-10 21:49:00 +00:00
print(car.toJson());
expect(car.make, beetle.make);
expect(car.description, beetle.description);
expect(car.familyFriendly, beetle.familyFriendly);
2018-08-24 12:30:38 +00:00
expect(dateYmdHms.format(car.recalledAt),
dateYmdHms.format(beetle.recalledAt));
2017-07-10 21:49:00 +00:00
});
2017-07-09 16:53:35 +00:00
});
2017-06-18 04:19:05 +00:00
}