Testing angel_orm

This commit is contained in:
thomashii@dukefirehawk.com 2021-05-03 14:45:41 +08:00
parent 81b2e2e6b2
commit 522f5215e0
9 changed files with 94 additions and 96 deletions

View file

@ -34,10 +34,10 @@
* Migrated angel_orm to 3.0.0 (0/0 tests passed)
* Migrated angel_migration to 3.0.0 (0/0 tests passed)
* Added inflection2 and migrated to 1.0.0 (28/32 tests passed)
* Updated angel_orm_generator to 4.0.0 (0/0 tests passed)
* Updated angel_migration_runner to 3.0.0 (0/0 tests passed)
* Updated angel_orm_test to 1.0.0 (in progress)
* Updated angel_orm_postgres to 2.0.0 (in progress)
* 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 (0/0 tests passed)
* Update orm-sdk-2.12.x boilerplate (in progress) <= Milestone 2

View file

@ -9,7 +9,7 @@ class PostgreSqlExecutor extends QueryExecutor {
PostgreSQLExecutionContext _connection;
/// An optional [Logger] to print information to.
final Logger logger;
final Logger? logger;
PostgreSqlExecutor(this._connection, {this.logger});
@ -27,8 +27,8 @@ class PostgreSqlExecutor extends QueryExecutor {
@override
Future<List<List>> query(
String tableName, String query, Map<String, dynamic> substitutionValues,
[List<String> returningFields]) {
String tableName, String? query, Map<String, dynamic> substitutionValues,
[List<String>? returningFields]) {
if (returningFields != null) {
var fields = returningFields.join(', ');
var returning = 'RETURNING $fields';
@ -37,15 +37,17 @@ class PostgreSqlExecutor extends QueryExecutor {
logger?.fine('Query: $query');
logger?.fine('Values: $substitutionValues');
return _connection.query(query, substitutionValues: substitutionValues);
return _connection.query(query!, substitutionValues: substitutionValues);
}
@override
Future<T> transaction<T>(FutureOr<T> Function(QueryExecutor) f) async {
if (_connection is! PostgreSQLConnection) return await f(this);
Future<T?> transaction<T>(FutureOr<T> Function(QueryExecutor) f) async {
if (_connection is! PostgreSQLConnection) {
return await f(this);
}
var conn = _connection as PostgreSQLConnection;
T returnValue;
T? returnValue;
var txResult = await conn.transaction((ctx) async {
try {
@ -61,12 +63,12 @@ class PostgreSqlExecutor extends QueryExecutor {
});
if (txResult is PostgreSQLRollback) {
if (txResult.reason == null) {
throw StateError('The transaction was cancelled.');
} else {
throw StateError(
'The transaction was cancelled with reason "${txResult.reason}".');
}
//if (txResult.reason == null) {
// throw StateError('The transaction was cancelled.');
//} else {
throw StateError(
'The transaction was cancelled with reason "${txResult.reason}".');
//}
} else {
return returnValue;
}
@ -84,7 +86,7 @@ class PostgreSqlExecutorPool extends QueryExecutor {
final PostgreSQLConnection Function() connectionFactory;
/// An optional [Logger] to print information to.
final Logger logger;
final Logger? logger;
final List<PostgreSqlExecutor> _connections = [];
int _index = 0;
@ -124,8 +126,8 @@ class PostgreSqlExecutorPool extends QueryExecutor {
@override
Future<List<List>> query(
String tableName, String query, Map<String, dynamic> substitutionValues,
[List<String> returningFields]) {
String tableName, String? query, Map<String, dynamic> substitutionValues,
[List<String>? returningFields]) {
return _pool.withResource(() async {
var executor = await _next();
return executor.query(
@ -134,7 +136,7 @@ class PostgreSqlExecutorPool extends QueryExecutor {
}
@override
Future<T> transaction<T>(FutureOr<T> Function(QueryExecutor) f) {
Future<T?> transaction<T>(FutureOr<T> Function(QueryExecutor) f) {
return _pool.withResource(() async {
var executor = await _next();
return executor.transaction(f);

View file

@ -1,32 +1,29 @@
name: angel_orm_postgres
version: 2.0.0
version: 3.0.0
description: PostgreSQL support for Angel's ORM. Includes functionality for querying and transactions.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/orm
homepage: https://github.com/dukefirehawk/angel
publish_to: none
environment:
sdk: '>=2.10.0 <3.0.0'
sdk: '>=2.12.0 <3.0.0'
dependencies:
angel_orm:
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x
ref: sdk-2.12.x_nnbd
path: packages/orm/angel_orm
logging: ^1.0.0
pool: ^1.0.0
postgres:
git:
url: https://github.com/dukefirehawk/postgresql-dart
logging: ^1.0.1
pool: ^1.5.0
postgres: ^2.3.2
dev_dependencies:
angel_orm_test:
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x
ref: sdk-2.12.x_nnbd
path: packages/orm/angel_orm_test
pretty_logging:
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x
ref: sdk-2.12.x_nnbd
path: packages/pretty_logging
test: ^1.15.7
test: ^1.17.3

View file

@ -13,13 +13,13 @@ Future<void> closePg(QueryExecutor executor) =>
(executor as PostgreSqlExecutor).close();
Future<PostgreSqlExecutor> connectToPostgres(Iterable<String> schemas) async {
var conn = new PostgreSQLConnection('127.0.0.1', 5432, 'angel_orm_test',
username: Platform.environment['POSTGRES_USERNAME'] ?? 'postgres',
password: Platform.environment['POSTGRES_PASSWORD'] ?? 'password');
var conn = PostgreSQLConnection('127.0.0.1', 5432, 'orm_test',
username: Platform.environment['POSTGRES_USERNAME'] ?? 'test',
password: Platform.environment['POSTGRES_PASSWORD'] ?? 'test123');
await conn.open();
for (var s in schemas)
await conn.execute(await new File('test/migrations/$s.sql').readAsString());
await conn.execute(await File('test/migrations/$s.sql').readAsString());
return new PostgreSqlExecutor(conn, logger: Logger.root);
return PostgreSqlExecutor(conn, logger: Logger.root);
}

View file

@ -16,14 +16,14 @@ belongsToTests(FutureOr<QueryExecutor> Function() createExecutor,
executor = await createExecutor();
// Insert an author
var query = new AuthorQuery()..values.name = 'J.K. Rowling';
var query = AuthorQuery()..values.name = 'J.K. Rowling';
jkRowling = await query.insert(executor);
query.values.name = 'J.K. Jameson';
jameson = await query.insert(executor);
// And a book
var bookQuery = new BookQuery();
var bookQuery = BookQuery();
bookQuery.values
..authorId = int.parse(jkRowling!.id!)
..partnerAuthorId = int.parse(jameson!.id!)
@ -36,7 +36,7 @@ belongsToTests(FutureOr<QueryExecutor> Function() createExecutor,
group('selects', () {
test('select all', () async {
var query = new BookQuery();
var query = BookQuery();
List<Book?> books = await query.get(executor);
expect(books, hasLength(1));
@ -52,7 +52,7 @@ belongsToTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('select one', () async {
var query = new BookQuery();
var query = BookQuery();
query.where!.id.equals(int.parse(deathlyHallows!.id!));
print(query.compile(Set()));
@ -68,15 +68,15 @@ belongsToTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('where clause', () async {
var query = new BookQuery()
var query = BookQuery()
..where!.name.equals('Goblet of Fire')
..orWhere((w) => w!.authorId.equals(int.parse(jkRowling!.id!)));
print(query.compile(Set()));
List<Book?> books = await query.get(executor);
List<Book> books = await query.get(executor) as List<Book>;
expect(books, hasLength(1));
var book = books.first!;
var book = books.first;
print(book.toJson());
expect(book.id, deathlyHallows!.id);
expect(book.name, deathlyHallows!.name);
@ -88,9 +88,9 @@ belongsToTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('union', () async {
var query1 = new BookQuery()..where!.name.like('Deathly%');
var query2 = new BookQuery()..where!.authorId.equals(-1);
var query3 = new BookQuery()
var query1 = BookQuery()..where!.name.like('Deathly%');
var query2 = BookQuery()..where!.authorId.equals(-1);
var query3 = BookQuery()
..where!.name.isIn(['Goblet of Fire', 'Order of the Phoenix']);
query1
..union(query2)
@ -126,12 +126,12 @@ belongsToTests(FutureOr<QueryExecutor> Function() createExecutor,
test('delete stream', () async {
printSeparator('Delete stream test');
var query = new BookQuery()..where!.name.equals(deathlyHallows!.name!);
var query = BookQuery()..where!.name.equals(deathlyHallows!.name!);
print(query.compile(Set(), preamble: 'DELETE', withFields: false));
List<Book?> books = await query.delete(executor);
List<Book>? books = await query.delete(executor) as List<Book>;
expect(books, hasLength(1));
var book = books.first!;
var book = books.first;
expect(book.id, deathlyHallows!.id);
expect(book.author, isNotNull);
expect(book.author!.name, jkRowling!.name);
@ -139,7 +139,7 @@ belongsToTests(FutureOr<QueryExecutor> Function() createExecutor,
test('update book', () async {
var cloned = deathlyHallows!.copyWith(name: "Sorcerer's Stone");
var query = new BookQuery()
var query = BookQuery()
..where!.id.equals(int.parse(cloned.id!))
..values.copyFrom(cloned);
var book = await (query.updateOne(executor) as FutureOr<Book>);

View file

@ -2,7 +2,6 @@ import 'dart:async';
import 'package:angel_orm/angel_orm.dart';
import 'package:test/test.dart';
import 'models/custom_expr.dart';
import 'util.dart';
customExprTests(FutureOr<QueryExecutor> Function() createExecutor,
{FutureOr<void> Function(QueryExecutor)? close}) {

View file

@ -11,7 +11,7 @@ hasManyTests(FutureOr<QueryExecutor> Function() createExecutor,
close ??= (_) => null;
setUp(() async {
var query = new TreeQuery()..values.rings = 10;
var query = TreeQuery()..values.rings = 10;
executor = await createExecutor();
appleTree = await query.insert(executor);
@ -36,11 +36,11 @@ hasManyTests(FutureOr<QueryExecutor> Function() createExecutor,
}
setUp(() async {
var appleQuery = new FruitQuery()
var appleQuery = FruitQuery()
..values.treeId = treeId
..values.commonName = 'Apple';
var bananaQuery = new FruitQuery()
var bananaQuery = FruitQuery()
..values.treeId = treeId
..values.commonName = 'Banana';
@ -49,13 +49,13 @@ hasManyTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('can fetch any children', () async {
var query = new TreeQuery()..where!.id.equals(treeId);
var query = TreeQuery()..where!.id.equals(treeId);
var tree = await (query.getOne(executor) as FutureOr<Tree>);
verify(tree);
});
test('sets on update', () async {
var tq = new TreeQuery()
var tq = TreeQuery()
..where!.id.equals(treeId)
..values.rings = 24;
var tree = await (tq.updateOne(executor) as FutureOr<Tree>);
@ -64,13 +64,13 @@ hasManyTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('sets on delete', () async {
var tq = new TreeQuery()..where!.id.equals(treeId);
var tq = TreeQuery()..where!.id.equals(treeId);
var tree = await (tq.deleteOne(executor) as FutureOr<Tree>);
verify(tree);
});
test('returns empty on false subquery', () async {
var tq = new TreeQuery()
var tq = TreeQuery()
..where!.id.equals(treeId)
..fruits!.where!.commonName.equals('Kiwi');
var tree = await (tq.getOne(executor) as FutureOr<Tree>);

View file

@ -11,7 +11,7 @@ hasOneTests(FutureOr<QueryExecutor> Function() createExecutor,
setUp(() async {
executor = await createExecutor();
var query = new LegQuery()..values.name = 'Left';
var query = LegQuery()..values.name = 'Left';
originalLeg = await query.insert(executor);
});
@ -19,7 +19,7 @@ hasOneTests(FutureOr<QueryExecutor> Function() createExecutor,
test('sets to null if no child', () async {
print(LegQuery().compile(Set()));
var query = new LegQuery()..where!.id.equals(int.parse(originalLeg!.id!));
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);
@ -28,10 +28,10 @@ hasOneTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('can fetch one foot', () async {
var footQuery = new FootQuery()
var footQuery = FootQuery()
..values.legId = int.parse(originalLeg!.id!)
..values.nToes = 5.64;
var legQuery = new LegQuery()..where!.id.equals(int.parse(originalLeg!.id!));
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>);
@ -43,10 +43,10 @@ hasOneTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('only fetches one foot even if there are multiple', () async {
var footQuery = new FootQuery()
var footQuery = FootQuery()
..values.legId = int.parse(originalLeg!.id!)
..values.nToes = 24;
var legQuery = new LegQuery()..where!.id.equals(int.parse(originalLeg!.id!));
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);
@ -57,10 +57,10 @@ hasOneTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('sets foot on update', () async {
var footQuery = new FootQuery()
var footQuery = FootQuery()
..values.legId = int.parse(originalLeg!.id!)
..values.nToes = 5.64;
var legQuery = new LegQuery()
var legQuery = LegQuery()
..where!.id.equals(int.parse(originalLeg!.id!))
..values.copyFrom(originalLeg!.copyWith(name: 'Right'));
var foot = await (footQuery.insert(executor) as FutureOr<Foot>);
@ -73,10 +73,10 @@ hasOneTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('sets foot on delete', () async {
var footQuery = new FootQuery()
var footQuery = FootQuery()
..values.legId = int.parse(originalLeg!.id!)
..values.nToes = 5.64;
var legQuery = new LegQuery()..where!.id.equals(int.parse(originalLeg!.id!));
var legQuery = LegQuery()..where!.id.equals(int.parse(originalLeg!.id!));
var foot = await (footQuery.insert(executor) as FutureOr<Foot>);
var leg = await (legQuery.deleteOne(executor) as FutureOr<Leg>);
print(leg.toJson());
@ -87,7 +87,7 @@ hasOneTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('sets null on false subquery', () async {
var legQuery = new LegQuery()
var legQuery = LegQuery()
..where!.id.equals(originalLeg!.idAsInt!)
..foot!.where!.legId.equals(originalLeg!.idAsInt! + 1024);
var leg = await (legQuery.getOne(executor) as FutureOr<Leg>);

View file

@ -3,13 +3,13 @@ import 'package:angel_orm/angel_orm.dart';
import 'package:test/test.dart';
import 'models/car.dart';
final DateTime y2k = new DateTime.utc(2000, 1, 1);
final DateTime y2k = DateTime.utc(2000, 1, 1);
standaloneTests(FutureOr<QueryExecutor> Function() createExecutor,
{FutureOr<void> Function(QueryExecutor)? close}) {
close ??= (_) => null;
test('to where', () {
var query = new CarQuery();
var query = CarQuery();
query.where
?..familyFriendly.isTrue
..recalledAt.lessThanOrEqualTo(y2k, includeTime: false);
@ -24,7 +24,7 @@ standaloneTests(FutureOr<QueryExecutor> Function() createExecutor,
// var row = [0, 'Mazda', 'CX9', true, y2k, y2k, y2k];
var row = [0, y2k, y2k, 'Mazda', 'CX9', true, y2k];
print(row);
var car = new CarQuery().deserialize(row)!;
var car = CarQuery().deserialize(row)!;
print(car.toJson());
expect(car.id, '0');
expect(car.make, 'Mazda');
@ -47,7 +47,7 @@ standaloneTests(FutureOr<QueryExecutor> Function() createExecutor,
group('selects', () {
test('select all', () async {
List<Car?> cars = await new CarQuery().get(executor);
List<Car?> cars = await CarQuery().get(executor);
expect(cars, []);
});
@ -55,7 +55,7 @@ standaloneTests(FutureOr<QueryExecutor> Function() createExecutor,
Car? ferrari;
setUp(() async {
var query = new CarQuery();
var query = CarQuery();
query.values
..make = 'Ferrari東'
..description = 'Vroom vroom!'
@ -64,11 +64,11 @@ standaloneTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('where clause is applied', () async {
var query = new CarQuery()..where!.familyFriendly.isTrue;
var query = CarQuery()..where!.familyFriendly.isTrue;
List<Car?> cars = await query.get(executor);
expect(cars, isEmpty);
var sportsCars = new CarQuery()..where!.familyFriendly.isFalse;
var sportsCars = CarQuery()..where!.familyFriendly.isFalse;
cars = await sportsCars.get(executor);
print(cars.map((c) => c!.toJson()));
@ -80,9 +80,9 @@ standaloneTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('union', () async {
var query1 = new CarQuery()..where!.make.like('%Fer%');
var query2 = new CarQuery()..where!.familyFriendly.isTrue;
var query3 = new CarQuery()..where!.description.equals('Submarine');
var query1 = CarQuery()..where!.make.like('%Fer%');
var query2 = CarQuery()..where!.familyFriendly.isTrue;
var query3 = CarQuery()..where!.description.equals('Submarine');
Union<Car?> union = query1.union(query2).unionAll(query3);
print(union.compile(Set()));
var cars = await union.get(executor);
@ -90,7 +90,7 @@ standaloneTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('or clause', () async {
var query = new CarQuery()
var query = CarQuery()
..where!.make.like('Fer%')
..orWhere((where) => where
?..familyFriendly.isTrue
@ -101,7 +101,7 @@ standaloneTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('limit obeyed', () async {
var query = new CarQuery()..limit(0);
var query = CarQuery()..limit(0);
print(query.compile(Set()));
List<Car?> cars = await query.get(executor);
expect(cars, isEmpty);
@ -109,23 +109,23 @@ standaloneTests(FutureOr<QueryExecutor> Function() createExecutor,
test('get one', () async {
var id = int.parse(ferrari!.id!);
var query = new CarQuery()..where!.id.equals(id);
var query = CarQuery()..where!.id.equals(id);
var car = await query.getOne(executor);
expect(car, ferrari);
});
test('delete one', () async {
var id = int.parse(ferrari!.id!);
var query = new CarQuery()..where!.id.equals(id);
var query = CarQuery()..where!.id.equals(id);
var car = await (query.deleteOne(executor) as FutureOr<Car>);
expect(car.toJson(), ferrari!.toJson());
List<Car?> cars = await new CarQuery().get(executor);
List<Car?> cars = await CarQuery().get(executor);
expect(cars, isEmpty);
});
test('delete stream', () async {
var query = new CarQuery()
var query = CarQuery()
..where!.make.equals('Ferrari東')
..orWhere((w) => w!.familyFriendly.isTrue);
print(query.compile(Set(), preamble: 'DELETE FROM "cars"'));
@ -136,7 +136,7 @@ standaloneTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('update', () async {
var query = new CarQuery()
var query = CarQuery()
..where!.id.equals(int.parse(ferrari!.id!))
..values.make = 'Hyundai';
List<Car?> cars = await query.update(executor);
@ -146,7 +146,7 @@ standaloneTests(FutureOr<QueryExecutor> Function() createExecutor,
test('update car', () async {
var cloned = ferrari!.copyWith(make: 'Angel');
var query = new CarQuery()..values.copyFrom(cloned);
var query = CarQuery()..values.copyFrom(cloned);
var car = await (query.updateOne(executor) as FutureOr<Car>);
print(car.toJson());
expect(car.toJson(), cloned.toJson());
@ -155,9 +155,9 @@ standaloneTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('insert', () async {
var recalledAt = new DateTime.now();
var query = new CarQuery();
var now = new DateTime.now();
var recalledAt = DateTime.now();
var query = CarQuery();
var now = DateTime.now();
query.values
..make = 'Honda'
..description = 'Hello'
@ -175,13 +175,13 @@ standaloneTests(FutureOr<QueryExecutor> Function() createExecutor,
});
test('insert car', () async {
var recalledAt = new DateTime.now();
var beetle = new Car(
var recalledAt = DateTime.now();
var beetle = Car(
make: 'Beetle',
description: 'Herbie',
familyFriendly: true,
recalledAt: recalledAt);
var query = new CarQuery()..values.copyFrom(beetle);
var query = CarQuery()..values.copyFrom(beetle);
var car = await (query.insert(executor) as FutureOr<Car>);
print(car.toJson());
expect(car.make, beetle.make);