Move all tests to angel_orm_test
This commit is contained in:
parent
97e146a80e
commit
260580ae00
62 changed files with 159 additions and 452 deletions
|
@ -1,67 +0,0 @@
|
||||||
import 'dart:async';
|
|
||||||
import 'dart:io';
|
|
||||||
import 'package:angel_orm/angel_orm.dart';
|
|
||||||
import 'package:postgres/postgres.dart';
|
|
||||||
|
|
||||||
Future<PostgresExecutor> 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');
|
|
||||||
await conn.open();
|
|
||||||
|
|
||||||
for (var s in schemas)
|
|
||||||
await conn.execute(await new File('test/migrations/$s.sql').readAsString());
|
|
||||||
|
|
||||||
return new PostgresExecutor(conn);
|
|
||||||
}
|
|
||||||
|
|
||||||
class PostgresExecutor extends QueryExecutor {
|
|
||||||
PostgreSQLExecutionContext connection;
|
|
||||||
|
|
||||||
PostgresExecutor(this.connection);
|
|
||||||
|
|
||||||
Future close() => (connection as PostgreSQLConnection).close();
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<List<List>> query(
|
|
||||||
String tableName, String query, Map<String, dynamic> substitutionValues,
|
|
||||||
[List<String> returningFields]) async {
|
|
||||||
if (returningFields != null) {
|
|
||||||
var fields = returningFields.join(', ');
|
|
||||||
var returning = 'RETURNING $fields';
|
|
||||||
query = '$query $returning';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Platform.environment.containsKey('STFU')) {
|
|
||||||
print('Running: $query');
|
|
||||||
if (substitutionValues.isNotEmpty) print('Values: $substitutionValues');
|
|
||||||
print(substitutionValues.map((k, v) => MapEntry(k, v.runtimeType)));
|
|
||||||
}
|
|
||||||
|
|
||||||
var rows =
|
|
||||||
await connection.query(query, substitutionValues: substitutionValues);
|
|
||||||
|
|
||||||
if (!Platform.environment.containsKey('STFU')) {
|
|
||||||
print('Got ${rows.length} row(s):');
|
|
||||||
rows.forEach(print);
|
|
||||||
}
|
|
||||||
|
|
||||||
return rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<T> transaction<T>(FutureOr<T> Function() f) async {
|
|
||||||
if (connection is! PostgreSQLConnection) return await f();
|
|
||||||
var old = connection;
|
|
||||||
T result;
|
|
||||||
try {
|
|
||||||
await (connection as PostgreSQLConnection).transaction((ctx) async {
|
|
||||||
connection = ctx;
|
|
||||||
result = await f();
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
connection = old;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,27 +1,31 @@
|
||||||
import 'dart:async';
|
|
||||||
|
|
||||||
import 'package:angel_orm_postgres/angel_orm_postgres.dart';
|
|
||||||
import 'package:angel_orm_test/angel_orm_test.dart';
|
import 'package:angel_orm_test/angel_orm_test.dart';
|
||||||
import 'package:postgres/postgres.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
import 'common.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
PostgreSQLConnection _connect() {
|
Logger.root.onRecord.listen((rec) {
|
||||||
return PostgreSQLConnection('localhost', 5432, 'angel_orm_test');
|
print(rec);
|
||||||
}
|
if (rec.error != null) print(rec.error);
|
||||||
|
if (rec.stackTrace != null) print(rec.stackTrace);
|
||||||
|
});
|
||||||
|
|
||||||
group('single', () {
|
group('postgresql', () {
|
||||||
PostgreSqlExecutor executor;
|
group('belongsTo',
|
||||||
PostgreSQLConnection c = _connect();
|
() => belongsToTests(pg(['author', 'book']), close: closePg));
|
||||||
|
group(
|
||||||
setUp(() async {
|
'edgeCase',
|
||||||
var c = _connect();
|
() => edgeCaseTests(pg(['unorthodox', 'weird_join', 'song', 'numba']),
|
||||||
await c.open();
|
close: closePg));
|
||||||
executor = PostgreSqlExecutor(c);
|
group('enumAndNested',
|
||||||
});
|
() => enumAndNestedTests(pg(['has_car']), close: closePg));
|
||||||
|
group('hasMany', () => hasManyTests(pg(['tree', 'fruit']), close: closePg));
|
||||||
tearDown(() => executor.close());
|
group('hasMap', () => hasMapTests(pg(['has_map']), close: closePg));
|
||||||
|
group('hasOne', () => hasOneTests(pg(['leg', 'foot']), close: closePg));
|
||||||
ormTests(() => executor);
|
group(
|
||||||
|
'manyToMany',
|
||||||
|
() =>
|
||||||
|
manyToManyTests(pg(['user', 'role', 'user_role']), close: closePg));
|
||||||
|
group('standalone', () => standaloneTests(pg(['car']), close: closePg));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
25
angel_orm_postgres/test/common.dart
Normal file
25
angel_orm_postgres/test/common.dart
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
|
import 'package:angel_orm_postgres/angel_orm_postgres.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:postgres/postgres.dart';
|
||||||
|
|
||||||
|
FutureOr<QueryExecutor> Function() pg(Iterable<String> schemas) {
|
||||||
|
return () => connectToPostgres(schemas);
|
||||||
|
}
|
||||||
|
|
||||||
|
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');
|
||||||
|
await conn.open();
|
||||||
|
|
||||||
|
for (var s in schemas)
|
||||||
|
await conn.execute(await new File('test/migrations/$s.sql').readAsString());
|
||||||
|
|
||||||
|
return new PostgreSqlExecutor(conn, logger: Logger.root);
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:angel_framework/angel_framework.dart';
|
import 'package:angel_framework/angel_framework.dart';
|
||||||
import 'package:angel_orm/angel_orm.dart';
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
import '../models/car.dart';
|
import 'package:angel_orm_test/src/models/car.dart';
|
||||||
|
|
||||||
@Expose('/api/cars')
|
@Expose('/api/cars')
|
||||||
class CarController extends Controller {
|
class CarController extends Controller {
|
|
@ -1,2 +1,8 @@
|
||||||
export 'src/cases/cases.dart';
|
export 'src/belongs_to_test.dart';
|
||||||
export 'src/models/models.dart';
|
export 'src/edge_case_test.dart';
|
||||||
|
export 'src/enum_and_nested_test.dart';
|
||||||
|
export 'src/has_many_test.dart';
|
||||||
|
export 'src/has_map_test.dart';
|
||||||
|
export 'src/has_one_test.dart';
|
||||||
|
export 'src/many_to_many_test.dart';
|
||||||
|
export 'src/standalone_test.dart';
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
/// Tests for @belongsTo...
|
import 'dart:async';
|
||||||
library angel_orm_generator.test.book_test;
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
|
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
import 'models/author.dart';
|
import 'models/author.dart';
|
||||||
import 'models/book.dart';
|
import 'models/book.dart';
|
||||||
import 'common.dart';
|
|
||||||
|
|
||||||
main() {
|
belongsToTests(FutureOr<QueryExecutor> Function() createExecutor,
|
||||||
PostgresExecutor executor;
|
{FutureOr<void> Function(QueryExecutor) close}) {
|
||||||
|
QueryExecutor executor;
|
||||||
Author jkRowling;
|
Author jkRowling;
|
||||||
Author jameson;
|
Author jameson;
|
||||||
Book deathlyHallows;
|
Book deathlyHallows;
|
||||||
|
close ??= (_) => null;
|
||||||
|
|
||||||
setUp(() async {
|
setUp(() async {
|
||||||
executor = await connectToPostgres(['author', 'book']);
|
executor = await createExecutor();
|
||||||
|
|
||||||
// Insert an author
|
// Insert an author
|
||||||
var query = new AuthorQuery()..values.name = 'J.K. Rowling';
|
var query = new AuthorQuery()..values.name = 'J.K. Rowling';
|
||||||
|
@ -32,7 +32,7 @@ main() {
|
||||||
deathlyHallows = await bookQuery.insert(executor);
|
deathlyHallows = await bookQuery.insert(executor);
|
||||||
});
|
});
|
||||||
|
|
||||||
tearDown(() => executor.close());
|
tearDown(() => close(executor));
|
||||||
|
|
||||||
group('selects', () {
|
group('selects', () {
|
||||||
test('select all', () async {
|
test('select all', () async {
|
|
@ -1,7 +0,0 @@
|
||||||
import 'package:angel_orm/angel_orm.dart';
|
|
||||||
import 'package:test/test.dart';
|
|
||||||
import 'standalone.dart';
|
|
||||||
|
|
||||||
void ormTests(QueryExecutor Function() executor) {
|
|
||||||
group('standalone', standaloneTests(executor));
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
import 'package:angel_orm/angel_orm.dart';
|
|
||||||
import 'package:angel_orm_test/angel_orm_test.dart';
|
|
||||||
import 'package:test/test.dart';
|
|
||||||
|
|
||||||
void Function() standaloneTests(QueryExecutor Function() executorFn) {
|
|
||||||
return () {
|
|
||||||
test('insert one', () async {
|
|
||||||
var executor = executorFn();
|
|
||||||
var query = TodoQuery();
|
|
||||||
query.values
|
|
||||||
..isComplete = false
|
|
||||||
..text = 'Clean your dirty room';
|
|
||||||
var todo = await query.insert(executor);
|
|
||||||
print(todo.toJson());
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -1,15 +1,19 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
import 'models/unorthodox.dart';
|
import 'models/unorthodox.dart';
|
||||||
import 'common.dart';
|
|
||||||
|
|
||||||
main() {
|
edgeCaseTests(FutureOr<QueryExecutor> Function() createExecutor,
|
||||||
PostgresExecutor executor;
|
{FutureOr<void> Function(QueryExecutor) close}) {
|
||||||
|
QueryExecutor executor;
|
||||||
|
close ??= (_) => null;
|
||||||
|
|
||||||
setUp(() async {
|
setUp(() async {
|
||||||
executor =
|
executor = await createExecutor();
|
||||||
await connectToPostgres(['unorthodox', 'weird_join', 'song', 'numba']);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tearDown(() => close(executor));
|
||||||
|
|
||||||
test('can create object with no id', () async {
|
test('can create object with no id', () async {
|
||||||
var query = UnorthodoxQuery()..values.name = 'Hey';
|
var query = UnorthodoxQuery()..values.name = 'Hey';
|
||||||
var model = await query.insert(executor);
|
var model = await query.insert(executor);
|
|
@ -1,12 +1,15 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
import 'models/has_car.dart';
|
import 'models/has_car.dart';
|
||||||
import 'common.dart';
|
|
||||||
|
|
||||||
main() {
|
enumAndNestedTests(FutureOr<QueryExecutor> Function() createExecutor,
|
||||||
PostgresExecutor executor;
|
{FutureOr<void> Function(QueryExecutor) close}) {
|
||||||
|
QueryExecutor executor;
|
||||||
|
close ??= (_) => null;
|
||||||
|
|
||||||
setUp(() async {
|
setUp(() async {
|
||||||
executor = await connectToPostgres(['has_car']);
|
executor = await createExecutor();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('insert', () async {
|
test('insert', () async {
|
|
@ -1,21 +1,26 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
import 'models/fruit.dart';
|
import 'models/fruit.dart';
|
||||||
import 'models/tree.dart';
|
import 'models/tree.dart';
|
||||||
import 'common.dart';
|
|
||||||
|
|
||||||
main() {
|
hasManyTests(FutureOr<QueryExecutor> Function() createExecutor,
|
||||||
PostgresExecutor executor;
|
{FutureOr<void> Function(QueryExecutor) close}) {
|
||||||
|
QueryExecutor executor;
|
||||||
Tree appleTree;
|
Tree appleTree;
|
||||||
int treeId;
|
int treeId;
|
||||||
|
close ??= (_) => null;
|
||||||
|
|
||||||
setUp(() async {
|
setUp(() async {
|
||||||
var query = new TreeQuery()..values.rings = 10;
|
var query = new TreeQuery()..values.rings = 10;
|
||||||
|
|
||||||
executor = await connectToPostgres(['tree', 'fruit']);
|
executor = await createExecutor();
|
||||||
appleTree = await query.insert(executor);
|
appleTree = await query.insert(executor);
|
||||||
treeId = int.parse(appleTree.id);
|
treeId = int.parse(appleTree.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tearDown(() => close(executor));
|
||||||
|
|
||||||
test('list is empty if there is nothing', () {
|
test('list is empty if there is nothing', () {
|
||||||
expect(appleTree.rings, 10);
|
expect(appleTree.rings, 10);
|
||||||
expect(appleTree.fruits, isEmpty);
|
expect(appleTree.fruits, isEmpty);
|
|
@ -1,14 +1,19 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
import 'models/has_map.dart';
|
import 'models/has_map.dart';
|
||||||
import 'common.dart';
|
|
||||||
|
|
||||||
main() {
|
hasMapTests(FutureOr<QueryExecutor> Function() createExecutor,
|
||||||
PostgresExecutor executor;
|
{FutureOr<void> Function(QueryExecutor) close}) {
|
||||||
|
QueryExecutor executor;
|
||||||
|
close ??= (_) => null;
|
||||||
|
|
||||||
setUp(() async {
|
setUp(() async {
|
||||||
executor = await connectToPostgres(['has_map']);
|
executor = await createExecutor();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tearDown(() => close(executor));
|
||||||
|
|
||||||
test('insert', () async {
|
test('insert', () async {
|
||||||
var query = HasMapQuery();
|
var query = HasMapQuery();
|
||||||
query.values
|
query.values
|
|
@ -1,21 +1,23 @@
|
||||||
/// Tests for @hasOne...
|
import 'dart:async';
|
||||||
library angel_orm_generator.test.has_one_test;
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
|
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
import 'models/foot.dart';
|
import 'models/foot.dart';
|
||||||
import 'models/leg.dart';
|
import 'models/leg.dart';
|
||||||
import 'common.dart';
|
|
||||||
|
|
||||||
main() {
|
hasOneTests(FutureOr<QueryExecutor> Function() createExecutor,
|
||||||
PostgresExecutor executor;
|
{FutureOr<void> Function(QueryExecutor) close}) {
|
||||||
|
QueryExecutor executor;
|
||||||
Leg originalLeg;
|
Leg originalLeg;
|
||||||
|
close ??= (_) => null;
|
||||||
|
|
||||||
setUp(() async {
|
setUp(() async {
|
||||||
executor = await connectToPostgres(['leg', 'foot']);
|
executor = await createExecutor();
|
||||||
var query = new LegQuery()..values.name = 'Left';
|
var query = new LegQuery()..values.name = 'Left';
|
||||||
originalLeg = await query.insert(executor);
|
originalLeg = await query.insert(executor);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tearDown(() => close(executor));
|
||||||
|
|
||||||
test('sets to null if no child', () async {
|
test('sets to null if no child', () async {
|
||||||
print(LegQuery().compile(Set()));
|
print(LegQuery().compile(Set()));
|
||||||
var query = new LegQuery()..where.id.equals(int.parse(originalLeg.id));
|
var query = new LegQuery()..where.id.equals(int.parse(originalLeg.id));
|
|
@ -1,15 +1,15 @@
|
||||||
library angel_orm_generator.test;
|
|
||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
import 'models/user.dart';
|
import 'models/user.dart';
|
||||||
import 'common.dart';
|
|
||||||
|
|
||||||
main() {
|
manyToManyTests(FutureOr<QueryExecutor> Function() createExecutor,
|
||||||
PostgresExecutor executor;
|
{FutureOr<void> Function(QueryExecutor) close}) {
|
||||||
|
QueryExecutor executor;
|
||||||
Role canPub, canSub;
|
Role canPub, canSub;
|
||||||
User thosakwe;
|
User thosakwe;
|
||||||
|
close ??= (_) => null;
|
||||||
|
|
||||||
Future<void> dumpQuery(String query) async {
|
Future<void> dumpQuery(String query) async {
|
||||||
if (Platform.environment.containsKey('STFU')) return;
|
if (Platform.environment.containsKey('STFU')) return;
|
||||||
|
@ -17,14 +17,14 @@ main() {
|
||||||
print('==================================================');
|
print('==================================================');
|
||||||
print(' DUMPING QUERY');
|
print(' DUMPING QUERY');
|
||||||
print(query);
|
print(query);
|
||||||
var rows = await executor.connection.query(query);
|
var rows = await executor.query(null, query, {});
|
||||||
print('\n${rows.length} row(s):');
|
print('\n${rows.length} row(s):');
|
||||||
rows.forEach((r) => print(' * $r'));
|
rows.forEach((r) => print(' * $r'));
|
||||||
print('==================================================\n\n');
|
print('==================================================\n\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
setUp(() async {
|
setUp(() async {
|
||||||
executor = await connectToPostgres(['user', 'role', 'user_role']);
|
executor = await createExecutor();
|
||||||
|
|
||||||
// await dumpQuery("""
|
// await dumpQuery("""
|
||||||
// WITH roles as
|
// WITH roles as
|
||||||
|
@ -87,6 +87,8 @@ main() {
|
||||||
print('==================================================\n\n');
|
print('==================================================\n\n');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tearDown(() => close(executor));
|
||||||
|
|
||||||
Future<User> fetchThosakwe() async {
|
Future<User> fetchThosakwe() async {
|
||||||
var query = UserQuery()..where.id.equals(int.parse(thosakwe.id));
|
var query = UserQuery()..where.id.equals(int.parse(thosakwe.id));
|
||||||
return await query.getOne(executor);
|
return await query.getOne(executor);
|
|
@ -1 +0,0 @@
|
||||||
export 'todo.dart';
|
|
|
@ -1,12 +0,0 @@
|
||||||
import 'package:angel_migration/angel_migration.dart';
|
|
||||||
import 'package:angel_serialize/angel_serialize.dart';
|
|
||||||
import 'package:angel_orm/angel_orm.dart';
|
|
||||||
part 'todo.g.dart';
|
|
||||||
|
|
||||||
@serializable
|
|
||||||
@orm
|
|
||||||
abstract class _Todo extends Model {
|
|
||||||
String get text;
|
|
||||||
|
|
||||||
bool get isComplete;
|
|
||||||
}
|
|
|
@ -1,258 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
part of 'todo.dart';
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// MigrationGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
class TodoMigration extends Migration {
|
|
||||||
@override
|
|
||||||
up(Schema schema) {
|
|
||||||
schema.create('todos', (table) {
|
|
||||||
table.serial('id')..primaryKey();
|
|
||||||
table.varChar('text');
|
|
||||||
table.boolean('is_complete');
|
|
||||||
table.timeStamp('created_at');
|
|
||||||
table.timeStamp('updated_at');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
down(Schema schema) {
|
|
||||||
schema.drop('todos');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// OrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
class TodoQuery extends Query<Todo, TodoQueryWhere> {
|
|
||||||
TodoQuery({Set<String> trampoline}) {
|
|
||||||
trampoline ??= Set();
|
|
||||||
trampoline.add(tableName);
|
|
||||||
_where = TodoQueryWhere(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
final TodoQueryValues values = TodoQueryValues();
|
|
||||||
|
|
||||||
TodoQueryWhere _where;
|
|
||||||
|
|
||||||
@override
|
|
||||||
get casts {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
get tableName {
|
|
||||||
return 'todos';
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
get fields {
|
|
||||||
return const ['id', 'text', 'is_complete', 'created_at', 'updated_at'];
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
TodoQueryWhere get where {
|
|
||||||
return _where;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
TodoQueryWhere newWhereClause() {
|
|
||||||
return TodoQueryWhere(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
static Todo parseRow(List row) {
|
|
||||||
if (row.every((x) => x == null)) return null;
|
|
||||||
var model = Todo(
|
|
||||||
id: row[0].toString(),
|
|
||||||
text: (row[1] as String),
|
|
||||||
isComplete: (row[2] as bool),
|
|
||||||
createdAt: (row[3] as DateTime),
|
|
||||||
updatedAt: (row[4] as DateTime));
|
|
||||||
return model;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
deserialize(List row) {
|
|
||||||
return parseRow(row);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TodoQueryWhere extends QueryWhere {
|
|
||||||
TodoQueryWhere(TodoQuery query)
|
|
||||||
: id = NumericSqlExpressionBuilder<int>(query, 'id'),
|
|
||||||
text = StringSqlExpressionBuilder(query, 'text'),
|
|
||||||
isComplete = BooleanSqlExpressionBuilder(query, 'is_complete'),
|
|
||||||
createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'),
|
|
||||||
updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at');
|
|
||||||
|
|
||||||
final NumericSqlExpressionBuilder<int> id;
|
|
||||||
|
|
||||||
final StringSqlExpressionBuilder text;
|
|
||||||
|
|
||||||
final BooleanSqlExpressionBuilder isComplete;
|
|
||||||
|
|
||||||
final DateTimeSqlExpressionBuilder createdAt;
|
|
||||||
|
|
||||||
final DateTimeSqlExpressionBuilder updatedAt;
|
|
||||||
|
|
||||||
@override
|
|
||||||
get expressionBuilders {
|
|
||||||
return [id, text, isComplete, createdAt, updatedAt];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TodoQueryValues extends MapQueryValues {
|
|
||||||
@override
|
|
||||||
get casts {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
int get id {
|
|
||||||
return (values['id'] as int);
|
|
||||||
}
|
|
||||||
|
|
||||||
set id(int value) => values['id'] = value;
|
|
||||||
String get text {
|
|
||||||
return (values['text'] as String);
|
|
||||||
}
|
|
||||||
|
|
||||||
set text(String value) => values['text'] = value;
|
|
||||||
bool get isComplete {
|
|
||||||
return (values['is_complete'] as bool);
|
|
||||||
}
|
|
||||||
|
|
||||||
set isComplete(bool value) => values['is_complete'] = value;
|
|
||||||
DateTime get createdAt {
|
|
||||||
return (values['created_at'] as DateTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
set createdAt(DateTime value) => values['created_at'] = value;
|
|
||||||
DateTime get updatedAt {
|
|
||||||
return (values['updated_at'] as DateTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
set updatedAt(DateTime value) => values['updated_at'] = value;
|
|
||||||
void copyFrom(Todo model) {
|
|
||||||
text = model.text;
|
|
||||||
isComplete = model.isComplete;
|
|
||||||
createdAt = model.createdAt;
|
|
||||||
updatedAt = model.updatedAt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// JsonModelGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
@generatedSerializable
|
|
||||||
class Todo extends _Todo {
|
|
||||||
Todo({this.id, this.text, this.isComplete, this.createdAt, this.updatedAt});
|
|
||||||
|
|
||||||
@override
|
|
||||||
final String id;
|
|
||||||
|
|
||||||
@override
|
|
||||||
final String text;
|
|
||||||
|
|
||||||
@override
|
|
||||||
final bool isComplete;
|
|
||||||
|
|
||||||
@override
|
|
||||||
final DateTime createdAt;
|
|
||||||
|
|
||||||
@override
|
|
||||||
final DateTime updatedAt;
|
|
||||||
|
|
||||||
Todo copyWith(
|
|
||||||
{String id,
|
|
||||||
String text,
|
|
||||||
bool isComplete,
|
|
||||||
DateTime createdAt,
|
|
||||||
DateTime updatedAt}) {
|
|
||||||
return new Todo(
|
|
||||||
id: id ?? this.id,
|
|
||||||
text: text ?? this.text,
|
|
||||||
isComplete: isComplete ?? this.isComplete,
|
|
||||||
createdAt: createdAt ?? this.createdAt,
|
|
||||||
updatedAt: updatedAt ?? this.updatedAt);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator ==(other) {
|
|
||||||
return other is _Todo &&
|
|
||||||
other.id == id &&
|
|
||||||
other.text == text &&
|
|
||||||
other.isComplete == isComplete &&
|
|
||||||
other.createdAt == createdAt &&
|
|
||||||
other.updatedAt == updatedAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
int get hashCode {
|
|
||||||
return hashObjects([id, text, isComplete, createdAt, updatedAt]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, dynamic> toJson() {
|
|
||||||
return TodoSerializer.toMap(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// SerializerGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
abstract class TodoSerializer {
|
|
||||||
static Todo fromMap(Map map) {
|
|
||||||
return new Todo(
|
|
||||||
id: map['id'] as String,
|
|
||||||
text: map['text'] as String,
|
|
||||||
isComplete: map['is_complete'] as bool,
|
|
||||||
createdAt: map['created_at'] != null
|
|
||||||
? (map['created_at'] is DateTime
|
|
||||||
? (map['created_at'] as DateTime)
|
|
||||||
: DateTime.parse(map['created_at'].toString()))
|
|
||||||
: null,
|
|
||||||
updatedAt: map['updated_at'] != null
|
|
||||||
? (map['updated_at'] is DateTime
|
|
||||||
? (map['updated_at'] as DateTime)
|
|
||||||
: DateTime.parse(map['updated_at'].toString()))
|
|
||||||
: null);
|
|
||||||
}
|
|
||||||
|
|
||||||
static Map<String, dynamic> toMap(_Todo model) {
|
|
||||||
if (model == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
'id': model.id,
|
|
||||||
'text': model.text,
|
|
||||||
'is_complete': model.isComplete,
|
|
||||||
'created_at': model.createdAt?.toIso8601String(),
|
|
||||||
'updated_at': model.updatedAt?.toIso8601String()
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract class TodoFields {
|
|
||||||
static const List<String> allFields = <String>[
|
|
||||||
id,
|
|
||||||
text,
|
|
||||||
isComplete,
|
|
||||||
createdAt,
|
|
||||||
updatedAt
|
|
||||||
];
|
|
||||||
|
|
||||||
static const String id = 'id';
|
|
||||||
|
|
||||||
static const String text = 'text';
|
|
||||||
|
|
||||||
static const String isComplete = 'is_complete';
|
|
||||||
|
|
||||||
static const String createdAt = 'created_at';
|
|
||||||
|
|
||||||
static const String updatedAt = 'updated_at';
|
|
||||||
}
|
|
|
@ -1,14 +1,13 @@
|
||||||
/// Test for queries without relationships.
|
import 'dart:async';
|
||||||
library angel_orm_generator.test.car_test;
|
|
||||||
|
|
||||||
import 'package:angel_orm/angel_orm.dart';
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
import 'models/car.dart';
|
import 'models/car.dart';
|
||||||
import 'common.dart';
|
|
||||||
|
|
||||||
final DateTime y2k = new DateTime.utc(2000, 1, 1);
|
final DateTime y2k = new DateTime.utc(2000, 1, 1);
|
||||||
|
|
||||||
main() {
|
standaloneTests(FutureOr<QueryExecutor> Function() createExecutor,
|
||||||
|
{FutureOr<void> Function(QueryExecutor) close}) {
|
||||||
|
close ??= (_) => null;
|
||||||
test('to where', () {
|
test('to where', () {
|
||||||
var query = new CarQuery();
|
var query = new CarQuery();
|
||||||
query.where
|
query.where
|
||||||
|
@ -35,15 +34,17 @@ main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
group('queries', () {
|
group('queries', () {
|
||||||
PostgresExecutor connection;
|
QueryExecutor executor;
|
||||||
|
|
||||||
setUp(() async {
|
setUp(() async {
|
||||||
connection = await connectToPostgres(['car']);
|
executor = await createExecutor();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tearDown(() => close(executor));
|
||||||
|
|
||||||
group('selects', () {
|
group('selects', () {
|
||||||
test('select all', () async {
|
test('select all', () async {
|
||||||
var cars = await new CarQuery().get(connection);
|
var cars = await new CarQuery().get(executor);
|
||||||
expect(cars, []);
|
expect(cars, []);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -56,18 +57,16 @@ main() {
|
||||||
..make = 'Ferrari東'
|
..make = 'Ferrari東'
|
||||||
..description = 'Vroom vroom!'
|
..description = 'Vroom vroom!'
|
||||||
..familyFriendly = false;
|
..familyFriendly = false;
|
||||||
ferrari = await query.insert(connection);
|
ferrari = await query.insert(executor);
|
||||||
});
|
});
|
||||||
|
|
||||||
tearDown(() => connection.close());
|
|
||||||
|
|
||||||
test('where clause is applied', () async {
|
test('where clause is applied', () async {
|
||||||
var query = new CarQuery()..where.familyFriendly.isTrue;
|
var query = new CarQuery()..where.familyFriendly.isTrue;
|
||||||
var cars = await query.get(connection);
|
var cars = await query.get(executor);
|
||||||
expect(cars, isEmpty);
|
expect(cars, isEmpty);
|
||||||
|
|
||||||
var sportsCars = new CarQuery()..where.familyFriendly.isFalse;
|
var sportsCars = new CarQuery()..where.familyFriendly.isFalse;
|
||||||
cars = await sportsCars.get(connection);
|
cars = await sportsCars.get(executor);
|
||||||
print(cars.map((c) => c.toJson()));
|
print(cars.map((c) => c.toJson()));
|
||||||
|
|
||||||
var car = cars.first;
|
var car = cars.first;
|
||||||
|
@ -83,7 +82,7 @@ main() {
|
||||||
var query3 = new CarQuery()..where.description.equals('Submarine');
|
var query3 = new CarQuery()..where.description.equals('Submarine');
|
||||||
var union = query1.union(query2).unionAll(query3);
|
var union = query1.union(query2).unionAll(query3);
|
||||||
print(union.compile(Set()));
|
print(union.compile(Set()));
|
||||||
var cars = await union.get(connection);
|
var cars = await union.get(executor);
|
||||||
expect(cars, hasLength(1));
|
expect(cars, hasLength(1));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -94,31 +93,31 @@ main() {
|
||||||
..familyFriendly.isTrue
|
..familyFriendly.isTrue
|
||||||
..make.equals('Honda'));
|
..make.equals('Honda'));
|
||||||
print(query.compile(Set()));
|
print(query.compile(Set()));
|
||||||
var cars = await query.get(connection);
|
var cars = await query.get(executor);
|
||||||
expect(cars, hasLength(1));
|
expect(cars, hasLength(1));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('limit obeyed', () async {
|
test('limit obeyed', () async {
|
||||||
var query = new CarQuery()..limit(0);
|
var query = new CarQuery()..limit(0);
|
||||||
print(query.compile(Set()));
|
print(query.compile(Set()));
|
||||||
var cars = await query.get(connection);
|
var cars = await query.get(executor);
|
||||||
expect(cars, isEmpty);
|
expect(cars, isEmpty);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('get one', () async {
|
test('get one', () async {
|
||||||
var id = int.parse(ferrari.id);
|
var id = int.parse(ferrari.id);
|
||||||
var query = new CarQuery()..where.id.equals(id);
|
var query = new CarQuery()..where.id.equals(id);
|
||||||
var car = await query.getOne(connection);
|
var car = await query.getOne(executor);
|
||||||
expect(car, ferrari);
|
expect(car, ferrari);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('delete one', () async {
|
test('delete one', () async {
|
||||||
var id = int.parse(ferrari.id);
|
var id = int.parse(ferrari.id);
|
||||||
var query = new CarQuery()..where.id.equals(id);
|
var query = new CarQuery()..where.id.equals(id);
|
||||||
var car = await query.deleteOne(connection);
|
var car = await query.deleteOne(executor);
|
||||||
expect(car.toJson(), ferrari.toJson());
|
expect(car.toJson(), ferrari.toJson());
|
||||||
|
|
||||||
var cars = await new CarQuery().get(connection);
|
var cars = await new CarQuery().get(executor);
|
||||||
expect(cars, isEmpty);
|
expect(cars, isEmpty);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -128,7 +127,7 @@ main() {
|
||||||
..orWhere((w) => w.familyFriendly.isTrue);
|
..orWhere((w) => w.familyFriendly.isTrue);
|
||||||
print(query.compile(Set(), preamble: 'DELETE FROM "cars"'));
|
print(query.compile(Set(), preamble: 'DELETE FROM "cars"'));
|
||||||
|
|
||||||
var cars = await query.delete(connection);
|
var cars = await query.delete(executor);
|
||||||
expect(cars, hasLength(1));
|
expect(cars, hasLength(1));
|
||||||
expect(cars.first.toJson(), ferrari.toJson());
|
expect(cars.first.toJson(), ferrari.toJson());
|
||||||
});
|
});
|
||||||
|
@ -137,7 +136,7 @@ main() {
|
||||||
var query = new CarQuery()
|
var query = new CarQuery()
|
||||||
..where.id.equals(int.parse(ferrari.id))
|
..where.id.equals(int.parse(ferrari.id))
|
||||||
..values.make = 'Hyundai';
|
..values.make = 'Hyundai';
|
||||||
var cars = await query.update(connection);
|
var cars = await query.update(executor);
|
||||||
expect(cars, hasLength(1));
|
expect(cars, hasLength(1));
|
||||||
expect(cars.first.make, 'Hyundai');
|
expect(cars.first.make, 'Hyundai');
|
||||||
});
|
});
|
||||||
|
@ -145,7 +144,7 @@ main() {
|
||||||
test('update car', () async {
|
test('update car', () async {
|
||||||
var cloned = ferrari.copyWith(make: 'Angel');
|
var cloned = ferrari.copyWith(make: 'Angel');
|
||||||
var query = new CarQuery()..values.copyFrom(cloned);
|
var query = new CarQuery()..values.copyFrom(cloned);
|
||||||
var car = await query.updateOne(connection);
|
var car = await query.updateOne(executor);
|
||||||
print(car.toJson());
|
print(car.toJson());
|
||||||
expect(car.toJson(), cloned.toJson());
|
expect(car.toJson(), cloned.toJson());
|
||||||
});
|
});
|
||||||
|
@ -163,7 +162,7 @@ main() {
|
||||||
..recalledAt = recalledAt
|
..recalledAt = recalledAt
|
||||||
..createdAt = now
|
..createdAt = now
|
||||||
..updatedAt = now;
|
..updatedAt = now;
|
||||||
var car = await query.insert(connection);
|
var car = await query.insert(executor);
|
||||||
expect(car.id, isNotNull);
|
expect(car.id, isNotNull);
|
||||||
expect(car.make, 'Honda');
|
expect(car.make, 'Honda');
|
||||||
expect(car.description, 'Hello');
|
expect(car.description, 'Hello');
|
||||||
|
@ -180,7 +179,7 @@ main() {
|
||||||
familyFriendly: true,
|
familyFriendly: true,
|
||||||
recalledAt: recalledAt);
|
recalledAt: recalledAt);
|
||||||
var query = new CarQuery()..values.copyFrom(beetle);
|
var query = new CarQuery()..values.copyFrom(beetle);
|
||||||
var car = await query.insert(connection);
|
var car = await query.insert(executor);
|
||||||
print(car.toJson());
|
print(car.toJson());
|
||||||
expect(car.make, beetle.make);
|
expect(car.make, beetle.make);
|
||||||
expect(car.description, beetle.description);
|
expect(car.description, beetle.description);
|
|
@ -12,4 +12,5 @@ dependencies:
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
angel_orm_generator:
|
angel_orm_generator:
|
||||||
path: ../angel_orm_generator
|
path: ../angel_orm_generator
|
||||||
|
angel_framework: ^2.0.0-alpha
|
||||||
build_runner: ^1.0.0
|
build_runner: ^1.0.0
|
|
@ -1,5 +1,18 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
cd angel_orm_generator
|
export POSTGRES_USERNAME="angel_orm"
|
||||||
pub get
|
export POSTGRES_PASSWORD="angel_orm"
|
||||||
pub run build_runner build
|
|
||||||
POSTGRES_USERNAME="angel_orm" POSTGRES_PASSWORD="angel_orm" pub run test
|
|
||||||
|
|
||||||
|
function angel_orm_test () {
|
||||||
|
cd $1;
|
||||||
|
pub get;
|
||||||
|
pub run test;
|
||||||
|
cd ..
|
||||||
|
}
|
||||||
|
|
||||||
|
cd angel_orm_generator;
|
||||||
|
pub get;
|
||||||
|
pub run build_runner build;
|
||||||
|
cd ..;
|
||||||
|
angel_orm_test angel_orm_postgres
|
Loading…
Reference in a new issue